'Having hard time understanding Forms from laravel collectives

I'm new to Laravel. I've created a form and trying to delete one of the post. I just want to know how opening a form from Laravel collectives works.

Currently, this is how my form looks like in show.blade.php

   {!! Form::open(['action' => ['PostsController@destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
    {{ Form::hidden('_method', 'DELETE') }}
    {{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
    {!! Form::close() !!}

The above form gives me the error Action PostsController@destroy not defined.

But when I add 'url' => 'posts/' in Form i.e.

    {!! Form::open(['url' => 'posts/', 'action' => ['PostsController@destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}

The above error disappears.

Below is the destroy() function in PostsController

class PostsController extends Controller
{
    public function destroy($id)
    {
        $post = Post::find($id);
        $post->delete();
        return redirect('/posts')->with('success','Post removed');
    }
}

web.php

// --- Posts Routing
Route::get('/posts', [App\Http\Controllers\PostsController::class, 'index'])->name('posts');

MY QUESTIONS

  • I'm redirecting in the destroy() function in PostsController, why url is necessary with action in Form from Laravel collectives to avoid the above error?
  • When to use 'url' and when to use action?

I've laravel 4.2.10.

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source