Passing request parameter in View - Laravel

Is it possible to pass the parameter routeto the controller and then go to the view in laravel?

Example

I have a route below;

Route::get('post/{id}/{name}', 'BlogController@post')->name('blog-post');

I want to convey {id}, and {name}to my mind, so in my controller

class BlogController extends Controller
{
    //
     public function post () {

     //get id and name and pass it to the view

        return view('pages.blog.post');
    }
}
+4
source share
2 answers

You can use:

public function post ($id, $name) 
{
   return view('pages.blog.post', ['name' => $name, 'id' => $id]);
}

or even shorter:

public function post ($id, $name) 
{
   return view('pages.blog.post', compact('name', 'id'));
}

EDIT If you need to return it as JSON, you can simply do:

public function post ($id, $name) 
{
   return view('pages.blog.post', ['json' => json_encode(compact('name', 'id'))]);
}
+2
source

Something like this work?

class BlogController extends Controller
{
    //
     public function post ($id, $name) {

     //get id and name and pass it to the view

        return view('pages.blog.post', ['name' => $name, 'id' => $id]);
    }
}
+2
source

Source: https://habr.com/ru/post/1621895/


All Articles