I know this question is repeated, but trying out several of the answers of those questions has done nothing for me.
In short, I have this store method:
public function store(Request $request)
{
$selected_products = json_decode($request->selectedproducts);
$cart = new Cart();
$cartprods = CartProd::hydrate( $selected_products );
// This sums all the end costs to get a total cost
// And saves the cart so that its id is not null
$final_cost = 0;
foreach ($cartprods as $prod) {
$final_cost += $prod->cost;
}
$cart->cost = $final_cost;
$cart->user_id = Auth::user()->id;
$cart->save();
foreach ($cartprods as $prod) {
$prod->cart_id = $cart->id;
$og_product = Product::FindOrFail($prod->product_id);
$og_product->amount -= $prod->amount;
$og_product->save();
$prod->save();
dd($prod->save());
}
return redirect()->route('cart');
}
Doing the dd above shows true, but going into my database and doing select * says that its an empty set.
Here is my CartProd model:
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class CartProd extends Model
{
use HasFactory;
protected $table = 'cartprod';
protected $primaryKey = 'id';
protected $fillable = [
'unit_price',
'amount',
'discount',
'cost',
'cart_id',
'product_id',
];
public function cart()
{
return $this->belongsTo(Cart::class, 'cart_id');
}
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
}
And just because it might be useful, these are the commands for the migrations of the table (they’re in their own migration file, so there’s no issue there):
Schema::create('cartprod', function (Blueprint $table) {
$table->id();
$table->decimal('unit_price',9,2);
$table->integer('amount');
$table->integer('discount');
$table->decimal('total_cost',9,2);
$table->timestamps();
});
Schema::table('cartprod', function (Blueprint $table) {
$table->foreignId('cart_id')->references('id')->on('cart');
});
Schema::table('cartprod', function (Blueprint $table) {
$table->foreignId('product_id')->references('id')->on('product');
});
Having searched for the answer I found several others that didn’t helped me, like the $primaryKey matching the name on the database (it does), or others that didn’t fit my model. Thanks a lot for your help!
Advertisement
Answer
Thanks to @Lyzvaleska for the suggestion! Turns out the Hydrate function was the problem, so all I had to do is replace it and create each cartprod with a foreach as such:
public function store(Request $request)
{
$selected_products = json_decode($request->selectedproducts);
$cartprods = array();
$final_cost = 0;
foreach ($selected_products as $prod) {
$final_cost += $prod->total_cost;
$selProd = new CartProd();
$selProd->unit_price = $prod->unit_price;
$selProd->amount = $prod->amount;
$selProd->discount = $prod->discount;
$selProd->total_cost = $prod->total_cost;
$selProd->cart_id = $prod->cart_id;
$selProd->product_id = $prod->product_id;
array_push($cartprods, $selProd);
}
$cart = new Cart();
$cart->cost = $final_cost;
$cart->user_id = Auth::user()->id;
$cart->save();
foreach ($cartprods as $prod) {
$prod->cart_id = $cart->id;
$og_product = Product::FindOrFail($prod->product_id);
$og_product->amount -= $prod->amount;
$og_product->save();
$prod->save();
}
return redirect()->route('cart');
}