Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abarua's avatar

Database Query Syntax

Hi, I have two tables users(id, username, city, country) and friends(id, user_id, friend_id, status). The 'users' table stores the user information and 'friends' table store the currently logged in user's id, friend_id is the id of the user who sent a friend request to the current user and status will be 'received'. I want to show information of the users to sent requests to the current user. Is my database query syntax correct? Thanks in advance!

I get the following error: Undefined property: Illuminate\Database\MySqlConnection::$username

My DB query:

$friendrequests = DB::table('users')->select(['username','city','country'])->where('id',DB::table('friends')->select('friend_id')->where([['status','received'],['user_id',$user->id]]));

My html:

@extends('layouts.master')

@section('title')
    Friend Requests
@endsection

@section('content')
    @include('includes.message-block')
    <section class="row requests">
        <div class="col-md-6 col-md-offset-3">
            <header><h3>Friend Requests</h3></header>
            <div class="requestlist">
                <header><h2>You have request from:</h2></header>
                @foreach($users as $user)
                    <p>Name: {{ $user->username }}<br />City: {{ $user->city }}<br />Country:{{ $user->country }}</p>
                    <div class="interaction">
                        //stuff here
                    </div>
                @endforeach
                <a href="{{ URL::previous() }}">Back</a>
            </div>
        </div>
    </section>
@endsection

The route:

Route::get('/friendrequests',[
        'uses' => 'FriendController@getFriendRequests',
        'as' => 'friendrequests',
        'middleware' => 'auth'
    ]);

The controller:

public function getFriendRequests()
    {
        $user = Auth::user();
        $friendrequests = DB::table('users')->select(['username','city','country'])->where('id',DB::table('friends')->select('friend_id')->where([['status','received'],['user_id',$user->id]]));

        return view('friendrequests',['users' => $friendrequests]);
    }
0 likes
8 replies
Snapey's avatar

You need a ->get() on the end of your query to execute it.

abarua's avatar

@SNAPEY - Thanks! I think the query ran, but it showed the following error message: Object of class Illuminate\Database\Query\Builder could not be converted to string

Snapey's avatar

You need to use ->get() in your subquery also.

$friendrequests = DB::table('users')
        ->select(['username','city','country'])
        ->where('id',
            DB::table('friends')->select('friend_id')->where([['status','received'],['user_id',$user->id]])->get())->get();
abarua's avatar

Still getting the same error. If I try only sql SELECT * FROM users WHERE id IN (SELECT friend_id FROM friends WHERE status = 'received' AND user_id = 1) in mysql it does show the desired output. Any thoughts?

abarua's avatar

If I don't use nested subquery I do see output in the view. Is there a very specific way to write nested subqueries?

abarua's avatar

@JLRDW - Running raw queries using DB::select() works. Thanks!

jlrdw's avatar

Just remember to do proper binding on your parameters.

Please or to participate in this conversation.