简体   繁体   中英

How I can send object to a view in Laravel?

How I can send object to a view, currently I have something like that:

Route::get('/search', 'sampleController@search')->name('search');

So, I have this object {Page_title:$pageName,error:$errorMesssage} and I want to pass it to /search route.

Also, how I can retrieve the object values in the view side.

I think you want to know how to send data from controller to view , if so

   Public function search(){
       $pageName = 'your page name';
       $error = 'something went wrong';

       return view('yourview')->with([
               'page_title'=> $pageName,
               'error'=> $errorMesssage
           ]);
   }

Now you can access those variable in your blade file as

{{$page_title}}

{{$error}}

It's probably worth mentioning that as of Laravel 5, passing data to the view is now done like this:

return view('blog')->with('pageName',  'Page Name')->with('errorMesssage', 'Error Message');

Or,

return view("blog", ["pageName"=> "Page Name", "errorMessage" => "Error Message" ]);

In your view:

Page : {{ $pageName ??  null }} // if variable not assigned then you don't get any error
Message : {{ $errorMessage ?? null }} // if variable not assigned then you don't get any error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM