【问题标题】:Displaying liked products from database laravel显示来自数据库 laravel 的喜欢的产品
【发布时间】:2019-10-27 09:26:16
【问题描述】:

我想显示数据库中特定用户的所有喜欢的产品,但我收到错误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);
}

刀片文件

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

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

@endforeach

【问题讨论】:

标签: php mysql laravel laravel-5 eloquent


【解决方案1】:

您在 users 表和 products 表中有两个 deleted_at 列。这就是错误的原因。像这样改变你的likedProducts函数

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

【讨论】:

  • 我收到一个错误Unknown column 'product.deleted_at'@shihab
  • 这也说Unknown column 'user.deleted_at'@shihab
  • @user11710915 你有任何表的deleted_at 的列名吗
  • 在产品表中我有deleted_at 列,但在用户中我没有deleted_at 列@shihab
  • @user11710915 products 表的名称是产品还是产品?
猜你喜欢
  • 1970-01-01
  • 2022-08-16
  • 1970-01-01
  • 1970-01-01
  • 2020-05-30
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多