Laravel 5.3 tutorial for beginners : how to create blog using laravel 5.3 part8 : deleting data in database using Eloquent Method in Laravel 5.3
Create blog using laravel 5.3 part8 - this lessons about how to Deleting Data Using Eloquent Method in Laravel 5.3 apps/blog. At the previews lessons, we have learned about displaying data from database, make a new form to create new data and save it into database, validation, create single page, error valitaion in laravel 5.3, you just read that tutorial read previews lessons :
BlogController.php
Next, we will create a link or a button delete in our index page, just edit your index page (index.blade.php)
index.blade.php
See you next Lessons ....
How to Delete data with Eloquent Syntax
First, we need to create new function to delete data in our database, in our controller (BlogController.php) add "destory" function like this :
BlogController.php
public function destroy($id)
{
$blog = Blog::find($id);
$blog->delete();
return redirect('blog')->with('message','data hasbeen deleted!');
}
Next, we will create a link or a button delete in our index page, just edit your index page (index.blade.php)
index.blade.php
{{ Session::get('message') }}
<h1>My First Blog</h1>
@foreach ($blogs as $blog)
<h2><a href="/blog/{{ $blog->id }}">{{ $blog->title }}</a></h2>
{{ date('F d, Y', strtotime($blog->created_at)) }}
<p>{{ $blog->description }}</p>
<a href="/blog/{{ $blog->id }}/edit">Edit this Post</a><br>
<form class="" action="/blog/{{ $blog->id }}" method="post">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" name="name" value="delete">
</form>
<hr>
@endforeach
Video Tutorial Deleting Data Using Eloquent Method in Laravel 5.3
See you next Lessons ....
COMMENTS