Why does this return only one result? - laravel

I have the following query. The query should be returning all five rows that I have where user_id = 1, but it's only returning the first result. Why?
$servers = DB::table('posts as p')
->select('p.id', 'p.content', DB::raw('COUNT(i.id) as num_results'))
->leftJoin('images as i', 'i.post_id', '=', 'p.id')
->where('p.user_id', Auth::id())
->get();
Thank you.

public function index(User $user)
{
$users = $user
->where('rol','2')//for normal user
->leftjoin('cityes', 'cityes.id', '=', 'users.cityes_id')
->select('users.id', 'users.name','cityes.name as city')
->get();
}
this code is work for me try this way

Related

Pluck the values from WITH Eloquent in Laravel

I have a dropdown box that shows only the user with collector and borrower's role. Here's my code
public function create()
{
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');
})
->with('profile')
->get()
->pluck('name','id');
return view('dashboard.documents.create', compact('users'));
}
this code is fine but what I what to pluck is the column first_name, last_name and user_id from 'profile'.
How can I achieve that?
Thank you so much in advance!
You do one of these
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');})
->with('profile')
->select('name','id')
->get();
Or
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');})
->with('profile')
->get(['name','id']);
Both ways will return a collection with related models each model record will has the selected columns, if you want to convert it to an array you can add ->toArray() after the two ways and it will convert the collection to array.

Laravel query not giving desired result

