【发布时间】:2019-10-27 13:55:02
【问题描述】:
我希望用户在单击不同按钮时删除产品,但我收到错误404 url not found,但我有 url。
如果我把dd($product) 放在$like = Like::findOrFail($product); 之前
它显示id(4),但如果我输入dd($like),则会引发错误404。我怎样才能使这个功能起作用?。
控制器
public function destroy($product)
{
$like = Like::findOrFail($product);
dd($like);
$like->delete();
return 'done';
}
刀片
<a class="remove" href="{{ route('product.unlike', ['product' => $product->id]) }}" > Unlike </a>
路线
Route::get('product/{product}/unlike', ['as' => 'product.unlike', 'uses' => 'LikeController@destroy']);
点赞.php
class Like extends Model
{
use SoftDeletes;
protected $table = 'likeables';
protected $fillable = [
'user_id',
'product_id',
'likeable_id',
'likeable_type',
];
public function products()
{
return $this->morphedByMany('App\Product', 'likeable');
}
【问题讨论】:
-
数据库中似乎没有具有给定 id 的产品。
-
@PaulSpiegel 数据库中有一个产品(4)
-
查看
Like模型中配置的表格! -
是的,我看到了 id 4 @PaulSpiegel
-
如果你
dd($product)会有什么结果?
标签: php mysql laravel laravel-5