I am trying to pass 2 different columns from my sql table to my view ,so that i can show them. My controller is

Published 11 months ago by gaan10

I am trying to pass 2 different columns from my sql table to my view ,so that i can show them. My controller is:

$interface = DB::table("users")->distinct()->get(['name']);
$interfac = DB::table("users")->distinct()->get(['email']);
        
        view()->share('users',compact('interface','interfac'));

My view is :

@foreach ($users as $key => $value)
<tr>
        <td>{{ $value->name }}</td>
        <td>{{ $value->email }}</td>
 </tr>

But this is not working while passing the two variables.If i pass one it is working but passing two is not working.thanks.

Best Answer (As Selected By gaan10)
Snapey

Your query makes no sense. You are asking for a list of distinct user names and then a list of distinct email addresses and hoping that they align so that you can then list them out as one list?

Why not;

$users = User::all();
        
view()->share('users',compact('users'));

Please explain why you feel you need to use distinct?

Stratos
$users = User:all();
@foreach ($users as $user)
<tr>
    <td>{{ $user->name }}</td>
    <td>{{ $user->email }}</td>
</tr>
@endforeach 

Your example is not working because you're not passing any users variable. Also why make two queries? Just retrieve all users once.

Snapey
Snapey
11 months ago (998,325 XP)

Your query makes no sense. You are asking for a list of distinct user names and then a list of distinct email addresses and hoping that they align so that you can then list them out as one list?

Why not;

$users = User::all();
        
view()->share('users',compact('users'));

Please explain why you feel you need to use distinct?

gaan10

I have to pass multiple queries,cause different data are needed.How to do that.The answer u provided will anyway work but won't fit my question.

gaan10

actually distinct was just an example .there will be others.actually i want to pass multiple sql queries.I am not sure how to do that

Snapey
Snapey
11 months ago (998,325 XP)

You just assign data to a different variable.

$foo = 1;

$bar = ['make' => 'Porsche', 'model' => 'boxter'];

$users = User::all();

view()->share('users',compact('foo','bar','users'));

now in the view you have variables $foo, $bar, $users

In your case you have $interface and $interfac as separate distinct lists

Note, you may also see this;

view()->share('users')
    ->withFoo($foo)
    ->withBar($bar)
    ->withUsers($users);
gaan10

My controller now:

$interface = DB::table("users")->distinct()->get(['name']); $inter = DB::table("users")->distinct()->get(['email']); $users = User::all();

view()->share('users',compact('interface','inter','users'));

My view now:

@foreach ($users as $key => $value) {{ $value->name }} {{ $value->email }}

Still getting error Undefined property $name

Stratos

Again your queries do not make any sense (to me). Your error is because you need to do $key->name, look up my example.

Snapey
Snapey
11 months ago (998,325 XP)
@foreach ($users as $user) 

   {{ $user->name }} {{ $user->email }}

@endforeach 

do you have a column called name ?

gaan10

yes i have the column name in users table. Thanks

Snapey
Snapey
11 months ago (998,325 XP)

Still getting error Undefined property $name

ok, so we will need to see the full error message and the file that the error is occurring in.

somewhere you are trying to use a variable called $name but its not in the code we have been looking at.

An issue with {{ $value->name }} will not give an error Undefined property $name

Please sign in or create an account to participate in this conversation.