Controller:
if(!in_array($request->id, $rowIds)) {
$product = Product::find($request->id);
$cartItem = Cart::add($product->id, $product->name, 1, $product->price)->associate($product);
$request->session()->flash('add_success','Product added to your cart');
return redirect()->back();
}`
View:
@foreach(Cart::content() as $product)
<tr>
<td>{{ $product->name }}</td>
<td>{{ $product->model->excerpt }}</td>
<td>${{ number_format($product->price ,'2','.',',') }}</td>
<td>`
results in:
ErrorException in CartItem.php line 274:
Class 'Product' not found
@pelachile What version of the library are you using?
I am having the exact same issue, using the most recent version.
Cart::add($product->sku, $product->name, $qty, $product->price)->associate('AD\Modules\Dashboard\Models\Product');
In a foreach in my view, dd($product->model) returns null.
Where does the $product variable come from when you do dd($product->model)?
@Crinsane The $product variable is off a foreach of Cart::content() within my view.
Ah ok, was wondering if it was a Product model or something. ;)
I'm trying to reproduce this, but can't seem to...
// CartController
public function index()
{
$content = $this->cart->content();
return view('cart.index', compact('content'));
}
public function store()
{
$product = Product::first();
$this->cart->add($product->id, $product->title, 1, $product->price)->associate($product);
return redirect()->to('/cart');
}
// Blade view
@foreach ($content as $row)
{{ var_dump($row->model) }}
@endforeach
// Result
/Users/robgloudemans/Sites/tempshop/storage/framework/views/d32565b479fd7c3109b6238ab9a8d9115e054634.php:12:
object(App\Product)[183]
protected 'connection' => null
protected 'table' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'keyType' => string 'int' (length=3)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=5)
'id' => int 1
'title' => string 'Sit' (length=3)
'price' => string '54.95' (length=5)
'created_at' => string '2016-10-13 14:43:30' (length=19)
'updated_at' => string '2016-10-13 14:43:30' (length=19)
protected 'original' =>
array (size=5)
'id' => int 1
'title' => string 'Sit' (length=3)
'price' => string '54.95' (length=5)
'created_at' => string '2016-10-13 14:43:30' (length=19)
'updated_at' => string '2016-10-13 14:43:30' (length=19)
protected 'relations' =>
array (size=0)
empty
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'appends' =>
array (size=0)
empty
protected 'fillable' =>
array (size=0)
empty
protected 'guarded' =>
array (size=1)
0 => string '*' (length=1)
protected 'dates' =>
array (size=0)
empty
protected 'dateFormat' => null
protected 'casts' =>
array (size=0)
empty
protected 'touches' =>
array (size=0)
empty
protected 'observables' =>
array (size=0)
empty
protected 'with' =>
array (size=0)
empty
public 'exists' => boolean true
public 'wasRecentlyCreated' => boolean false
So seems to work perfectly @mbsmi Are you sure the ID you specify on the first parameter of the add() method is the actual primary key of the Product model? Because that's what the package will use.
Are you sure the ID you specify on the first parameter of the add() method is the actual primary key of the Product model?
@Crinsane Hate to say it, I wasn't. I did not think about the package needing it for the model. Thanks for your assistance on this, everything is working now.
Might be interesting to add something so one can specify the way a model is fetched. But that's for the future. Glad it is fixed!
"Might be interesting to add something so one can specify the way a model is fetched. But that's for the future. Glad it is fixed!'
Did this ever get added?
I'm having a problem where I'm coming into a project late and don't want to change too much in case I break something! A column called 'code' is being added instead of the 'id' from the Product model. Is there a way of using another column other than 'id'?