Passing data to views

One data variable to view

Create a custom route:

Route::get('/post/{id}',[PostsController::class, 'show_post']);

Create custom controller function that passes url data plus a string to the view

public function show_post($id){
    return view('post')->with('id',$id);
}

Create a post.blade.php file and add this line to it:

<h1>Post {{$id}}</h1>

You’ll see the url folder in the page now

Sending multiple data to view

Update your controller function to use compact like so:

public function show_post($id, $name, $another){
    return view('post', compact('id','name','another'));
}

Update route like this:

Route::get('/post/{id}/{name}/{another}',[PostsController::class, 'show_post']);

Update view:

<h1>Post {{$id}} {{$name}} {{$another}}</h1>

Leave a Reply

Your email address will not be published. Required fields are marked *