Friday, March 10, 2017

laravel 5.4 How do I get current user through User model? and Auth::user() dosen't work

I'm trying to get the current user through User model but it returns null.

$user = new User;
//$user = Auth::user();
$product = new Product;

$product->user_id = $user->user_id;
$product->save();

This is what I get Integrity constraint violation: 1048 Column 'user_id' cannot be null

When I tried to get it with Auth::user(), it gives same error.

What's wrong with this code?



via ishan shah

Advertisement