Kaustubh's avatar

Cant Foreach array in Blade

how i can send null data or default value of data from controller here is my code Controller

public function recruiterDet()
{
    $auth = Auth::User()->id;
    $miRec = Recruiters::where('userid',$auth)->first();

    if(!empty($miRec))
    {
        return view('RcDetailMainpage',compact('miRec'));
    }

here i m try to fill some default value so i can foreach it in blade

    else{
        $miRec = collect([['cmpname'=>NULL,'Address'=>NULL],]);
        return view('RcDetailMainpage',compact('miRec'));
    }
}

Blade View

@foreach($miRec as $a)
<input type="text" class="form-control" id="cmpname" name="cmpname" value="{{ $a->cmpname }}">
<input type="text" class="form-control" id="Address" name="Address" value="{{ $a->Address }}">
<input type="submit" value="Update">
@endforeach 

i don't want to use if else condition in blade because i have lots of form if i use if else then it will increase 2 time in size in blade

0 likes
2 replies
LiamHammett's avatar

When using the compact() function you don't need to pass through the dollar symbol ($) too — it's possible your data isn't getting through to the view at all.

Remove the dollar symbol:

compact('miRec')

1 like
aricci's avatar

Why are you using a foreach in your blade when you are only returning one result [->first()]? Does the above not work for you? You could simplify the it as such:

$miRec = Recruiters::where('userid',$auth)->first();

if(empty($miRec)) 
{
     $miRec = collect([['cmpname'=>NULL,'Address'=>NULL],]);   
}

 return view('RcDetailMainpage',compact('miRec'));

...And leave out the @foreach in blade, unless you intended on returning more than one result.

Please or to participate in this conversation.