【问题标题】:Calling destroy method in resource controller在资源控制器中调用destroy方法
【发布时间】:2020-12-09 15:55:15
【问题描述】:

当我在 laravel 中使用资源控制器时,我无法理解如何调用 destroy 方法。 删除.blade.php

@extends('main')

@section('content')
<form method="POST" action="{{route('posts.destroy', '$post->id') }}"  >
@method('DELETE')
@csrf
    <select name="id">
        <option value="1">vddv</option>
        <option value="2">miss</option>
        <option value="3">miss</option>
        <option value="4">joy</option>
    </select>

      <br><br>
    <button type="submit"> Delete blog</button>
</form>
@endsection

资源控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\posts;
use Sessions; 


class PostController extends Controller
{ 
    public function create()
    {
        return view('posts.create');   
    }

    public function store(Request $request)
    {
       $post = new posts;
        $post->title = $request->input('title');
        $post->body = $request->input('body');
        $post->save();
        return redirect('posts/read');
    }

    public function show($data)
    {
       echo "show";
    }

    public function edit($id)
    {
        return view('posts.edit');
    }

    public function update(Request $req, $id)
    {
        echo posts::where('title' , $req->title)
        ->update(['body'=>$req->body]);
        return redirect('/');
    }

    public function destroy($id)
    {
        $post = posts::find($id);
        $post->delete();
        return redirect('/');

    }
}

路线:

Route::resource('posts', 'PostController');

在 GET 请求通过时调用 show 方法。请指导我如何调用销毁方法。如文档中所述,我使用表单方法欺骗传递 @method('DELETE'),因为 html 仅识别 GET 和 POST 方法。

【问题讨论】:

  • 您不能以这种方式删除表单。你只能这样做:stackoverflow.com/questions/46098806/…
  • 可能不相关,但你拼错了,把{{route('posts.destroy', '$post-&gt;id') }}改成{{route('posts.destroy', $post-&gt;id) }}
  • public function destroy($id) 中更改 $post = Post::find($id); 而不是 $post = posts::find($id); 并在控制器中更改 use App\Post; 而不是 use App\posts;

标签: php laravel laravel-5 backend laravel-7


【解决方案1】:

您可以使用以下代码。我希望它会起作用。

<form method="POST" action="{{ url('/posts' . '/' .$post->id) }}">
  {{ method_field('DELETE') }}
  {{ csrf_field() }}
  <select name="id">
    <option value="1">vddv</option>
    <option value="2">miss</option>
    <option value="3">miss</option>
    <option value="4">joy</option>
  </select>
  <button type="submit" title="Delete Post">Delete</button>
</form>

// In Controller

public function destroy($id) {
  Post::destroy($id);
  return redirect('posts')->with('flash_message', 'Post deleted!');
}

【讨论】:

    【解决方案2】:
    Hello, Brother please try this i hope it will work.
    
    {!! Form::open(['method'=>'DELETE', 'url' =>route('posts.destroy', $post->id),'style' => 'display:inline']) !!}
    
     {!! Form::button('<i class="ft-trash"></i>delete', array('type' => 'submit','class' => 'btn btn-defult','title' => 'Delete Post','onclick'=>'return confirm("Confirm delete?")')) !!}
    
    {!! Form::close() !!}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 2017-04-20
      • 2014-12-19
      • 2014-10-07
      • 1970-01-01
      相关资源
      最近更新 更多