删除记录MethodNotAllowedHttpException

时间:2018-04-05 09:45:45

标签: php mysql laravel

我正在尝试从中删除记录 Db和我得到methodnotallowed:  HomeController的

public function index()
    {
        $YourData = DB::table('packages')
            ->join('users', 'users.id', '=', 'packages.user_id')
            ->select('users.name as username','packages.*',DB::raw('SUM(packages.entry_nr) as entries'))
            ->where('users.id', Auth::id())
            ->get();


             $id =Auth::user()->id;
            $lesson = DB::select("
            SELECT *
            FROM lessons t
            inner join bookings b
            on b.lessons_id =t.id
            where b.user_id = ".$id."
            and t.date > now() 
            ");
            return view('home') ->with('YourData', $YourData)
                                ->with('lesson', $lesson);

    }
public function destroy($lessonId,$authId)
{    $book= Booking::where('lessons_id',$lessonId)
        ->where('user_id',$authId)
        ->delete();

    return redirect()->action('HomeController@index');
}

我的表格就像这样

<form method="post" class="table_content_form">
        <form  method="post" action="{{action('HomeController@destroy', $ls->id, Auth::user()->id)}}">
          {{ csrf_field() }}
                <input name="_method" type="hidden" value="DELETE">
          <button class="btn btn-outline-danger" type="submit" >Rimuovi</button>
        </form>

我的路线Route::delete('/delete/','HomeController@destroy');

提前致谢!我正在尝试一步一步学习!

2 个答案:

答案 0 :(得分:0)

你应该试试这个:

$catscon = $this->db->query($cats);

更新回答::

在您的路线文件更改中。

Route::post('/delete/{id}/{userId}','HomeController@destroy')->name('deletedata');

public function destroy($lessonId,$authId)
{    $book= Booking::where('lessons_id',$lessonId)
        ->where('user_id',$authId)
        ->delete();

    return redirect()->action('HomeController@index');
}

<form method="post" class="table_content_form">
        <form  method="post" action="{{route('deletedata', [$ls->id, Auth::user()->id])}}">
          {{ csrf_field() }}
                <input name="_method" type="hidden" value="DELETE">
          <button class="btn btn-outline-danger" type="submit" >Rimuovi</button>
        </form>

在控制器文件中更改。

Route::post('/delete/{id}','HomeController@destroy')->name('deletedata');

在表单更改中

public function destroy($lessonId)
{    
$user_id = Auth::user()->id;
$book= Booking::where('lessons_id',$lessonId)
        ->where('user_id',$user_id)
        ->delete();

    return redirect()->action('HomeController@index');
}

答案 1 :(得分:0)

尝试在控制器中使用route()method_field('DELETE')并接收$request变量 你的表格应该是这样的

<form action="{{ route('Booking.destroy',$ls->id) }}" method="POST">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}
    <button class="btn btn-danger" type="submit">Delete
    </button>
</form>

你的路线应该是这样的

Route::post('/delete/{$id}','HomeController@destroy')->name('Booking.destroy');

你的控制器应该是

public function destroy(Request $request,$lessonId){
    $id =Auth::user()->id;
    $book= Booking::where('lessons_id',$lessonId)
    ->where('user_id',$id)
    ->delete();
    //Or you should try 
    //$book=Booking::destroy($lessonId);
    $request->session()->flash('message','Product deleted successfully');
    return redirect()->action('HomeController@index');
}