nested hasmany relationship in laraval-admin

670 Views Asked by At

I am stuck in a nested hasmany relation. Here is a scenario where there are no units entered separately by the user and milestones in this form must be equal to the number of units and each unit can have multiple milestones. I was trying to loop but it is not working.

for($i = 1; $i <= $noOfUnit; $i++){
    $form->hasmany('majorMilestone', 'Unit '.$i , function (Form\NestedForm $form) use($visitId, $projectId, $ps2EditId, $i) {
        $form->hidden('project_id', 'ProjectId')->default($projectId);
        $form->text('major_milestone', "Major Milestone")->prepend(false);
        $form->date('scheduled', "Scheduled");
    })->setWidth(9, 3);
}

more clarification

for example I have a text input on page one, where user add number of units. On second page I need to capture the completion date an remark in steps for each unit. Then I have looped the number of unit to get the below structure enter image description here

and I have below model in place to add/modify data

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

class Step2 extends Model
{

    protected $table = 'proj_clearence_bckwrd_frwrd_supply';
    public $timestamps = false;

    public function majormilestone1()
    {
        return $this->hasMany(Majormilestone::class,'edit_id')->where('unit_no', '1');
    }
    public function majormilestone2()
    {
        return $this->hasMany(Majormilestone::class,'edit_id')->where('unit_no', '2');
    }
    public function majormilestone3()
    {
        return $this->hasMany(Majormilestone::class,'edit_id')->where('unit_no', '3');
    }
}

but this code is limited to three units only. I need to remove this dependency. I hope this is clear now.

here is the code for MajorMilestone model.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Majormilestone extends Model
{
    protected $table = 'major_milestone';
    protected $fillable = ['project_id', 'edit_id', 'visit_id', 'unit_no',  'major_milestone', 'scheduled', 'anticipated', 'actual', 'remarks']; 
    public $timestamps = false;

    public function step2()
    {
        return $this->belongsTo(Step2::class,'edit_id');
   }
}
0

There are 0 best solutions below