How can I write this kind of queries in laravel 5.2 eloquent or query builder?
$query = Customers::leftjoin('Query string');
if(condition)
{
//This part added to query
}
else
{
//This part added to query
}
->get();
You have to remember your query in a variable, I.E. like this:
$query = Customers::leftjoin('Query string');
if(condation){
$query = $query->where('something', '=', 'something');
} else {
$query = $query->where('somethingelse', '=', 'somethingelse');
}
$query = $query->get();
Normally you would have $query->where()-get();, which is basically the same thing, $query will hold the result of your ->where() and allows you to chain further on $query
Related
I am in new in Laravel. I want to make dynamic where queries with laravel query builder.Normally I can make dynamic queries in php
$where = array(
'hello' => 'world'
);
function get($where = null){
if($where == "") $where = "";
//Function which converts where clause into queries
wheretoqueries($where); //converts where clause
$sql = "SELECT * FROM $tbl $where";
return $sql;
}
echo get($where);
If where clause is null queries will be
SELECT * FROM $tbl
If where clause is not null queries will be
SELECT * FROM $tbl WHERE hello = "world"
Laravel orm works fine for where clause if key and value exists
A::where($where)->get();
If where is null following method will not work
You can chain the where queries as:
$query = Model::query();
if (!empty($value)) {
$query->where('column', $value);
}
$query->get();
OR
You can use when method as:
Model::when($value, function ($query) use ($value) {
return $query->where('column', $value);
})
->get();
Try this. If $where variable contain something then the query will execute otherwise it will retrieve all data from A Model.
function get($where = null){
if($where != null){
A::where('field_name', '=', $where)->first();
}else{
A::all();
}
}
Note: if your query return more than one value then you have to use get() method at end of query builder instead of first();
Reference: https://laravel.com/docs/5.3/queries#where-clauses
I am trying to make a MySQL query inside Laravel.
> "select * from users u where (name like
> '%".$request->search_string."%' or email like
> '%".$request->search_string."%') and (user_type=2)";
I tried the code below
public function searchUsers(Request $request){
$query = DB::table('users as u');
$query->where('u.user_type',2);
$query->where(function($query,$request){
$query->orwhere('u.name','LIKE','%'.$request->search_string.'%');
$query->orwhere('u.email','LIKE','%'.$request->search_string.'%');
});
$result['all_users'] = $query->get();
return Response::json($result);
}
But I am getting the following error
Missing argument 2 for
App\Http\Controllers\PatientController::App\Http\Controllers{closure}()
You have syntax error in where clause. See this:
public function searchUsers(Request $request){
$query = DB::table('users as u');
$query->where('u.user_type',2);
$query->where(function($query)use($request){ // Here is the change
// ^^ Pass only one parameter to closure function and pass `$request` in `use` function
$query->orwhere('u.name','LIKE','%'.$request->search_string.'%');
$query->orwhere('u.email','LIKE','%'.$request->search_string.'%');
});
$result['all_users'] = $query->get();
return Response::json($result);
}
I am using codeigniter active record query.
function select_in($table,$cond)
{
$this->db->select('*');
$this->db->from($table);
$this->db->where_in('brand_id',$cond);
$query = $this->db->get();
//echo $this->db->last_query(); exit;
return $query;
}
I need to pass another one where_in condition in this query.Is it Possible?
Try with -
$this->db->where_in('brand_id',$cond);
$this->db->where_in('field' , $cond_new);
I have a query builder that works:
$article = Page::where('slug', '=', $slug)
->where('hide', '=', $hidden)
->first();
But I want to only add the second where statement if hidden is equal to 1. I've tried the code below which shows the logic of what I'm trying to do, but it doesn't work.
$article = Page::where('slug', '=', $slug);
if ($hidden == 1) {
$article->where('hide', '=', 1);
}
$article->first();
I'm using Laravel 4, but I think the question still stands with Laravel 3.
Yeah there's a little "gotcha" with Eloquent and the query builder. Try the code below ;)
$query = Page::where('slug', '=', $slug);
if ($hidden == 1) {
$query = $query->where('hide', '=', 1);
}
$article = $query->first();
Note the assigning of $query within the conditional. This is becuase the first where (statically called) returns a different object to the query object within the conditional. One way to get around this, I believe due to a recent commit, is like so:
$query = Page::where('slug', '=', $slug)->query();
This will return the query object and you can do what you want as per normal (Instead of re-assigning $query).
Hope that helps.
If I got this:
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->execute()->as_array();
For normal, dsiplaying all orders date_created. Now I can filter by user, doing this ?user_id=112
This will make me having this:
if(isset($get['user_id']))
{
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->where('user_id', '=', $get['user_id'])
->execute()->as_array();
}else{
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->execute()->as_array();
}
Although this is no problem for me have this, but this get to be real much ugly code when I have 4 params to filter out from(4 where statements) e.g ?user_id=112&quantity=2&...
Can I somehow make a inline if/action statement, which would grab all user_ids if there's nothing in $get['user_id'] ?
So I can end up having only this:
$result =
DB::select(
'orders.date_created'
)
->from('orders')
->where('user_id', '=', (isset($get['user_id']) ? $get['user_id'] : 'GRAB ALL!'))
->execute()->as_array();
or is there another, even better way to do this?
Tried looking in Kohana docs if there's some ->if()->where()->exitif().. (just some imagination of how it could work)
You can modify Query object before executing:
$query = DB::select()->from(...);
if (isset($get['user_id'])) {
$query->where('user_id', '=', intval($get['user_id']));
}
if (isset($get['quantity'])) {
$query->where('quantity', '=', floatval($get['quantity']));
}
$result = $query->execute()->as_array();