【发布时间】:2019-11-01 12:18:56
【问题描述】:
我推荐了包含 product_id、user_id 和 deleted_at 列的表,我想在视图中显示查看次数最多的产品,它工作正常,但问题是当用户删除产品时出现错误即使产品被删除,推荐表中的trying to get property of non object 和列deleted_at 仍显示为空。删除产品后如何使产品在推荐表中消失,并且视图应仅显示未删除的产品?
控制器
$recommends = Recommends::with('product')
->select('product_id', DB::raw('COUNT(*) AS total'))
->whereNotNull('product_id')
->groupBy('product_id')
->orderby('total', 'DESC')
->take(12)
->get();
推荐.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Recommends extends Model
{
public function product()
{
return $this->belongsTo('App\Product','product_id')
}
}
产品.php
public function recommends()
{
return $this->hasMany('App\Recommends','product_id');
}
【问题讨论】:
-
你在使用
softdelete吗?如果是,请显示您的 Product.php 型号代码并删除代码? -
不,我只有 deleted_at 列@DilipHirapara
-
你在表中使用关系吗?
-
是的,我在表@YasinPatel 中使用了关系
-
所以我们删除了产品,它没有从推荐表中删除?