amsgames / laravel-shop

Laravel shop package
MIT License
483 stars 166 forks source link

$cart = Cart::current(); doesn't work for me in L5.2 #31

Closed ITwrx closed 8 years ago

ITwrx commented 8 years ago

with logged in user $cart = Cart::current(); in my controller causes

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into `cart` (`user_id`, `updated_at`, `created_at`) values (, 2016-01-14 16:17:00, 2016-01-14 16:17:00))

and further down

at call_user_func('App\Models\Cart::create', array('user_id' => null)) in ShopCartTrait.php line 225

i also notice

 public function user()
    {
        return $this->belongsTo(Config::get('auth.model'), 'user_id');
    }

in ShopCartTrait.php

but auth.model is different in L5.2's config/auth.php. Maybe the $user problems start there?

Thanks

ghost commented 8 years ago

if(Auth::user()) { $shopItem = ShopItem::find(1); $cart =Cart::current(); $cart->add($shopItem); }

ITwrx commented 8 years ago

i've updated my issue to indicate that my user is logged in. it still happens with or without the auth check, as well, as the user is logged in. thanks

deepmk commented 8 years ago

solution of the problem

Config::get('auth.model') replace Config::get('auth.providers.users.model') laravel 5.2 config('auth.providers.users.model')

fwartner commented 8 years ago

You can simply add 'model' => App\User::class, to your auth config.

ITwrx commented 8 years ago

@wildturk I tried replacing Config::get('auth.model') with Config::get('auth.providers.users.model') in ShopCartTrait.php but that didn't do the trick. am i misunderstanding you or is there somewhere else?

@fwartner I already had:'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ], in my Auth.php

Thanks for the input.

fwartner commented 8 years ago

Doesn´t matter.. You need to use the old one additionally..

Am 30.01.2016 um 15:36 schrieb ITwrx notifications@github.com:

@wildturk https://github.com/wildturk I tried replacing Config::get('auth.model') with Config::get('auth.providers.users.model') in ShopCartTrait.php but that didn't do the trick. I i misunderstanding you or is there somewhere else?

@fwartner https://github.com/fwartner I already had:'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ], in my Auth.php

Thanks for the input.

— Reply to this email directly or view it on GitHub https://github.com/amsgames/laravel-shop/issues/31#issuecomment-177193048.

ITwrx commented 8 years ago

@fwartner Thanks, but i don't understand what you're trying to tell me. I currently have 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], ],

what am i supposed to have in Auth.php instead?

fwartner commented 8 years ago

You can simply leave it as it is atm.. But to get the package working, you´ll need to add the model-binding like i mentioned before.. Took me a few hours to figure out, why the package wouldn´t work for me.. ^^ But now it´s working..

Am 30.01.2016 um 19:30 schrieb ITwrx notifications@github.com:

@fwartner https://github.com/fwartner Thanks, but i don't understand what you're trying to tell me. I currently have 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], ],

what am i supposed to have in Auth.php instead?

— Reply to this email directly or view it on GitHub https://github.com/amsgames/laravel-shop/issues/31#issuecomment-177268159.

ITwrx commented 8 years ago

i finally got it, thanks. my Auth.php needed to be 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], ], because that's where my user model is. I thought i had tried that already but i guess not. :)

fwartner commented 8 years ago

Glad to see.. :)

Am 30.01.2016 um 20:04 schrieb ITwrx notifications@github.com:

i finally got it, thanks. my Auth.php needed to be 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], ], because that's where my user model is. I thought i had tried that alreadyt but i guess not. :)

— Reply to this email directly or view it on GitHub https://github.com/amsgames/laravel-shop/issues/31#issuecomment-177278021.

fwartner commented 8 years ago

There´s one strongly recommended point for this package that NEED to do asap! The ability to store cart-items as a guest.. One of my clients need it in his project..

I guess it could be done using the session-helpers.. But i dunno how to re-use the session in case a user is going to sign up and store his cart in the database.. :/

Am 30.01.2016 um 20:04 schrieb ITwrx notifications@github.com:

i finally got it, thanks. my Auth.php needed to be 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], ], because that's where my user model is. I thought i had tried that alreadyt but i guess not. :)

— Reply to this email directly or view it on GitHub https://github.com/amsgames/laravel-shop/issues/31#issuecomment-177278021.

manoj-nandakumar commented 8 years ago

The Documentation says that the guest cart is in the Horizon... :+1: lets see..

ITwrx commented 8 years ago

@fwartner yeah, good luck with it. I haven't even looked at it as i don't need guest cart right now. If it's for a new project you might find a suitable alternative cart package as there's been several laravel compatible cart projects show up on the scene lately.

ITwrx commented 8 years ago

update: The config change mentioned above allowed Cart::current() to work but i later had trouble adding an item to cart. Changing return $this->belongsTo(Config::get('auth.model'), 'user_id'); to return $this->belongsTo(config('auth.providers.users.model')); on line 56 in vendor/amsgames/laravel-shop/src/Traits/ShopCartTrait.php did the trick as widlturk suggested. Thanks