You need a ->get() on the end of your query to execute it.
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]);
}
Have you tried the db facade for this:
Please or to participate in this conversation.