gpt4 book ai didi

php - Laravel 路由模型绑定(bind) : destroy method not grab necessary info

转载 作者:行者123 更新时间:2023-12-04 02:52:40 25 4
gpt4 key购买 nike

这是我的 ProducerType 模型 Controller :

namespace App\Http\Controllers;

use App\ProducerType;
use App\Http\Requests\ValidateProducerTypes;
use Illuminate\Http\Request;

class ProducerTypeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}

...

public function destroy(ProducerType $producerType)
{
$producerType->delete();
return redirect('/producers');
}
}

这是我的模型:
namespace App;

use Illuminate\Database\Eloquent\Model;

class ProducerType extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'nome'
];
}

看法:
<form action="/producers-type/{{ $type->id }}" method="POST">
@csrf
@method('DELETE')
<button type="submit" class="btn-icon">
<img src="{{ asset('images/times-circle-regular.svg') }}" alt="">
</button>
</form>

路线:
Route::resource('producers-type', 'ProducerTypeController', [
'only' => ['store', 'update', 'destroy']
])->middleware('permission:create users');

我的问题是: $producerType变量没有捕获必要的属性。

最佳答案

虽然上面的答案给出了问题的解决方案,但我最终在我的代码中发现了问题!

来自 Route Model Binding 的 Laravel 文档:

Since the $user variable is type-hinted as the App\User Eloquent model and the variable name matches the {user} URI segment, Laravel will automatically inject the model instance that has an ID matching the corresponding value from the request URI. If a matching model instance is not found in the database, a 404 HTTP response will automatically be generated.



这是我的问题:我正在使用 $producerType变量和 Laravel 期待 $producers_type因为我的路线 producers-type/{producers_type} .

关于php - Laravel 路由模型绑定(bind) : destroy method not grab necessary info,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54315116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com