Can someone help fix this query. Query is returning null result for "j.code" and "accounts.title". And I am 100% sure that it should return result. I think my left join are missing something. Is this the proper way of using left join within a left join.
$query = DB::table('autostk')
->where('autostk.branchid', $branch_id)
->where('autostk.itemcode',$request->itemcode)
->whereDate('autostk.date','<=',$request->tdate)
->leftjoin('journal AS j', function($join) use ($branch_id) {
$join->on('autostk.refno', '=', 'j.vno')
->where('j.code', '>=', 100)
->where('j.branchid', $branch_id)
->where('j.vtype', '=', 'autostk.vtype')
->leftjoin('accounts', 'j.code', '=', 'accounts.code')
->where('accounts.branchid', $branch_id);
})
->select('j.code','accounts.title','autostk.*')
->orderBY('date')->get()
->map(function ($item, $key) {
return (array) $item;
})
->all();
The raw query being generated is :
select `j`.`code`, `accounts`.`title`, `autostk`.* from `autostk`
left join (`journal` as `j` left join `accounts` on `j`.`code`=`accounts`.`code`)
on `autostk`.`refno` = `j`.`vno` and `j`.`code` >= ? and `j`.`branchid` = ?
and `j`.`vtype` = ? and `accounts`.`branchid` = ? where `autostk`.`branchid` = ?
and `autostk`.`itemcode` = ? and date(`autostk`.`date`) <= ? order by
`autostk`.`date` asc
UPDATE :
While checking the QueryLog i noticed that the binding for 'j'.'vtype' is "autostk.vtype"
Applying the query in workbench with 'autostk.vtype' returned null results.
But when I changed it to 'autostk'.'vtype' the results showed up correctly.
How to make this change in Laravel Eloquent ?
Please try like below:
$query = DB::table('autostk')
->where('autostk.branchid', $branch_id)
->where('autostk.itemcode',$request->itemcode)
->whereDate('autostk.date','<=',$request->tdate)
->leftjoin('journal as j', function($join) use ($branch_id) {
$join->on('autostk.refno', '=', 'j.vno')
->where('j.code', '>=', 100)
->where('j.branchid', $branch_id)
->where('j.vtype', '=', 'autostk.vtype')
->leftjoin('accounts', 'j.code', '=', 'accounts.code')
->where('accounts.branchid', $branch_id);
})
->select('j.code','accounts.title','autostk.*')
->orderBy('autostk.date')->get()
->map(function ($item, $key) {
return (array) $item;
})
->all();
There is one problem:
->orderBy('autostk.date')
And you can use toArray() instead of map() like this
->orderBy('autostk.date')->get()->toArray();
Found the solution. Correct query is :
$query = DB::table('autostk')
->where('autostk.branchid', $branch_id)
->where('autostk.itemcode',$request->itemcode)
->whereDate('autostk.date','<=',$request->tdate)
->leftjoin('journal AS j', function($join) use ($branch_id) {
$join->on('autostk.refno', '=', 'j.vno')
->on('autostk.vtype', '=', 'j.vtype')
->where('j.code', '>=', 100)
->where('j.branchid', $branch_id)
->leftjoin('accounts', 'j.code', '=', 'accounts.code')
->where('accounts.branchid', $branch_id);
})
->select('j.code','accounts.title','autostk.*')
->orderBY('autostk.date')->get()
->map(function ($item, $key) {
return (array) $item;
})
->all();
->leftjoin('journal as j', function($join) use ($branch_id) {
as instead of AS

Collapse two collections to one in laravel

I need help with collapse my collections.
Its my code
public function showFriends(){
$request1 = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.user_id')
->where([['friend_id', '=',Auth::user()->id],['accepted', '=',true]])
->orWhere([['user_id','=',Auth::user()->id],['accepted','= ',true]])
->get();
$request2 = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.friend_id')
->where([['user_id','=',Auth::user()->id] , ['accepted','=',true]])
->get();
$all = collect($request1,$request2)->collapse();
return $all;
}
Problem is in $all variable because it return empty collection. Thank you for help.
Use the merge method on collections:
$all = $request1->merge($request2);
You can try this
$collection = collect($request1,$request2);
$collapsed = $collection->collapse();
You can also do this potentially with one query by utilizing union:
public function showFriends(){
$query1 = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.user_id')
->where([['friend_id', '=',Auth::user()->id],['accepted', '=',true]])
->orWhere([['user_id','=',Auth::user()->id],['accepted','= ',true]]);
$request = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.friend_id')
->where([['user_id','=',Auth::user()->id] , ['accepted','=',true]])
->union($query1)
->get();
return $request;
return $request->all(); // If you want an array rather than collection
}
The Laravel Documentation on unions:
The query builder also provides a quick way to "union" two queries
together. For example, you may create an initial query and use the
union method to union it with a second query

laravel join query is not working

I have the next query and I want to get the price_types.name but is not returned:
$projects = Project::with('projectsTask')
->select('projects.*',
'price_types.name as name_type'
)
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get();
Here an image query is retrievng
This on picture "type_list" must be string text
Maybe somebody can help me.
Many thanks!
Try this:
$projects = Project::with('projectsTask')
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get([''projects.*',
'price_types.name as name_type'']);
get method receive as parameter an array with fields that you want.
$projects = Project::join('tasks', 'projects.id', '=', 'tasks.project_id')
->select('tasks.*',
'price_types.name as name_type',
'statuses.name as name_status'
)
->where([['client_id', $client->id], ['tasks.status_type', '!=', 2]])
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->join('statuses', 'tasks.status_type', '=', 'statuses.type')
->orderBy('tasks.id', 'DESC')
->get();

LIKE doesn't return results

I am trying to find "sam" inside FullName in the table users with this eloquent operation:
$user = User::where('id', '=', $input)->orWhere('fullName', 'LIKE', "%$input%")->find(10);
finding through id works as expected but the Where with LIKE isn't returning any results.
(if $input is 1 the first where returns a result but if $input is sam the second where doesn't return anything)
In the database fullName has the value "Sam Pettersson".
Anything I am doing wrong?
For some reason laravel doesn't want find() and like queries in the same query so using this instead worked:
$user = User::where('id', '=', $input)->orWhere('fullName', 'LIKE', "%$input%")->take(10)->get();
$user = User::where(function($query) use($input)
{
$query->where('id', '=', $input)
->orWhere('fullName','LIKE', '%'.$input.'%');
})->get();
This worked for me. Hope this work for you.

Resources