Thứ Hai, 19 tháng 9, 2016

#Part8 laravel 5 Blog Tutorial : Deleting Data 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 :
  1. Update Database Recorded in laravel 5.3
  2. Create Single Page and 404 Page in laravel 5.3

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 :

Deleting Data Using Eloquent Method in Laravel 5.3

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 ....

Không có nhận xét nào:

Đăng nhận xét