0

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.wishlist_id' in 'where clause' (SQL: select * from products where products.wishlist_id = 1 and products.wishlist_id is not null and products.deleted_at is null)

In my controller >>>

public function index() {
    $wishlist_items = Wishlist::where('user_id', Auth::user()->id)->get();
    foreach($wishlist_items as $item) {
        dd($item->getProducts()->get());
    }
}

Wishlist.php >>>

public function getProducts() {
    return $this->hasMany(Product::class);
}

Product.php >>>

public function getWishlist() {
    return $this->belongsTo(Wishlist::class);
}

Migration table >>>

Schema::create('wishlists', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('product_id');
    $table->unsignedBigInteger('user_id');
    $table->timestamps();

    $table->foreign('product_id')->references('id')->on('products');
    $table->foreign('user_id')->references('id')->on('users');
});
  • You could at least explain what is happening here. Pasting only code and error message is a bit rude. – AymDev Jun 04 '21 at 08:35
  • Does this answer your question? [Unknown Column In Where Clause](https://stackoverflow.com/questions/153598/unknown-column-in-where-clause) – AymDev Jun 04 '21 at 08:36
  • @AymDev Yes, I apologize for my unreasonable question coz of my first experience on Stack Overflow. I means this, column not found in Where clause. Could you pls explain me as u can? – Zin Min Htun Jun 04 '21 at 08:44

1 Answers1

1

The hasMany() function queries the table for a {model_name}_id column - In this case, wishlist_id. Your products table is missing a wishlist_id column and the relevant foreign key. I'd suggest taking a look at the Laravel Documentation on one-to-many relationships.

Your migration will need to include updates to the existing products table to add the column and foreign key:

Schema::table('products', function (Blueprint $table) {
    $table->unsignedBigInteger('wishlist_id');
    $table->foreign('wishlist_id')->references('id')->on('wishlists');
});

That said, are you sure you're looking for a one-to-many relationship here? A wishlist can have multiple products, and a product can be in multiple wishlists - A many-to-many relationship may be a better option.

SierraKomodo
  • 365
  • 4
  • 12
  • Very thanks for your answer. As i assume that Wishlist ID-1 can have product(P1) and also Wishlist ID-2 can have product(P2) anyway. But if a user try to add again on P1, that user wont add P1 to Wishlist coz that item already exist in Wishlist. By that way, I assume that a Wishlist can have one product. Could you please explain me, Is that a reasonable solution for me? – Zin Min Htun Jun 04 '21 at 09:04
  • So the many-to-many vs many-to-one issue I'm seeing is this - You have User1 and User2. Both users have a separate wishlist - Wishlist1, Wishlist2. You also have a product Product. User1 adds Product to Wishlist1. User2 also wants to add Product to Wishlist2. With the one-to-many setup, the product cannot be attached to both wishlists - The product can only belong to one. With many-to-many, the product can have multiple different wishlists. – SierraKomodo Jun 04 '21 at 09:09