Selva
Selva

Reputation: 33

Controller to view data cannot pass

This is my controller code:

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

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

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

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

$usermobile = user::where('businessid', $businessid)->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)

Upvotes: 1

Views: 49

Answers (2)

Irshad Khan
Irshad Khan

Reputation: 432

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.

Upvotes: -1

Drk
Drk

Reputation: 435

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

{{$username}}

Upvotes: 2

Related Questions