mahmoud fathy
mahmoud fathy

Reputation: 391

Bind multiple models to one View Controller in laravel

I am starting to give up on laravel for not being able to follow a certain pattern that I always have been taking for granted !!

let's assume I have the example from this link https://laravel.com/docs/5.4/container#introduction

public function show($id)
{
    $user = $this->users->find($id);

    return view('user.profile', ['user' => $user]);
}

This user model is bound to that controller to be shown in the view. What if I need to inject another model, let's call it plane which is independent from users model (no DB relation). So that I have both of them listed in the same view.

In Angular for instance I could create multiple components on the same page and each have their own independent controllers and models.

Upvotes: 1

Views: 5388

Answers (3)

Hasnain Abid Khanzada
Hasnain Abid Khanzada

Reputation: 165

Simply put, One controller can send request to multiple models to update single view, So yes you can use multiple models in one controller

Upvotes: 1

yoeunes
yoeunes

Reputation: 2945

yes you can bind multiple models with laravel like this :

public function show(User $user, Plane $plane)
{
    return view('user.plane', compact('user', 'plane')]);
}

and as @apokryfos said your example does not have any binding in the controller,

this is an example for the corresponding route:

Route::get('/fakeRoute/{user}/{plane}', 'FakeController@show');

link example:

yourSite.com/fakeRoute/1/25

Upvotes: 2

apokryfos
apokryfos

Reputation: 40673

Laravel supports contextual binding.

It also has shortcuts for binding database models:

Model binding

In your RouteServiceProvider you can have:

Route::model("user", App\User::class);

Then your route declared as:

Route::get("/users/{user}","UserController@show");

And your controller:

public function show(User $user) {
   //$user->id is based on the {user} route parameter
}

Other contextual binding

Route::bind("plane", function ($id, RouteInfo $routeInfo) {
      // Get the plane object based on the given $id and optionally the extra route info parameters
     return $planeObject;
});

Your route can be declared as:

 Route::get("/planes/{plane}", function (Plane $plane) {
      //$plane will depend on passed parameter
 });

Laravel also supports normal binding:

In your AppServiceProvider you can have:

 $this->app->bind(Plane::class, function ($app) {
      //Make a plane class object
     return $planeObject;
 });

Then in your controller method (or any place where Laravel allows dependency injection to occur) you can do:

public function show(Plane $plane) {
   //Plane is the globally declared binding
} 

You can also combine the two e.g:

Route::model("user", App\User::class);
$this->app->bind(Plane::class, function ($app) {
      //Make a plane class object
     return $planeObject;
 });

 Route::get("/users/{user}","UserController@show");

 public function show(User $user, Plane $plane) {
   //$user->id is based on the {user} route parameter and $plane is resolved using the service container
}

Upvotes: 5

Related Questions