Setup Menus in Admin Panel

  • No products in the cart.

  • LOGIN

Laravel Views


Views separate the application logic and the presentation logic. Views are stored in resources/views directory. Generally, the view contains the HTML which will be served by the application.

 

Creating a Views:

Look at the below example:

<!-- View stored in resources/views/greeting.blade.php -->

<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>

Since this view is stored at resources/views/greeting.blade.php, we may return it using the global view helper like so:

Route::get('/', function () {
return view('greeting', ['name' => 'Joseph']);
});

Here, you can see the first argument passed to the view helper corresponds to the name of the view file in the resources/views directory. The second argument is an array of data that should be made available to the view. In this case, we are passing the name variable, which is displayed in the view using Blade syntax.

Views can also be nested within sub-directories of the resources/views directory. “Dot” notation may be used to reference nested views.
For example, if your view is stored at resources/views/admin/profile.blade.php, you may reference it like so:

return view('admin.profile', $data);

 

Determining If a View Exists

If you need to determine if a view exists, you may use the View facade. The exists method will return true if the view exists:

use Illuminate\Support\Facades\View;

if (View::exists(’emails.customer’)) {
//
}

 

Creating The First Available View

Using the first method, you may create the first view that exists in a given array of views. This is useful if your application or package allows views to be customized or overwritten:

return view()->first(['custom.admin', 'admin'], $data);
Of course, you may also call this method via the View facade:

use Illuminate\Support\Facades\View;

return View::first(['custom.admin', 'admin'], $data);

 

Passing Data To Views:

You have already seen the example, Now you can pass an array of data to views:

return view('greetings', ['name' => 'sia']);

When passing information in this manner, the data should be an array with key/value pairs. Inside your view, you can then access each value using its corresponding key, such as <?php echo $key; ?>. As an alternative to passing a complete array of data to the view helper function, you may use with the method to add individual pieces of data to the view:

return view('greeting')->with('name', 'sia');

 

Sharing Data With All Views

Sometimes you need to share a piece of data with all views that are rendered by your application. You may do so using the view facade’s share method. You should place calls to share within a service provider’s boot method. You can also add them to the AppServiceProvider or generate a separate service provider to house them:

<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
View::share('key', 'value');
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}
}
SEE ALL Add a note
YOU
Add your Comment
 

Advanced Course Search Widget

Popular Courses

Course Categories

© PHPhelp. 2002 - 2018 All rights reserved.
X