I have a Query Builder problem on Laravel 5.4,
I want to select only certain field on where statement.
if I don't put WHERE statement function the SELECT function performed nicely, but when I put WHERE LIKE function then seems the SELECT function not working and it automatically selecting all (*)
Fyi, both user and address table has 'name' field. it only select name from user table. it work nicely.
DB::table('user')
-leftJoin('address', 'user.id', '=', 'address.user_id')
->select('user.name')
->paginate(1);
but this is not working
DB::table('user')
-leftJoin('address', 'user.id', '=', 'address.user_id')
->select('user.name')
->where('user.name', 'LIKE', '%jhon%')
->paginate(1);
how do I accomplish this?
Any anwer will be highly appreciated!
Please write this. This will solve your problem
DB::table('user')
->leftJoin('address', 'user.id', 'address.user_id')
->where('user.name', 'LIKE', '%jhon%')
->select('user.name')
->get();
Its working. Give it a test.
DB::table('user')
->leftJoin('address', 'address.user_id', '=', 'user.id')
->where('user.name', 'like', '%jhon%')
->select('user.name')
->get();
Related
I'm working with a query where the results finish on page 21, but I'm getting 29 pages of links. My problem might be a groupBy() problem, but I don't know how to do it.
$afiliates = DB::table('ad_afiliado as af')
->select('af.logo_url', 'af.NombreComercial', 'af.Destacado',
'af.id_afiliado', 'af.Clave')
->join('af_promocion as promo', function ($join) {
$join->on('af.Clave', '=', 'promo.id_afiliado');
})
->where('promo.v_fin', '>', $FechaActual)
->where('af.Activo', '=', 'S')
->where('af.Categoria', 'like', $categoryStr)
->orderBy('af.NombreComercial')
->orderBy(DB::raw('RAND()'))
->distinct()
->paginate(9);
I found the answer. It seems to be an issue with laravel distinct() and pagination, especialy when making joins.
The thread is here:
distinct() with pagination() in laravel 5.2 not working
I had to add the field name was causing repetition of results, to the distinct() and paginate().
In my case 'promo.id_afiliado'
as you'll see in the code next
$afiliates = DB::table('ad_afiliado as af')
->join('af_promocion as promo', 'af.Clave', '=', 'promo.id_afiliado')
->select('af.logo_url', 'af.NombreComercial', 'af.Destacado', 'af.id_afiliado', 'af.Clave')
->where('promo.v_fin','>',$FechaActual)
->where('af.Activo','=', 'S')
->distinct('promo.id_afiliado')
->orderBy('af.Destacado', 'desc')
->orderBy('af.NombreComercial')
->paginate(9, 'promo.id_afiliado');
Thaks #TimLewis for caring, hope this will usefull to others.
I try to implement ajax based filtering in my ecommerce project homepage for searching a product. I am using Laravel Query Builder. My Query for filtering products is given below-
$result= DB::table('products')
->leftjoin('products_description','products.products_id','products_description.products_id')
->leftjoin('image_categories', 'products.products_image', '=', 'image_categories.image_id')
->leftjoin('products_to_categories','products.products_id','products_to_categories.products_id')
->leftjoin('categories','products_to_categories.categories_id','categories.categories_id')
->when($category_slug, function($q) use ($category_slug) {
return $q->where('categories.categories_slug', $category_slug);
})
->where('products_name','like',"%{$querval}%")
->where('image_categories.image_type','=','ACTUAL')
->orderby('products.products_id','DESC')
->take(5)
->get();
I get every product twice in search result. don't know why. A sample response is given in this picture.
Can anyone help me to optimize my query for getting the desired result?
join with categories table only when you have to.
and select your columns strictly. then group by selected columns.
$result= DB::table('products')
->leftjoin('products_description','products.products_id','products_description.products_id')
->leftjoin('image_categories', 'products.products_image', '=', 'image_categories.image_id')
->when($category_slug, function($q) use ($category_slug) {
return $q->leftjoin('products_to_categories','products.products_id','products_to_categories.products_id')
->leftjoin('categories','products_to_categories.categories_id','categories.categories_id')
->where('categories.categories_slug', $category_slug);
})
->where('products_name','like',"%{$querval}%")
->where('image_categories.image_type','=','ACTUAL')
->orderby('products.products_id','DESC')
->select(['products_name','products.id','image_path'])
->groupBy(['products_name','products.id','image_path'])
->take(5)
->get();
you can use from groupBy to avoid comming repeated data.
try this one:
$result= DB::table('products')
->leftjoin('products_description','products.products_id','products_description.products_id')
->leftjoin('image_categories', 'products.products_image', '=', 'image_categories.image_id')
->when($category_slug, function($q) use ($category_slug) {
return $q->where('categories.categories_slug', $category_slug);
})
->where('products_name','like',"%{$querval}%")
->where('image_categories.image_type','=','ACTUAL')
->orderby('products.products_id','DESC')
->groupBy('products_name','products.products_id')
->take(5)
->get();
The following query works fine, but the problem is that it gives the id of the events table instead of tasks table id in the output result.
Task::join('events', function ($join) {
$join->on('events.task_id', '=', 'tasks.id')
->where('events.event_type', '=', 'Task')
->where('events.task_stage', '!=', 'assigned');
})->select('tasks.*')
->get();
Try this, one should work
->select('*', 'tasks.id as taskID')->get();
This is because maybe both tasks and events table have id field. So u need to use a separate query to specify the id.
try this one
use mysql alias here
Task::join('events', function ($join) {
$join->on('events.task_id', '=', 'tasks.id')
->where('events.event_type', '=', 'Task')
->where('events.task_stage', '!=', 'assigned');
})->select('tasks.*','tasks.id as taskId')->get();
I noticed that when you join tables in laravel using lefJoin() or Join the ids that it returns are the one of the last table. So I was wondering if I can join two tables in laravel and keepig the ids of the first table?
Here's my query
$comments = DB::table('posts')
->where('posts.parent_id',$id)
->leftJoin('profiles', 'profiles.user_id', '=', 'posts.user_id')
->leftJoin('users', 'users.id', '=', 'posts.user_id')
->get();
you should show ur code .
but try like this
$query = Users::where("users.user_id","=",$users->user_id)
if ($request->exists('product_name')) {
$query->join('products', 'users.user_id', 'products.user_id');
$query->where('products.product_name', 'like', '%' . $request->input('product_name')
. '%');
}
$test= $query->get();
Better use aliases in this case.For Example
DB::table('table_1')->select('table1.column_name as alias_name', 'table_2.column_name as alias_name2')->leftjoin('table_2', 'table_2.userId', '=', 'table_1.id')
->get();
You can select all from first table and nothing from second
DB::table('table_1')->join('table_2', 'table_1.id', '=', 'table_2.table_1_id')
->selectRaw('table_1.*')
->get();
Example DB::table('guests')->join('answers', 'guests.id', '=', 'answers.guest_id')
->selectRaw('guests.*')->get()
You also can select here anything from another table
use: ->addSelect('table_2.column')
I'm implementing a search bar that can search customers by first name, last name, or both. So, for example, Mike Hizer would be matched by Mike, Hizer, zer, Mike Hizer, etc. Here's what I came up with:
Customer::where(
DB::raw('concat(first_name," ",last_name)'), 'like', "%{$request->search}%"
)->get()
It works. But is there a more Eloquent approach to combine these two columns (first_name and last_name) without resorting to the DB facade? Would something like
->where(['first_name','last_name'], 'like', "%{$request->search}%")
be possible to achieve?
If you don't want to use the DB facade you could use the whereRaw method:
Customer::whereRaw('concat(first_name," ",last_name) like ?', "%{$request->search}%")->get();
Hope this helps!
$query = User::where('id', '=', '2');
$query->where('first_name', 'LIKE', "%$search%");
$query->or_where('last_name', 'LIKE', "%$search%");
$users = $query->get();
Try this way..