Fetch data belongs to an ID using Laravel


Hello today I'm back to continue my Laravel tutorial series..What did I do in last tutorial? Do you remember? If not visit this link to grab the idea.
Fetch data using Laravel from database

Now I thought to do this for special reason. In the last article, I have shown you the way to fetch all data. You will remember I retrieved some news posts from the database with pagination. It's completed now. But think if you want to more details of each and every post, now you can? No definitely no.. So I'm going to implement it today. When you click on the post title, it will redirect you to the page which shows the details of single post, using ID.

Let's start!

First thing I need to tell is I will continue the project I have already started in the previous article. So, no need of creating new project. Just open your cmd and go into your project folder. Next step is to run the program. Type in cmd:
php artisan serve

Then open the web browser and type: http://localhost:8000/posts
Now see the view you got like this.


Now we can start the rest of the things...Open the project in your text editor and go into the file PostController.
I told before, this is a Resource Controller. So, it has in-built functions with empty bodies. We have already completed the index function. Now we need to implement show function with takes id as a parameter. Find it and enter this code.

public function show($id)
{
$post = Post::find($id);
return view('posts.show')->with('post',$post)->withTitle('Post Details');
}

That's it...This is the power of Laravel. Just 2 lines and work is completed! But there's another part. We need to create a view to display the single post details. And we need to pass the ID to the title in the page where the all posts are displaying. We have included it previously. Create a file called show.blade.php in  posts folder within the views folder. Since the controller is passing the ID, now we just need to echo the data of a post.

posts/show.blade.php


@extends('layouts.master')

@section('content')

@include('partials.navbar')
<div class="container">
<h1 style="margin-top: 30px;">{{$post->title}}</h1><br>
<div class="row">
<div class="col-lg-12">
<p style="font-size:20px;">{{$post->body}}</p>
<p style="font-size:14px;">Written on {{$post->created_at}}</p>
</div>
</div>
</div>
@endsection

Now the view is ready! Now go http://localhost:8000/posts to and click on a post title..What happened? You got a view like this?


Look at the URL..Now it has changed and ID has came to the party guys!!!! Now go back and click on another post title. Is its URL like this?
localhost:8000/posts/1
localhost:8000/posts/2

OK! Now we have to wrap up. The main thing I wanted to show you is the way to pass the ID to the URL and get data according to a specific record. I used ID foe my ease. You can use any thing that a post have, like title..You have to change the controller function. So try to do it and check the URL! I hope you will understand the way to do this. I'll be back with another article soon...

Good Luck guys! Enjoy Laravel...



Admin Semoga Informasi Yang Diberikan Melalui Web Ini Dapat Bermanfaat Bagi Saudara/i

No comments for "Fetch data belongs to an ID using Laravel"