(select business_name from clients where id = orders.main_client_id) as b_name
This above sql query and I am converted sql to laravel type is right or wrong because this below query give me output [][][] this type any suggestion..
->addColumn('business_name', function($data) {
$b_name = DB::table("clients")
->select("business_name")
->where('clients.id','main_client_id')
->get();
return $b_name;
});
You left out the comparison symbol (=)
$b_name = DB::table('clients')
->where('id', '=', 'main_client_id')
->get();
or
$b_name = DB::table('clients')
->where('clients.id', '=', 'main_client_id')
->value('business_name');
you are comparing with column value not mere value, so you have to use whereColumn:
->addColumn('business_name', function($data) {
$b_name = DB::table("clients")
->select("business_name")
->whereColumn('clients.id','main_client_id')
->get();
return $b_name;
});
Related
I have a query
$BaseUsers = DB::table('users', 'users.ustatus', '=', '1')
->join('user_infos', 'user_infos.user_id', '=', 'users.id')
->select('users.*', 'user_infos.add1', 'user_infos.add3')
->paginate(3);
It work fine. But i need insert condition "where" to search in result
i send $request from form. How insert condition to query?
In PHP it looks like this:
if($request['add_1'])
{
$insert1=" and add_1<>'' ";
}
if($request['add_2'])
{
$insert2=" and add_2<>'' ";
}
...
$sql=" SELECT users.*, user_infos.add1, user_infos.add3 FROM users JOIN user_infos where users.ustatus=1 and user_infos.user_id=users.id ".$insert1." ".$insert2."";
How can this be done on laravel in Controller? I mean - just insert the necessary condition insert1, insert2 ... into the query
you can use where() and has() to build your query
$query = DB::table('users');
if($request->has('age')) {
$query->where('age', '>', $request->age);
}
$query = $query->get();
I hope it's helpful
All is simple ) I use when in query
$BaseUsers = DB::table('users', 'users.ustatus', '=', '1')
->join('user_infos', 'user_infos.user_id', '=', 'users.id')
->when($request->input('f_add3'), function ($query) {
return $query->where('user_infos.add3', '<>', '');
})
->select('users.*', 'user_infos.add1', 'user_infos.add3')
->paginate(3);
I am using ::selectRaw inside my PHP function and I need that statement to get two conditions. In my case, I am using my category_id column
I tried to do two where but seems like these conflict with each other:
$query = self::selectRaw((is_array($fields)?implode(", ",$fields):$fields))
->where('category_id', '=', 1)
->where('category_id', '=', 2)
->where(function($q) use($search){
if($search){
return $q->where(['group_name' => $search]);
}
});
how do I translate WHERE category_id = 1 AND category_id = 2 in the ::selectRaw query?
You can use the whereIn function.
$query = self::selectRaw((is_array($fields)?implode(", ",$fields):$fields))
->whereIn('category_id', [1,2])
->where(function($q) use($search){
if($search){
return $q->where(['group_name' => $search]);
}
});
I want to show all the Orders from orders table where the status field of that table, equals to canceled OR ignored.
Currently my query goes like this:
$canceled = Order::where('status', 'canceled')
->where('user_id', $uid)
->latest()
->paginate(2);
So how can I add ignored as well to the where clause here?
I think whereIn will do the trick
$canceled = Order::whereIn('status', ['canceled','ignored'])
->where('user_id', $uid)
->latest()
->paginate(2);
Try this query -
$canceled = Order::where(function($q) {
$q->where('status', 'canceled')
->orWhere('status', 'ignored');
})
->where('user_id', $uid)
->latest()
->paginate(2);
I am trying to make the following query in laravel:
SELECT a.name AS subname, a.area_id, b.name, u. id, u.lawyer_id,u.active_search,
FROM subarea a
LEFT JOIN user_subarea u ON u.subarea_id = a.id
AND u.user_id = ?
LEFT JOIN branch b ON a.area_id = b.id
The idea is to obtain the subareas and see if the search is activated by the user.
The user_subarea table might have a record that matches the id of the subarea table where the active_search is equal to 0 or 1. If it doesn't exist I would like the query to return null.
While I was able to achieve this in raw SQL when I try the same with eloquent in Laravel I am not returning any value. I have done the following:
$query = DB::table('subarea')
->join('user_subarea', function($join)
{
$value = \Auth::user()->id;
$join->on( 'subarea.id', '=', 'user_subarea.subarea_id')->where('user_subarea.user_id', '=',$value);
})
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('branch.name', 'subarea.name as subarea', 'user_subarea.active_search_lawyer', 'user_subarea.id' )
->get();
Any help will be much appreciated.
I found by myself the answer it was just to add a lefjoin in the first join. It is not in the laravel docs but works too.
$query = DB::table('subarea')
->lefjoin('user_subarea', function($join)
{
$value = \Auth::user()->id;
$join->on( 'subarea.id', '=', 'user_subarea.subarea_id')->where('user_subarea.user_id', '=',$value);
})
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('branch.name', 'subarea.name as subarea', 'user_subarea.active_search_lawyer', 'user_subarea.id' )
->get();
Try this one, If you get a problem, please comment.
$value = \Auth::user()->id;
$query = DB::table('subarea')
->where('user_subarea.user_id', '=',$value)
->leftJoin('user_subarea', 'subarea.id', '=', 'user_subarea.subarea_id')
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('subarea.name AS subname','subarea.area_id', 'branch.name', 'user_subarea.id','user_subarea.lawyer_id','user_subarea.active_search')
->get();
I have a question regarding join clauses in Eloquent, and whether you can join on a string value rather than a table column.
I have the code below querying a nested set joining parent/child records in a table 'destinations' via a table 'taxonomy'.
The second $join statement in the closure is the one causing an issue; Eloquent assumes this is a column, when I would actually just like to join on t1.parent_type = 'Destination' - ie, t1.parent_type should = a string value, Destination.
$result = DB::connection()
->table('destinations AS d1')
->select(array('d1.title AS level1', 'd2.title AS level2'))
->leftJoin('taxonomy AS t1', function($join) {
$join->on('t1.parent_id', '=', 'd1.id');
$join->on('t1.parent_type', '=', 'Destination');
})
->leftJoin('destinations AS d2', 'd2.id', '=', 't1.child_id')
->where('d1.slug', '=', $slug)
->get();
Is it possible to force Eloquent to do this? I've tried replacing 'Destination' with DB::raw('Destination') but this does not work either.
Thanking you kindly.
Another best way to achieve same is :
$result = DB::connection()
->table('destinations AS d1')
->select(array('d1.title AS level1', 'd2.title AS level2'))
->leftJoin('taxonomy AS t1', function($join) {
$join->on('t1.parent_id', '=', 'd1.id');
$join->where('t1.parent_type', '=', 'Destination');
})
->leftJoin('destinations AS d2', 'd2.id', '=', 't1.child_id')
->where('d1.slug', '=', $slug)
->get();
Replace your on with where
try using DB::raw("'Destination'")