Sina Maafi
Sina Maafi

Reputation: 119

laravel get user information from another table problem

I use this code to get all bank accounts of my users in admin page and keep in mind that i already define uid to relate to user id in my table.

my problem is i can't find a way to show that each bank account related to what account and user info. what is wrong?

Here is my controller :

public function cards()
{
    $cards=DB::table('cards')->orderBy('id','DESC')->get();
    return view('admin.cards' , compact('cards'));
}

Upvotes: 2

Views: 610

Answers (2)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

Use ->joins() or ->leftJoins(), ->rightJoins()

$cards = DB::table('cards')
    ->select('cards.id', 'cards.status', 'cards.other_column', 'users.id as user_id', 'users.status as user_status', 'users.other_col')
    ->join('users', 'cards.uid', '=', 'users.id')
    ->orderBy('cards.id','DESC')
    ->get();

or

$cards = DB::table('cards')
    ->select('cards.*', 'users.id as user_id', 'users.status as user_status', 'users.other_col')
    ->join('users', 'cards.uid', '=', 'users.id')
    ->orderBy('cards.id','DESC')
    ->get();

for more info joins see https://www.w3schools.com/sql/sql_join.asp

Upvotes: 1

Ohidul
Ohidul

Reputation: 192

Here you have to join your users table with card table.Then you will get your desired solution.you can try this.

$id = Your user id;

public function cards()
    {
        $cards=DB::table('cards')
               ->join('users','users.id','=','cards.uid')
               ->select('cards.*')
               ->where('users.id'=$id);
               ->get();
    }

Upvotes: 3

Related Questions