Laravel 保存显示为真,但数据库中没有任何内容

Laravel save shows true, but theres nothing in database

我知道这个问题被重复了,但尝试了这些问题的几个答案对我没有任何帮助。

简而言之,我有这个存储方法:

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');
}

执行上面的 dd 显示正确,但进入我的数据库并执行 select * 表示它是一个空集。

这是我的 CartProd 模型:

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

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');
}
}

并且只是因为它可能有用,所以这些是 table 的迁移命令(它们在它们自己的迁移文件中,所以那里没有问题):

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');
});

在搜索答案后,我发现了其他几个对我没有帮助的答案,例如 $primaryKey 与数据库中的名称匹配(确实如此),或者其他不适合我的模型的答案。非常感谢您的帮助!

感谢@Lyzvaleska 的建议!事实证明 Hydrate 函数是问题所在,所以我所要做的就是替换它并用这样的 foreach 创建每个 cartprod:

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');
}