Home > Back-end >  Controller to view data cannot pass
Controller to view data cannot pass

Time:10-21

This is my controller code:

$id = Auth::user()->id;

$bussinessid = Bunk::where('bunkvendorid', $id)->first()->id;

$username = user::where('bussinessid', $bussinessid)->first()->name;

$useremail = user::where('bussinessid', $bussinessid)->first()->email;

$usermobile = user::where('bussinessid', $bussinessid)->first()->mobile;

$datas = [
    'username' => $username, 'useremail' => $useremail, 'usermobile' => $usermobile
];

return view('bunk.cashier')->with($datas);

This is my view file code:

@foreach ($datas as $data)
    <tr>
        <td>{{ $data->$username }}</td>
        
        <td>{{ $data->$useremail }}</td>
        
        <td>{{ $data->$usermobile }}</td>
    </tr>
@endforeach

I am getting error

Undefined variable $datas (View: C:\Users\Gowtham\Desktop\blog2\resources\views\bunk\cashier.blade.php)

CodePudding user response:

You don't need the $datas variable in your view. Simply access the variables in the $datas array like so:

{{$username}}

CodePudding user response:

You are sending an aray wth key values paires mean $datas contain keys like username useremail etc. When you apply loop on this then in $data variable your keys vlaues exist. You can simply use like this,

@foreach ($datas as $data)
<tr>
    <td>{{ $data }}</td>
    
    <td>{{ $data }}</td>
    
    <td>{{ $data }}</td>
</tr>
@endforeach

In first iteration data contain username then useremail, and phone soon.

  • Related