Laravelshoppingcart: Model association not working as expected

Created on 1 May 2019  路  14Comments  路  Source: Crinsane/LaravelShoppingcart

2019-05-01 (1)
2019-05-01 (2)
2019-05-01 (3)
2019-05-01 (4)
2019-05-01 (5)
2019-05-01
Using laravel 5.8
Please help

Most helpful comment

May be you are using find method or getting a relation from a model like $item->model .... they are automatically search by (id) column but your primary key is product_id .... In my mind you can
1- Change your primary key in your product model protected $primaryKey = 'product_id';
2- Change your primary key in products migration table

All 14 comments

May be you are using find method or getting a relation from a model like $item->model .... they are automatically search by (id) column but your primary key is product_id .... In my mind you can
1- Change your primary key in your product model protected $primaryKey = 'product_id';
2- Change your primary key in products migration table

@taha7
Thank's man
I try That but it gives me a new error
2019-05-01 (7)
2019-05-01 (8)

Please keep this in mind (as already mentioned) :
https://laravel.com/docs/5.8/eloquent#eloquent-model-conventions

@bumbummen99 I hope if I can understand you or understand what to do?

@Power50015 I just left the link to the eloquent model conventions. By looking at your migration i can see that you are not holding to the default naming by eloquent, therefore you have to overwrite them manually, as @taha7 mentioned. Hope it helps in the future 馃憤

As for your second issue, are you sure that product_name is not null for this instance?

@bumbummen99 yes my friend it isn't null.

@Power50015 Ahh, i think i have found the answer ^^
https://github.com/Crinsane/LaravelShoppingcart#models
It says that the Model has to implement the Buyable interface for the association to work.

if ($id instanceof Buyable) {
    $cartItem = CartItem::fromBuyable($id, $qty ?: []);
    $cartItem->setQuantity($name ?: 1);
    $cartItem->associate($id);
} elseif (is_array($id)) {

https://github.com/Crinsane/LaravelShoppingcart/blob/f460ab7312cce32cb428cf39a500a2d16600b1d6/src/Cart.php#L452

@taha7 @bumbummen99
Can you please take a look at my Code and Tell me exactly what I need to do because of I completely new in larval and in need some help, please
https://github.com/Power50015/fup

Hehe i think i won't need the whole code ^^ I think this Product model should do the trick:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Gloudemans\Shoppingcart\Contracts\Buyable;

class Product extends Model implements Buyable
{
    public function presentPrice()
    {
        $price = $this->product_price;
        return "$" . $price;
    }

    /**
     * Get the identifier of the Buyable item.
     *
     * @return int|string
     */
    public function getBuyableIdentifier($options = null) {
        return $this->product_id;
    }

    /**
     * Get the description or title of the Buyable item.
     *
     * @return string
     */
    public function getBuyableDescription($options = null) {
        return $this->product_name;
    }

    /**
     * Get the price of the Buyable item.
     *
     * @return float
     */
    public function getBuyablePrice($options = null) {
        return $this->product_price;
    }
}

@bumbummen99
Why Code hate me so much
2019-05-02

Don't worry you are close 馃憤 I tought you are using the official version of the package. Assuming you are using my fork you have to also define a getter method for the product weight. If you do not need any weight calculations (for shipping) you can just 0 everything as it will not affect the other calculations.

/**
 * Get the weight of the Buyable item.
 *
 * @return float
 */
public function getBuyableWeight($options = null) {
    return 0; //return $this->weight;
}

By writing class Product extends Model implements Buyable you say that your Product implements the Buyable Interface (It's called a Contract in Laravel) and it's abstract methods. Also if you do not already have one i suggest to install an extension for intellisense and php, this has saved me hours already.

@Power50015 Did you solve the problem ?

first of all, I already use a bumbummen99 fork.
I fix it by only Two lines of code in product model
protected $primaryKey = 'product_id'; public $incrementing = false;
Now I have a
2019-05-03 (1)
2019-05-03

compleatly new error when I try to remove.

actually I fix it too by this line of code
<form action="{{ route('cart.destroy', $item->rowId)}}" method="POST">
thanks guys

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hatamiarash7 picture hatamiarash7  路  6Comments

matbeard picture matbeard  路  7Comments

hasanali05 picture hasanali05  路  6Comments

ChrisThompsonTLDR picture ChrisThompsonTLDR  路  5Comments

sobhanattar picture sobhanattar  路  4Comments