Laravel Jetstream 更新不是从表单触发的,而是重定向到错误的页面
Laravel Jetstream Update not being triggered from form and instead redirecting to wrong page
我一直在尝试获取一个表格来更新已经存在的数据库。我已经有效地将完整的工作代码从我的创建 blade 复制并粘贴到我的编辑 blade 并且它有一个奇怪的问题,而不是编辑数据库它似乎 运行 错误的功能。
当我单击 index.blade.php 中的按钮导航到编辑 blade 时,一切看起来都不错,但是当我单击保存按钮而不是按应有的方式保存并停止时,因为我注释掉了导航线似乎是 运行 控制器显示功能并导航到 projects.courses.index.
我已阅读以确保表单不会使用 POST 进行更新,即使 POST 在我的创建 blade 上工作正常。当我在表单标记中将它保留为 POST 并添加一个方法使其稍后 PUT 时,它会抛出 403 错误。我怀疑这可能真的是我想要它做的,然后我需要更新我的 web.php 路线,但我不确定如果那是我需要做的,我会放在那里。
感谢您提供的任何帮助。
这是web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProjectController;
use App\Http\Controllers\CourseController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::group(['middleware'=>'auth'], function() {
Route::resource('projects', ProjectController::class);
Route::resource('projects.courses', CourseController::class);
});
这是控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\StoreProjectRequest;
use App\Http\Requests\UpdateProjectRequest;
use App\Models\Project;
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$projects = Project::all();
return view('projects.index') -> with('name', 'Projects') -> with(compact('projects'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('projects.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(StoreProjectRequest $request)
{
$row = new Project;
$row->code = $request->code;
$row->code_full = $request->code_full;
$row->title = $request->title;
$row->description = $request->description;
$row->save();
return redirect()->route('projects.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Project $project)
{
return redirect()->route('projects.courses.index', $project);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Project $project)
{
return view('projects.edit') -> with('project', $project);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(UpdateProjectRequest $request, $id)
{
$row = Project::find($id);
$row->code = $request->code;
$row->code_full = $request->code_full;
$row->title = $request->title;
$row->description = $request->description;
$row->save();
//return redirect()->route('projects.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Project $project)
{
$project->delete();
return redirect()->route('projects.index');
}
}
编辑如下blade
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Edit Project') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="mt-10 sm:mt-0">
<div class="md:grid md:grid-cols-3 md:gap-6">
<div class="md:col-span-1">
<div class="px-4 sm:px-0">
<h3 class="text-lg font-medium leading-6 text-gray-900">Project Information</h3>
<p class="mt-1 text-sm text-gray-600">
Please fill out this form to edit the project {{$project->title}}.
</p>
</div>
</div>
<div class="mt-5 md:mt-0 md:col-span-2">
<form action="{{ route('projects.update', $project->id) }}" method="PUT" enctype="multipart/form-data">
@csrf
<div class="shadow overflow-hidden sm:rounded-md">
<div class="px-4 py-5 bg-white sm:p-6">
<div class="grid grid-cols-6 gap-6">
<div class="col-span-6 sm:col-span-3">
<label for="code" class="block text-sm font-medium text-gray-700">Project ID (4 digit code)</label>
<input type="text" name="code" id="code" autocomplete="" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="{{ $project->code }}" value="{{ old('code', '') }}">
@error('code')
<p class="text-sm text-red-600">{{$message}}</p>
@enderror
</div>
<div class="col-span-6 sm:col-span-3">
<label for="code_full" class="block text-sm font-medium text-gray-700">Project Code</label>
<input type="text" name="code_full" id="code_full" autocomplete="" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="{{ $project->code_full }}" value="{{ old('code_full', '') }}">
@error('code_full')
<p class="text-sm text-red-600">{{$message}}</p>
@enderror
</div>
<div class="col-span-6 sm:col-span-4">
<label for="title" class="block text-sm font-medium text-gray-700">Project Title</label>
<input type="text" name="title" id="title" autocomplete="" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="{{ $project->title }}" value="{{ old('title', '') }}">
@error('title')
<p class="text-sm text-red-600">{{$message}}</p>
@enderror
</div>
<div class="col-span-6">
<label for="description" class="block text-sm font-medium text-gray-700">Description</label>
<input type="text" name="description" id="description" autocomplete="" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="{{ $project->description }}" value="{{ old('description', '') }}">
@error('description')
<p class="text-sm text-red-600">{{$message}}</p>
@enderror
</div>
</div>
</div>
<div class="px-4 py-3 bg-gray-50 text-right sm:px-6">
<button type="submit" class="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
Save
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</x-app-layout>
我是对的,403 错误是我应该得到的,但这不是路由问题。以下是我最终解决它的方法,因为它归结为代码在需要为真的地方有一个假。
第一步:
在编辑 blade 中,表单方法应为“POST”,在 @csrf 下我需要放置一个 @method('PUT') 我曾尝试过一次但出现 403 错误并且解开它。
第 2 步:
控制器更新应该稍微更改为
public function update(UpdateProjectRequest $request, Project $project)
{
$row = Project::find($project->id);
第 3 步:
要结束 403 错误,我需要转到 UpdateProjectRequest 并将 false 更改为 true。
public function authorize()
{
return true;
}
我一直在尝试获取一个表格来更新已经存在的数据库。我已经有效地将完整的工作代码从我的创建 blade 复制并粘贴到我的编辑 blade 并且它有一个奇怪的问题,而不是编辑数据库它似乎 运行 错误的功能。
当我单击 index.blade.php 中的按钮导航到编辑 blade 时,一切看起来都不错,但是当我单击保存按钮而不是按应有的方式保存并停止时,因为我注释掉了导航线似乎是 运行 控制器显示功能并导航到 projects.courses.index.
我已阅读以确保表单不会使用 POST 进行更新,即使 POST 在我的创建 blade 上工作正常。当我在表单标记中将它保留为 POST 并添加一个方法使其稍后 PUT 时,它会抛出 403 错误。我怀疑这可能真的是我想要它做的,然后我需要更新我的 web.php 路线,但我不确定如果那是我需要做的,我会放在那里。
感谢您提供的任何帮助。
这是web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProjectController;
use App\Http\Controllers\CourseController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::group(['middleware'=>'auth'], function() {
Route::resource('projects', ProjectController::class);
Route::resource('projects.courses', CourseController::class);
});
这是控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\StoreProjectRequest;
use App\Http\Requests\UpdateProjectRequest;
use App\Models\Project;
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$projects = Project::all();
return view('projects.index') -> with('name', 'Projects') -> with(compact('projects'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('projects.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(StoreProjectRequest $request)
{
$row = new Project;
$row->code = $request->code;
$row->code_full = $request->code_full;
$row->title = $request->title;
$row->description = $request->description;
$row->save();
return redirect()->route('projects.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Project $project)
{
return redirect()->route('projects.courses.index', $project);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Project $project)
{
return view('projects.edit') -> with('project', $project);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(UpdateProjectRequest $request, $id)
{
$row = Project::find($id);
$row->code = $request->code;
$row->code_full = $request->code_full;
$row->title = $request->title;
$row->description = $request->description;
$row->save();
//return redirect()->route('projects.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Project $project)
{
$project->delete();
return redirect()->route('projects.index');
}
}
编辑如下blade
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Edit Project') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="mt-10 sm:mt-0">
<div class="md:grid md:grid-cols-3 md:gap-6">
<div class="md:col-span-1">
<div class="px-4 sm:px-0">
<h3 class="text-lg font-medium leading-6 text-gray-900">Project Information</h3>
<p class="mt-1 text-sm text-gray-600">
Please fill out this form to edit the project {{$project->title}}.
</p>
</div>
</div>
<div class="mt-5 md:mt-0 md:col-span-2">
<form action="{{ route('projects.update', $project->id) }}" method="PUT" enctype="multipart/form-data">
@csrf
<div class="shadow overflow-hidden sm:rounded-md">
<div class="px-4 py-5 bg-white sm:p-6">
<div class="grid grid-cols-6 gap-6">
<div class="col-span-6 sm:col-span-3">
<label for="code" class="block text-sm font-medium text-gray-700">Project ID (4 digit code)</label>
<input type="text" name="code" id="code" autocomplete="" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="{{ $project->code }}" value="{{ old('code', '') }}">
@error('code')
<p class="text-sm text-red-600">{{$message}}</p>
@enderror
</div>
<div class="col-span-6 sm:col-span-3">
<label for="code_full" class="block text-sm font-medium text-gray-700">Project Code</label>
<input type="text" name="code_full" id="code_full" autocomplete="" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="{{ $project->code_full }}" value="{{ old('code_full', '') }}">
@error('code_full')
<p class="text-sm text-red-600">{{$message}}</p>
@enderror
</div>
<div class="col-span-6 sm:col-span-4">
<label for="title" class="block text-sm font-medium text-gray-700">Project Title</label>
<input type="text" name="title" id="title" autocomplete="" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="{{ $project->title }}" value="{{ old('title', '') }}">
@error('title')
<p class="text-sm text-red-600">{{$message}}</p>
@enderror
</div>
<div class="col-span-6">
<label for="description" class="block text-sm font-medium text-gray-700">Description</label>
<input type="text" name="description" id="description" autocomplete="" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" placeholder="{{ $project->description }}" value="{{ old('description', '') }}">
@error('description')
<p class="text-sm text-red-600">{{$message}}</p>
@enderror
</div>
</div>
</div>
<div class="px-4 py-3 bg-gray-50 text-right sm:px-6">
<button type="submit" class="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
Save
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</x-app-layout>
我是对的,403 错误是我应该得到的,但这不是路由问题。以下是我最终解决它的方法,因为它归结为代码在需要为真的地方有一个假。
第一步: 在编辑 blade 中,表单方法应为“POST”,在 @csrf 下我需要放置一个 @method('PUT') 我曾尝试过一次但出现 403 错误并且解开它。
第 2 步: 控制器更新应该稍微更改为
public function update(UpdateProjectRequest $request, Project $project)
{
$row = Project::find($project->id);
第 3 步: 要结束 403 错误,我需要转到 UpdateProjectRequest 并将 false 更改为 true。
public function authorize()
{
return true;
}