For this task we need to implement one To Many relationship.
In your Checklist model you need to define
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Checklist extends Model
{
public function alats()
{
return $this->hasMany('App\Alat');
}
}
which means CheckList
has many Alat
models.
Now,in your Alat model you need to define
namespace App;
use Illuminate\Database\Eloquent\Model;
class Alat extends Model
{
public function checklist()
{
return $this->belongsTo('App\Checklist');
}
}
which means Alat
has a relationship with CheckList
model.
Usage:To save multiple records, need to use saveMany()
$alats = App\Checklist::where('column_name_of_checklists_table', 'IDC001')->first()->alats;
$alats->saveMany([
new App\Alat(['column_name_of_alats_table' => 'IDA001']),
new App\Alat(['column_name_of_alats_table' => 'ID0002']),
new App\Alat(['column_name_of_alats_table' => 'ID0003']),
]);