Crinsane / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
3.67k stars 1.73k forks source link

Model association not working as expected #559

Closed Power50015 closed 5 years ago

Power50015 commented 5 years ago

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

taha7 commented 5 years ago

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

Power50015 commented 5 years ago

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

bumbummen99 commented 5 years ago

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

Power50015 commented 5 years ago

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

bumbummen99 commented 5 years ago

@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?

Power50015 commented 5 years ago

@bumbummen99 yes my friend it isn't null.

bumbummen99 commented 5 years ago

@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

Power50015 commented 5 years ago

@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

bumbummen99 commented 5 years ago

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;
    }
}
Power50015 commented 5 years ago

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

bumbummen99 commented 5 years ago

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.

taha7 commented 5 years ago

@Power50015 Did you solve the problem ?

Power50015 commented 5 years ago

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.

Power50015 commented 5 years ago

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