Laravel 更新和销毁路线不起作用
Larvel Update and Destroy route not working
接受按钮有路由 route('members.update', $member->id)
拒绝按钮有路由route('members.destroy', $member->id)
这些是卡片:
@foreach ($members as $member)
<div class="my-1 px-1 w-full md:w-1/2 lg:my-4 lg:px-4 lg:w-1/3">
<article class="bg-white overflow-hidden rounded-lg shadow-lg">
<div class="flex items-center justify-between leading-tight p-2 md:p-4 w-max">
<div class="w-1/2">
<small class="text-gray-600">Full name:</small>
<h1 class="text-lg pr-2"> {{ $member->firstname }} {{ $member->lastname }} </h1>
</div>
<div class="w-1/2">
<small class="text-gray-600">Birthdate:</small>
<h1 class="text-lg">{{ date('d-m-Y', strtotime($member->birthdate)) }}</h1>
</div>
</div>
<div class="flex items-center justify-between leading-tight p-2 md:p-4">
<div class="w-1/2">
<small class="text-gray-600">Student id:</small>
<h1 class="text-lg pr-2">{{ $member->studentid }}</h1>
</div>
<div class="w-1/2">
<small class="text-gray-600">Discord:</small>
<h1 class="text-lg">{{ $member->discordname }}{{ $member->discordtag }}</h1>
</div>
</div>
<footer class="flex items-center leading-tight p-2 md:p-4 justify-start">
<form method="PATCH" action="{{ route('members.update', $member->id) }}">
@method('PATCH')
<button class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded mr-2">Accept</button>
@csrf
</form>
<form method="DELETE" action="{{ route('members.destroy', $member->id) }}">
@method('DELETE')
<button class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded">Deny</button>
@csrf
</form>
</footer>
</article>
</div>
@endforeach
这是我的控制器:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Member;
use Illuminate\Http\Request;
class MemberController extends Controller {
public function index() {
$members = Member::where(['status' => false])->get();
return view('dashboard')->with(['members' => $members]);
}
public function show($id) {
$members = Member::where(['id' => $id])->get();
return view('dashboard')->with(['members' => $members]);
}
public function store(Request $request) {
$validatedData = $request->validate([
'firstname' => 'required|max:255|alpha|min:2',
'lastname' => 'required|max:255|alpha|min:2',
'studentid' => 'required|integer|alpha_num',
'birthdate' => 'required|date',
'discordname' => 'required|max:255',
'discordtag' => 'required|max:255|size:5'
]);
$member = new Member;
$member->firstname = $request->firstname;
$member->lastname = $request->lastname;
$member->studentid = $request->studentid;
$member->birthdate = $request->birthdate;
$member->discordname = $request->discordname;
$member->discordtag = $request->discordtag;
$member->status = false;
$member->save();
return \redirect()->route('index');
}
public function update($id) {
die;
$member = Member::find($id);
$member->status = 1;
$member->save();
return \redirect()->route('dashboard');
}
/**
* TODO - SEND EMAIL ON DELETE
*
* @return void
*/
public function destroy($id) {
DB::delete('delete members where id = ?', [$id]);
}
public function accepted($id) {
update($id);
}
}
我的路线:
当我单击接受或拒绝按钮时,它会将我发送到此 link:http://hit.localhost.nl/members/3?_method=PATCH&_token=0g5odDLEKCicceDOf4EuPMGIB1X95cwWHGMxMqcR
并运行 show() 函数而不是更新或销毁函数。我在这里做错了什么?
method="PATCH"
和 method="DELETE"
不存在,只有 get
、post
和 dialog
。将两者都更改为 method="post"
.
接受按钮有路由 route('members.update', $member->id)
拒绝按钮有路由route('members.destroy', $member->id)
这些是卡片:
@foreach ($members as $member)
<div class="my-1 px-1 w-full md:w-1/2 lg:my-4 lg:px-4 lg:w-1/3">
<article class="bg-white overflow-hidden rounded-lg shadow-lg">
<div class="flex items-center justify-between leading-tight p-2 md:p-4 w-max">
<div class="w-1/2">
<small class="text-gray-600">Full name:</small>
<h1 class="text-lg pr-2"> {{ $member->firstname }} {{ $member->lastname }} </h1>
</div>
<div class="w-1/2">
<small class="text-gray-600">Birthdate:</small>
<h1 class="text-lg">{{ date('d-m-Y', strtotime($member->birthdate)) }}</h1>
</div>
</div>
<div class="flex items-center justify-between leading-tight p-2 md:p-4">
<div class="w-1/2">
<small class="text-gray-600">Student id:</small>
<h1 class="text-lg pr-2">{{ $member->studentid }}</h1>
</div>
<div class="w-1/2">
<small class="text-gray-600">Discord:</small>
<h1 class="text-lg">{{ $member->discordname }}{{ $member->discordtag }}</h1>
</div>
</div>
<footer class="flex items-center leading-tight p-2 md:p-4 justify-start">
<form method="PATCH" action="{{ route('members.update', $member->id) }}">
@method('PATCH')
<button class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded mr-2">Accept</button>
@csrf
</form>
<form method="DELETE" action="{{ route('members.destroy', $member->id) }}">
@method('DELETE')
<button class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded">Deny</button>
@csrf
</form>
</footer>
</article>
</div>
@endforeach
这是我的控制器:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Member;
use Illuminate\Http\Request;
class MemberController extends Controller {
public function index() {
$members = Member::where(['status' => false])->get();
return view('dashboard')->with(['members' => $members]);
}
public function show($id) {
$members = Member::where(['id' => $id])->get();
return view('dashboard')->with(['members' => $members]);
}
public function store(Request $request) {
$validatedData = $request->validate([
'firstname' => 'required|max:255|alpha|min:2',
'lastname' => 'required|max:255|alpha|min:2',
'studentid' => 'required|integer|alpha_num',
'birthdate' => 'required|date',
'discordname' => 'required|max:255',
'discordtag' => 'required|max:255|size:5'
]);
$member = new Member;
$member->firstname = $request->firstname;
$member->lastname = $request->lastname;
$member->studentid = $request->studentid;
$member->birthdate = $request->birthdate;
$member->discordname = $request->discordname;
$member->discordtag = $request->discordtag;
$member->status = false;
$member->save();
return \redirect()->route('index');
}
public function update($id) {
die;
$member = Member::find($id);
$member->status = 1;
$member->save();
return \redirect()->route('dashboard');
}
/**
* TODO - SEND EMAIL ON DELETE
*
* @return void
*/
public function destroy($id) {
DB::delete('delete members where id = ?', [$id]);
}
public function accepted($id) {
update($id);
}
}
我的路线:
当我单击接受或拒绝按钮时,它会将我发送到此 link:http://hit.localhost.nl/members/3?_method=PATCH&_token=0g5odDLEKCicceDOf4EuPMGIB1X95cwWHGMxMqcR
并运行 show() 函数而不是更新或销毁函数。我在这里做错了什么?
method="PATCH"
和 method="DELETE"
不存在,只有 get
、post
和 dialog
。将两者都更改为 method="post"
.