Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zsoh's avatar
Level 1

Redirect and pass model instance from controller to view

Hi,

I wanted to redirect from my controller and pass model instance to view so I can call controller method from the view.

I can pass data like this from my controller to view:

return view('home', compact('data'));

However, I would like to pass a model and do it throught redirect. The following is not working: MyModel

public function action() {
    // 
}

MyController

$myModelInstance = MyModel::where('field', 'value')->first();
return redirect()->to('route1')->withModel($myModelInstance);

//route1 in web.php just call another method (in the same MyController) to return view('view1');

view1

{{ $myModelInstance->action() }}

In my view I am having undefined variable $myModelInstance.

Any advice please? Thanks

0 likes
5 replies
bobbybouwmann's avatar

You can't do that because the redirect only knows to what url it should go, not with what data. Instead you should pass the id in the url you redirect to and resolve the model again in the controller and pass it to the view

// RedirectController

public function action()
{
    $myModelInstance = MyModel::where('field', 'value')->first();


    return redirect()->route('route', $myModelInstance->id);
}

// ViewController
public function action(MyModel $myModel)
{
    return view('my-model.show', compact('myModel'));
}

So were using route model binding here, if you don't know what that is check here: https://laravel.com/docs/5.8/routing#route-model-binding

Does this make sense? Let me know if you have any follow up questions

1 like
zsoh's avatar
Level 1

@BOBBYBOUWMANN - Thank you @bobbybouwmann for letting me know route-model-binding. I wanted to do that, but is there any way to do it once if I want to access my model instance everywhere in my app? Let say I want to access certain model instance everywhere in my app. My app includes auth with my "users" table defined as "members". A member have many memberships and many-to-many relation between membership and role (so roles are not linked to members).

During login process (after auth ....in authenticated()), a member choose the membership and I am interested in membership (instead of member) during the member session. How can I "register" the choosen membership and use it throught member session like Auth::user()

The reason is that I want to know the current membership of the logged member everytime, including writing a blade directive in my service provider using the current membership. For example, the following is the @role directive using auth user. But I wanted to do it using the current membership (membership already have hasRole() method).

    Blade::directive('role', function ($role){
       return "<?php if(auth()->check() && auth()->user()->hasRole({$role})) :";
      });

Thanks,

zsoh's avatar
Level 1

Thanks @bobbybouwmann view composer is really what I was looking for ... Best answer :-)

Just currious ... any way to use view composer to write blade directive?

Thanks

bobbybouwmann's avatar

I think you're mixing up stuff here. View composers are there to share extra data to a view. A blade directive is like a reusable method that outputs something based on the data you give it.

Please or to participate in this conversation.