显示来自数据库laravel的喜欢的产品

问题描述 投票:1回答:1

我想从数据库中显示特定用户的所有喜欢的产品,但出现错误Integrity constraint violation: 1052 Column 'deleted_at' in where clause is ambiguous。如何显示特定用户的所有喜欢的产品?

Like.php

class Like extends Model
{
use SoftDeletes;

protected $table = 'likeables';

protected $fillable = [
    'user_id',
    'likeable_id',
    'likeable_type',
];

/**
 * Get all of the products that are assigned this like.
 */
public function products()
{
    return $this->morphedByMany('App\Product', 'likeable');
}

}

Product.php

   public function likes()
{
    return $this->morphToMany('App\User', 'likeable')->whereDeletedAt(null);
}

public function getIsLikedAttribute()
{
    $like = $this->likes()->whereUserId(Auth::id())->first();
    return (!is_null($like)) ? true : false;
}

User.php

public function likedProducts()
{
return $this->morphedByMany('App\Product', 'likeable')->whereDeletedAt(null);
}

刀片文件

@foreach (Auth::user()->likedProducts as $product)

<h2>{{ $product->price }}</h2>

@endforeach
php mysql laravel laravel-5 eloquent
1个回答
0
投票
您在用户表和产品表中有两个deleted_at列。这就是错误的原因。像这样更改likedProducts函数

public function likedProducts() { return $this->morphedByMany('App\Product', 'likeable')->whereNull('products.deleted_at'); }

© www.soinside.com 2019 - 2024. All rights reserved.