简体   繁体   English

如何仅检索 Laravel 中未从数据库中删除的记录?

[英]How to retrieve only records that are not deleted from database in Laravel?

I have recommends table that has product_id , user_id and deleted_at columns and I want to display the most viewed products in the view and it works fine but the problems is when user deletes the product I get an error trying to get property of non object and the column deleted_at in recommends table still showing null even if the product is deleted.我推荐了具有product_iduser_iddeleted_at列的表,我想在视图中显示查看次数最多的产品,它工作正常,但问题是当用户删除产品时,我在trying to get property of non object和即使产品被删除,推荐表中的deleted_at列仍显示 null。 How can I make the product disappear in recommends table after the product has been deleted and the view should display only the products which are not deleted?删除产品后,如何使产品在推荐表中消失,并且视图应仅显示未删除的产品?

Controller Controller

  $recommends = Recommends::with('product')
    ->select('product_id', DB::raw('COUNT(*) AS total'))
    ->whereNotNull('product_id')
    ->groupBy('product_id')
    ->orderby('total', 'DESC')
    ->take(12)
    ->get();

Recommends.php推荐.php

 <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Recommends extends Model
{
public function product()
{
    return $this->belongsTo('App\Product','product_id')
}
}

Product.php产品.php

 public function recommends()
 {
    return $this->hasMany('App\Recommends','product_id');
 }

Try with wherehas() it will check that product exist or not:尝试使用wherehas()它将检查产品是否存在:

$recommends = Recommends::with('product')
    ->select('product_id', DB::raw('COUNT(*) AS total'))
    ->whereNotNull('product_id')
    ->wherehas('product')
    ->groupBy('product_id')
    ->orderby('total', 'DESC')
    ->take(12)
    ->get();

And also on delete product remove all recommendations for that product.并且在删除产品时删除该产品的所有推荐。

Change in Product model,产品变更 model,

protected static function boot() {
   parent::boot();

   static::deleting(function($product) {
     $product->recommends()->delete();
   });
}

When soft deleting a model, it is not actually removed from your database.当软删除 model 时,它实际上并没有从您的数据库中删除。 Instead, a deleted_at timestamp is set on the record.相反,在记录上设置了deleted_at时间戳。 To enable soft deletes for a model, apply the SoftDeletes to the model:要为 model 启用软删除,请将SoftDeletes应用于 model:

use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model {

    use SoftDeletes;

    protected $dates = ['deleted_at'];

}

Now, when you call the delete method on the model, the deleted_at column will be set to the current timestamp.现在,当您在 model 上调用delete方法时, deleted_at列将设置为当前时间戳。 When querying a model that uses soft deletes, the "deleted" models will not be included in query results.查询使用软删除的 model 时,“已删除”模型不会包含在查询结果中。

Please check more info of soft delete请查看软删除的更多信息

After soft delete if you run this query the product will not come which is soft-deleted.软删除后,如果您运行此查询,将不会出现软删除的产品。

 $recommends = Recommends::with('product')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM