我想在保存数据的同时将其他数据库的主键保存到其外键-Laravel

问题描述 投票:0回答:1

我有一个发票表,它的主键是 id,对于发票详细信息,外键是invoice_id。我想要的是在保存发票上的主键的同时,它还会将主键保存在发票详细信息的外键上。

这是我的商店代码:

public function store(Request $request)
    {
       
     
       $validation = $request->validate([
        'user' => 'required',
        'company' => 'required',
        'invoice_month' => 'required',
       ]);
        Invoice::create($validation);
    
        $title = $request->title;
        $quantity = $request->quantity;
        $unit_price = $request->unit_price;
        $sort_order = $request->sort_order;

        $now = Carbon::now();

        for($i=0; $i < count($title); $i++){
            $datasave = [
                'sort_order' => $sort_order[$i],
                'title' => $title[$i],
                'quantity' => $quantity[$i],
                'unit_price' => $unit_price[$i],
                'created' => $now,
                'modified' => $now,

            ];

           
            DB::table('invoice_details')->insert($datasave);

        }

        return redirect('invoice')->with('flash_message', 'Saved');
        
    }

对于型号:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Invoice_detail extends Model
{
    use HasFactory;
    protected $table = 'invoice_details';
    protected $primaryKey = 'id';
    protected $fillable = ['title', 'quantity', 'unit_price'];
    

    public function invoice(){
        return $this->belongsTo(\App\Invoice::class, 'invoice_id');
    }

}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Invoice extends Model
{
    use HasFactory;
    protected $table = 'invoices';
    protected $primaryKey = 'id';
    protected $fillable = ['user', 'company', 'invoice_month'];
    
    const UPDATED_AT = 'modified';
    const CREATED_AT = 'created';
    
    public function invoice_detail(){
        return $this->hasOne(\App\Invoice_detail::class);

    }

}

路线


Route::get('/home', function(){
    return view('homepage');

});

//Route::resource('invoice', InvoiceController::class);

Route::get('invoice', [InvoiceController::class, 'index']);

Route::get('search', [InvoiceController::class, 'search']);

Route::resource('/invoic', InvoiceController::class);

任何帮助或想法,我将不胜感激。我想要发生的事情是,当您输入所有字段时,主键也将保存在发票详细信息的外键上。

laravel sqlite foreign-keys primary-key
1个回答
0
投票

您可以拥有许多发票详细信息,并且您已经编写了一种关系,并且您正在保存许多记录。

另一方面,您可以使用关系来保存相关数据,也可以手动完成。

首先在发票模型中更改您的关系。

public function invoice_detail(){
    return $this->hasMany(\App\Invoice_detail::class, 'invoice_id');
}

然后在 Invoice_detail 中也在可填写数组中提供发票_id。

protected $fillable = ['title', 'quantity', 'unit_price', 'invoice_id'];

然后您可以这样做来保存相关数据。

$invoice = Invoice::create($validation);

$title = $request->title;
$quantity = $request->quantity;
$unit_price = $request->unit_price;
$sort_order = $request->sort_order;

$now = now();

$invoiceDetails = [];

for ($i = 0; $i < count($title); $i++) {
    $invoiceDetails[] = [
        'sort_order' => $sort_order[$i],
        'title' => $title[$i],
        'quantity' => $quantity[$i],
        'unit_price' => $unit_price[$i],
    ];
}

$invoice->invoice_detail()->createMany($invoiceDetails);
© www.soinside.com 2019 - 2024. All rights reserved.