0

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');
   }
}
AG_
  • 2,589
  • 3
  • 20
  • 32
  • 1
    I think your question is a little sparse with what you are trying to achieve, i have time understanding what the issue is and what you want to obtain. – mrhn Oct 24 '20 at 11:33
  • Please try to explain your problem clearly. It would be interesting to explain the context and the entities. – Alexandre Strapacao G. Vianna Oct 26 '20 at 11:55
  • thanks you for showing your interest, I have added more clarification. do let me know if you need more clarification. – AG_ Oct 26 '20 at 12:41
  • Please post you Majormilestone code and your form code. From what I can gather, it looks like your data model is off. Does Majormilestone have multiple Units? What is the base model that Majormilestone's are related to? – whoacowboy Oct 27 '20 at 17:11
  • @whoacowboy I have added the Majormilestone code, the number units are user defined and each unit can have multiple milestone. in above code if user enter 4 in unit it will break. – AG_ Oct 29 '20 at 05:15

0 Answers0