How to Pass Data to View in Laravel

How to pass data to view in Laravel?

You can pass data to the view using the with method.

return View::make('blog')->with('posts', $posts);

Pass data from controller to View in Laravel

This is the the definition of the view helper

function view($view = null, $data = [], $mergeData = []) { }

You are misusing the function by giving it three separate arrays expecting it to get them as $data.

Fixes

return view('ausencia', [
'tabela' => $tabela,
'itens' => $ferias,
'dias_usados' => $dias_usados,
]);
return view('ausencia')
->with(compact('tabela'))
->with(['itens' => $ferias])
->with(['dias_usados' => $dias_usados]);
return view(
'ausencia',
array_merge(
compact('tabela'),
['itens' => $ferias],
['dias_usados' => $dias_usados]
)
);

Passing data from controller to view in Laravel

Can you give this a try,

return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));

While, you can set multiple variables something like this,

$instructors="";
$instituitions="";

$compactData=array('students', 'instructors', 'instituitions');
$data=array('students'=>$students, 'instructors'=>$instructors, 'instituitions'=>$instituitions);

return View::make("user/regprofile", compact($compactData));
return View::make("user/regprofile")->with($data);

How to pass data to all views in Laravel 5?

This target can achieve through different method,

1. Using BaseController

The way I like to set things up, I make a BaseController class that extends Laravel’s own Controller, and set up various global things there. All other controllers then extend from BaseController rather than Laravel’s Controller.

class BaseController extends Controller
{
public function __construct()
{
//its just a dummy data object.
$user = User::all();

// Sharing is caring
View::share('user', $user);
}
}

2. Using Filter

If you know for a fact that you want something set up for views on every request throughout the entire application, you can also do it via a filter that runs before the request — this is how I deal with the User object in Laravel.

App::before(function($request)
{
// Set up global user object for views
View::share('user', User::all());
});

OR

You can define your own filter

Route::filter('user-filter', function() {
View::share('user', User::all());
});

and call it through simple filter calling.

Update According to Version 5.*

3. Using Middleware

Using the View::share with middleware

Route::group(['middleware' => 'SomeMiddleware'], function(){
// routes
});



class SomeMiddleware {
public function handle($request)
{
\View::share('user', auth()->user());
}
}

4. Using View Composer

View Composer also help to bind specific data to view in different ways. You can directly bind variable to specific view or to all views. For Example you can create your own directory to store your view composer file according to requirement. and these view composer file through Service provide interact with view.

View composer method can use different way, First example can look alike:

You could create an App\Http\ViewComposers directory.

Service Provider

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
public function boot() {
view()->composer("ViewName","App\Http\ViewComposers\TestViewComposer");
}
}

After that, add this provider to config/app.php under "providers" section.

TestViewComposer

namespace App\Http\ViewComposers;

use Illuminate\Contracts\View\View;

class TestViewComposer {

public function compose(View $view) {
$view->with('ViewComposerTestVariable', "Calling with View Composer Provider");
}
}

ViewName.blade.php

Here you are... {{$ViewComposerTestVariable}}

This method could help for only specific View. But if you want trigger ViewComposer to all views, we have to apply this single change to ServiceProvider.

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
public function boot() {
view()->composer('*',"App\Http\ViewComposers\TestViewComposer");
}
}

Reference

Laravel Documentation

For Further Clarification Laracast Episode

If still something unclear from my side, let me know.

Passing data from controller to view in Laravel - Display the error message Undefined variable

The steps to pass data to subview is by passing the data from the parent (In your case it is the homepage) to the subview (recent-gallery) with the help of the blade syntax.

You must first pass the data to the Homepage from your function which initialize your homepage route and pass the image data for your subview.

public function viewHomePage()
{
$images = ImageGallery::all();

// passing your image data to the homepage not to your subview

return view('home', ['images' => $images]);
}

You do not have to have a separate route for your subview as it is a subview. Pass the data you passed along from your Homepage view function to the subview by blade syntax @include('partials/recent-gallery', ['images' => $images]). Notice we have not routed a subview in web.php or have a controller function to pass image data.

home.blade.php

<!-- homepage content -->

@include('partials/recent-gallery', ['images' => $images])

Then you can access the image data inside your subview with the parameter $images. You can check your passing data with dd() method and passing the parameters dd($images) so you can debug.

partials/recent-gallery.blade.php

@if($images->count())
@foreach($images as $image)
{{ $image->galley_image }}
@endforeach
@endif

Pass data to view in Laravel

Make sure in your route file on routes/web.php you have a route to the index function like so:

Route::get('urlToYourView', 'YourController@index');

LARAVEL 7 - How to pass variables to views

I would be hilarious if I am right....

In your models where defining relations replace App/Project with App\Project. Do the same for Company.... Replace "/" with "\".

How to pass data from controller to view in Laravel

If you add get() to the end of your $category = Categories::where(['restorant_id' => $restorant_id]); statement, you'll have an Eloquent collection returned:

$category = Categories::where(['restorant_id' => $restorant_id])->get();

Pass the $category variable to your view as you are currently, consider renaming it to $categories though just to infer that there could be multiple.

Then in your view you can loop over the Category results and access the name property:

@forelse ($category as $cat)
{{ $cat->name }}
@empty
No categories.
@endforelse

Update

If you want to get items by their category_id, you can either do as you have done with $categories:

$items = Items::where(['category_id' => $category_id])->get();

Alternatively, if you have an items relationship on your Categories model you can access them that way:

$category = Categories::with('items')
->where(['restorant_id' => $restorant_id])
->get();

The above will eager load the items related to a category which you can then access in your view, for example:

@forelse ($category as $cat)
{{ $cat->name }}

@foreach ($cat->items as $item)
{{ $item->name }}
@endforeach

@empty
No categories.
@endforelse


Related Topics



Leave a reply



Submit