php - 从数据库 laravel 中显示喜欢的产品

标签 php mysql laravel laravel-5 eloquent

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

点赞.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');
}

}

产品.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;
}

用户.php

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

Blade 文件

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

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

@endforeach

最佳答案

用户表和产品表中有两个 deleted_at 列。这就是错误的原因。像这样更改您的 likedProducts 函数

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

关于php - 从数据库 laravel 中显示喜欢的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58578096/

相关文章:

php - 显示同一 div 内表中的所有相等值

php - 如何使用 php 和 mysql 在表中插入新列?

Laravel session 变量一旦重定向到支付网关并返回,就会变为 Null

php - 当主数据库关闭时连接到备用数据库?

php - 查询数据库以获取时间戳相同且电子邮件地址相同的多行

laravel - 如何检查 Laravel 队列中是否有作业?

拉拉维尔 4 : Failed to open stream: Permission denied

php - Zend Framework 2 - 表单元素装饰器

PHP 事件日历。 javascript按钮不会按下

php - htaccess 重定向 vs php 重定向