In this video we are going to learn about the DB Query Builder and also we will see the crud operations using the DB Query Builder.
Laravel's database query builder provides a convenient, fluent interface to creating and running database queries.
It can be used to perform most database operations in your application and works on all supported database systems.
Before using the DB Query Builder make sure.
database configurations has been configured in your application.
Now lets see how can we perform the crud operations in laravel using query builder.
Goto the PostController and here in previous video i have created a method for getAllPost.
It was showing all the posts let check first.
You can see here its showing all the post which are stored in our database.
Now lets create methods for create post, get post by id,update post and delete post.
So start with create post first for that got to the PostController and crate a function.


public function addPost()
{
return view('add-post');
}


Now add the route for this function so go to the web.php file and add create the route.

Route::get('/add-post',[PostController::class,'addPost']);


Now lets create this add-post view.
So just go to the view folder and create add-post.blade.php.
And here first of all write the html5 biolerplate.
So write ! sign and press tab.
Then add bootsrap 4 here for better look and feel.
So go to the bootsrap4 official site and copy css and js from here and add in add-post.blade.php file.
Now inside the body lets create a form so write the following code.

<section>
<div class=\"container\">
<div class=\"row\">
<div class=\"col-md-6 offset-md-3\">
<div class=\"card\">
<div class=\"card-header\">
Add New Post
</div>
<div class=\"card-body\">
@if(Session::has('post_created')
<div class=\"alert alert-success\" role=\"alert\"> {{Session::get('post_created')}} </div>
@endif
<form method=\"POST\" action=\"{{route('post.add')}}\">
@csrf
<div class=\"form-group\">
<label for=\"title\">Post Title</label>
<input type=\"text\" name=\"title\" class=\"form-control\" placeholder=\"Enter post title\" />
</div>
<div class=\"from-group\">
<label for=\"body\">Post Description</label>
<textarea class=\"form-control\" name=\"body\" rows=\"5\"></textarea>
</div>
<button type=\"submit\" class=\"btn btn-success\">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
</section>


Now go to the post controller and create another function.

public function addPostSubmit()
{
DB::table('posts')->insert([
'title' => $request->title,
'body' => $request->body
]);
return back()->with('post_created','Post has been created successfully!');
}


Alright now create routes for this.
So go to the web.php file and here lets creat a route.

Route::post('/add-post/',[PostController::class, 'addPostSubmit ')->name('post.add');


Alright now lets crate a post.
so goto the browser open url localhost:8000/add-post.
Now add a post here so just add a post title and post description and then click on submit.
You can see post is created.
Lets see all the post so go to the /posts.
You can see here all the post along with newly crated post.
Inside the posts view, add bootstrap for better view.
So just copy css and js from add-post.blade.php file and paste here.
Now make the following changes in code.

<section>
<div class=\"container\">
<div class=\"row\">
<div class=\"col-md-12\">
<div class=\"card\">
<div class=\"card-header\">
All Posts
</div>
<div class=\"card-body\">
<table>
<thead>
<tr>
<th>Post Title</th>
<th>Post Body</th>
</tr>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{$post->title}}</td>
<td>{{$post->body}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</section>


Now save the file see the changes on posts view.
Here you can see its looking better.
Now create another method for fetching the post by their id.
So goto the PostController and here lets create function.

public function getPostById($id)
{
$result = DB::table('posts')->where('id',$id)->first();
return view('single-post',['post',$result]);
}

Now create a view for single post.
So go to the views directory and right click on views folder.
Create new file single-post.blade.php.
Inside this view just copy text from other view and paste here now.
And Make the following changes.


<section>
<div class=\"container\">
<div class=\"row\">
<div class=\"col-md-12\">
<div class=\"card\">
<div class=\"card-header\">
Post
</div>
<div class=\"card-body\">
<h3>{{$post->title}}</h3>
<p>{{$post->body}}</p>
</div>
</div>
</div>
</div>
</div>
</section>


Now crate the route.
Goto the web.php file and here just type.


Route::get::('/post/{id}',[PostController::class,'getPostById')->name(post.getbyid);


Now go to the post.blade.php view file and inside the table body add the link.

<a href=\"{{route('postbyid',['id'=>$post->id])}}\">View Post</a>

Now check this so go to the browser.
Lets open /posts
Now click on view post.
you can see the post.
Once again click on other link and here it is.

Alright lets create another method for delete the post.
So go to the PostController and just create a function.


public function deletePost($id)
{
DB::table('posts')->where('id',$id)->delete();
return back()->with(\"post_deleted\", \"Post is deleted sucessfully!\");
}


Go to the posts.blade.php view file and before the table lets add the following code for showing the message.
@if(Session::has('post_deleted')
<div class=\"alert alert-success\" role=\"alert\"> {{Session::get('post_deleted')}} </div>
@endif

Now Create the route for deletePost.
So go to the web.php and creat the route.


Route::get('/delete-post/{id}','PostController@deletePost')->name('post.delete');


Now implement this route inside posts view.
Goto the post.blade.php file and here create a link for delete the post.
so inside the table body just write.

<a href=\"{{route('post.delete',['id',$post->id])}}\">Delete</a>

Now lets check, switch to the browser and go to the url /posts.
Now you can see here the delete link in each row.
Now you can see here post is deleted.
Try one more time.
Delete another post and see post deleted.

In last lets see how can we update a post using DB query builder.
So for that switch to PostController and here create following functions.



public function editPost($id)
{
$post = DB::table('posts')->where('id',$id);
return view('edit-post',['post'=>$post]);
}


public updatePost($Request $request){
DB::table('posts')->where('id',$request->id)->update(['title'=>$request->title,'body'=>$body]);
return \"Post has been update successfully!\";
}


Now create route

Route::get('edit-post/{id}','PostController@editPost')->name('post.edit');
Route::get('/update-post',[PostController::class,'updatePost'])->name(post.update);


Now Create a view for edit the post.
So go to the views folder and here crate new view.
Let say name is edit-post.blade.php.
Now here crate a form.
So just copy all the text from post.blade.php and make the following changes.


<form action=\"{{route('post.add')}}\" method=\"post\">
@csrf
<div class=\"form-group\">
<label for=\"title\">Post ID</label>
<input type=\"text\" name=\"id\" class=\"form-control\" value=\"{{$post->id}}\" >
</div>

<div class=\"form-group\">
<label for=\"title\">Post Title</label>
<input type=\"text\" name=\"title\" class=\"form-control\" value=\"{{$post->title}}\" placeholder=\"Enter Post Title\">
</div>

<div class=\"form-group\">
<label for=\"body\">Enter Post Body </label>
<textarea class=\"form-control\" name=\"body\" rows=\"3\">{{$post->body}}</textarea>
</div>
<input type=\"submit\" value=\"Update\" />
</form>


Now goto the posts.blade.php and make a link here for edit the post.
Inside the table bode lets write the following code.

<a herf=\"{{route('post.edit',['id'=>$post->id])}}\">Edit Post</a>


Now just check it. Switch to the browser and go to url /posts.
Now click on edit.
Now make some changes here and click on update button.
You can see here post is updated.
Lets see all the posts, here is updated post.
So in this way you can perform the crud operations using DB Query Builder.