1

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)

3
  • return view(''bunk.cashier'', compact('datas')); Don't use $ sign here. Commented Oct 20, 2021 at 11:14
  • Did you tried, to print $datas, I mean like print_r($datas), to check the variable? Commented Oct 20, 2021 at 11:17
  • 1
    {{ $data->$username }} should be {{ $data->username }} (without the $). Same for the others in your view file Commented Oct 20, 2021 at 11:31

2 Answers 2

2

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

{{$username}}
Sign up to request clarification or add additional context in comments.

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.