user11710915
user11710915

Reputation: 434

Displaying liked products from database laravel

I want to display all liked products of a specific user from the database, but I'm getting an error Integrity constraint violation: 1052 Column 'deleted_at' in where clause is ambiguous .How can I display all liked products of specific user?

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);
}

Blade file

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

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

@endforeach

Upvotes: 1

Views: 130

Answers (1)

sh1hab
sh1hab

Reputation: 670

You have two deleted_at column in users table and products table. That's why the error . change your likedProducts function like this

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

Upvotes: 1

Related Questions