How to delete message resource in laravel 5?

Laravel 5 version

I am working on a project with the new release of laravel 5, and for some reason I cannot delete the message, when I press delete, it just redirects me to the page after showing with the identifier, for example / post / 3, and I get a blank white page When I return to the index view, I get all the messages and it has not been deleted. Here is what I have below:

Message Migration File

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreatePostsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title')->default('');
            $table->string('slug')->default('');
            $table->text('body');
            $table->integer('author_id');
            $table->string('author');
            $table->string('featured_image');
            $table->softDeletes();
            $table->timestamps();
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('posts');
    }

}

PostsController

use Input;
use Redirect;
use Storage;
use SirTrevorJs;
use STConverter;
use Validator;
use Image;
use Boroughcc\Post;
use Boroughcc\Http\Requests;
use Boroughcc\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostsController extends Controller {
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $posts = Post::all();
        return view('posts.index', compact('posts'));
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update(Post $post)
    {
        //
        $this->middleware('auth');
        $input = array_except(Input::all(), '_method');
        $post->update($input);

        return Redirect::route('posts.show', $post->slug)->with('message', 'Post updated.');
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy(Post $post)
    {
        //
        //$post = Post::findOrFail($id);
        $post->delete();
        if($post->delete()) { 
            return Redirect::route('posts.index')->with('message', 'Post deleted.');
        }
    }

}

As far as I know, everything is fine, but I think this is a removal method that twists it all. In the routes file, I have installed route resources, so in php artisanorder to display the routes, I see the destruction route as follows:

|        | GET|HEAD                       | posts                                                 | posts.index   | Boroughcc\Http\Controllers\PostsController@index                 |            |
|        | GET|HEAD                       | posts/create                                          | posts.create  | Boroughcc\Http\Controllers\PostsController@create                |            |
|        | POST                           | posts                                                 | posts.store   | Boroughcc\Http\Controllers\PostsController@store                 |            |
|        | GET|HEAD                       | posts/{posts}                                         | posts.show    | Boroughcc\Http\Controllers\PostsController@show                  |            |
|        | GET|HEAD                       | posts/{posts}/edit                                    | posts.edit    | Boroughcc\Http\Controllers\PostsController@edit                  |            |
|        | PUT                            | posts/{posts}                                         | posts.update  | Boroughcc\Http\Controllers\PostsController@update                |            |
|        | PATCH                          | posts/{posts}                                         |               | Boroughcc\Http\Controllers\PostsController@update                |            |
|        | DELETE                         | posts/{posts}                                         | posts.destroy | Boroughcc\Http\Controllers\PostsController@destroy

Mail form with delete button

, .

{!! Form::open(array('class' => 'form-inline', 'method' => 'DELETE', 'route' => array('posts.destroy', $post->id))) !!}
                    <li>
                        <img src="{!! $post->featured_image !!}" alt="" />
                        <a href="{{ route('posts.show', $post->id) }}">{{ $post->title }}</a>
                    </li>
                    (
                        {!! link_to_route('posts.edit', 'Edit', array($post->slug), array('class' => 'btn btn-info')) !!},
                        {!! Form::submit('Delete', array('class' => 'btn btn-danger')) !!}
                    )
                {!! Form::close() !!}

, , , . .

+4
4

, Form, , HTML-.

:

<form action="{{ action('Controller@destroy') }}" method="DELETE">
...
</form>

:

<form action="{{ action('Controller@destroy') }}" method="POST">
    <input type="hidden" name="_method" value="DELETE">
    ...
</form>

:

+4

.

boot app/Providers/RouteServiceProvider.php int:

$router->bind('posts','Boroughcc\Post');

laravel posts ( route:list, ) should be bound to the Post`- (Laravel id)

app/Http/routes.php, :

Router::bind('posts','Boroughcc\Post');
0

,

$model -> find($id);
$model -> delete();

$model -> destroy($id);

$model -> destroy ([1,2,3]);
0

, :

:

<form class="" action="{{ url('/home/destroy') }}" method="post">
  <li><button type="submit"><i class="fa fa-btn fa-sign-out"></i>Destroy user</button></li>
  <input type="hidden" name="id" value="{{Auth::user()->id}}">
  {{ method_field('DELETE') }}
  {!! csrf_field() !!}
</form>

:

Route::group(['middleware' => 'web'], function () {
 Route::delete('/home/destroy',[
  'as'=> 'home.destroy',
  'uses'=> 'homeController@destroy',
 ]);
});

:

use app\User;
use Illuminate\Support\Facades\Auth;

protected function destroy(Request $request)
{
  $id= $request->id;
  if (Auth::user()->id == $id){
    $user= User::find($id);
    $user-> delete();
    Auth::logout();
    return redirect()->route('welcome');
  }else{
    return redirect()->route('home');
  }
}

if() , , Auth:: logout() - , , , == not === $id Auth:: user() → id, $id , Auth:: user() → id .

This method is equivalent to using get with this code:

View:

<li><a href="{{ url('/home/delete', Auth::user()->id) }}"><i class="fa fa-btn fa-sign-out"></i>Delete user</a></li>

Route:

Route::group(['middleware' => 'web'], function () {
  Route::get('/home/delete/{id}', 'homeController@delete');
});

Controller:

protected function delete($id)
{
  if (Auth::user()->id == $id){
    $user= User::find($id);
    $user-> delete();
    Auth::logout();
    return redirect()->route('welcome');
  }else{
    return redirect()->route('home');
  }
}
0
source

All Articles