fetch data using query builder by checking multiple data from same column in laravel - sql

DB::table('products')
->where('status', '=', 'published')
->where('sub_category', '=', 'grooming-wellness')
->where('sub_category', '=', 'beauty-care')
->get();
it doesn't work. it returns 0 data.

I am assuming you want to check if a field has a particular value or another value. You can use whereIn to achieve this:
DB::table('products')
->where('status', '=', 'published')
->whereIn('sub_category', ['grooming-wellness', 'beauty-care'])
->get();
Laravel 6.x Docs - Query Builder - Where Clauses - Additional Where Clauses whereIn

Related

Laravel query builder left join from subquery

I'm using Laravel query builder and trying to do something like this:
$builder = DB::table('mainTable')->where("mainTable.id", "=", 111);
$builder->leftJoinSub(
DB::table('table2')
->where('table2.id', '=', DB::raw("mainTable.subtableId"))
->where('table2.region', '=', DB::raw("mainTable.region"))
->orderBy('table2.someOrder', 'DESC')
->select('kamery2.id')
->first(),
'myId',
'table2.id',
'=',
'myId'
);
And I'm getting
The multi-part identifier "mainTable.subtableId" could not be bound.
However when I write it with DB::raw('...') it works.
$builder->leftJoin(
'table2',
'table2.id',
'=',
DB::Raw("...")
);
So I'm trying to find out what I'm doing wrong. Did I make a syntactic mistake only? Or Laravel query's builder doesn't allow what I want to achieve and I have to use ->leftJoin(.... DB::raw() ) ?

Laravel carbon check if past date from table

So i want to get data from my API where datetime in the past,but when i tried this code.. its appear that i got data where ONLY DATE in the past not include the time
$id_merchant = $request->input('id_merchant');
$Promotion = DB::table('promotion')
->leftJoin('product', 'product.id', '=', 'promotion.id_product')
->where('promotion.id_merchant', '=', $id_merchant)
->whereDate('end_time','>=', Carbon::now())
->select('promotion.*','product.product_name','product.price','product.stock')
->get();
i see some people solve it with
$end_time->isPast() function
but i dont know how to implement this in my case.
sorry for my english
->where('end_time', '>=', Carbon::now())
(whereDate filter the time)

ambiguous in property names of object due to inner join in laravel

I using following code for joining two tables in my controller
$clientdata = DB::table('clients')
->join('users', 'clients.id', '=', 'users.id')->get();
In blade:
#foreach($clientdata as $clientdata)
<td>{{$clientdata->first_name(client)}}</td>
<td>{{$clientdata->last_name(client)}}</td>
<td>{{$clientdata->first_name(users)}}</td>
<td>{{$clientdata->last_name(users)}}</td>
#endforeach
but my both tables clients and users contains column of same name as first_name and last_name, and i want to access first_name and last_name of both clients table and users table, so how can i do that
You can do
$clientdata = DB::table('clients')
->join('users', 'clients.id', '=', 'users.id')
->select(
'users.first_name as users_first_name',
'users.last_name as users_last_name',
'clients.first_name as clients_first_name',
'clients.last_name as clients_last_name',
)
->get();
In your blade file:
#foreach($clientdata as $data)
<td>{{$data->users_first_name}}</td>
<td>{{$data->users_last_name}}</td>
<td>{{$data->clients_first_name}}</td>
<td>{{$data->clients_last_name}}</td>
#endforeach
I use always this solution
Lazy one :) (In case of high number of columns)
I select all columns in both tables (table.*), and I add my ambiguous columns with aliases (table.ambiguous_1 AS alias)
Example
$clientdata = DB::table('clients')
->join('users', 'clients.id', '=', 'users.id')
->select(
'users.*',
'clients.*',
'users.first_name AS users_first_name',
'users.last_name AS users_last_name',
'clients.first_name AS clients_first_name',
'clients.last_name AS clients_last_name',
)
->get();
In the blade I use the alias for ambiguous columns and orginal column name in others.
Try this code
DB::table('clients') ->join('users', 'clients.id', '=', 'users.id')->select('clients*', DB::raw('clients as client_name'))->get();

Laravel Query Builder Statement return no result, but sql statement does

Laravel Query Builder
$data = CustomerPrepaid
::join('pos_sales', 'customer_prepaid.customer_id', '=', 'pos_sales.customer_id')
->join('pos_sales_product', 'pos_sales.pos_sales_code', '=', 'pos_sales_product.pos_sales_code')
->where('pos_sales_product.product_id', 'customer_prepaid.product_id')
->select('customer_prepaid.customer_id', 'customer_prepaid.created_at',
'pos_sales_product.pos_sales_product_code as reference_no',
'customer_prepaid.product_id', 'customer_prepaid.balance',
'last_used', 'expiry_date', 'customer_prepaid.amount as price')
->offset(($page-1)*$limit)->limit($limit)->get();
SQL
SELECT customer_prepaid.customer_id, customer_prepaid.created_at as purchase_date,
pos_sales_product.pos_sales_product_code as reference_no, customer_prepaid.product_id,
customer_prepaid.balance, customer_prepaid.amount*customer_prepaid.balance as value,
last_used, expiry_date, customer_prepaid.amount as price,
customer_prepaid.amount*customer_prepaid.balance as total
FROM customer_prepaid
JOIN pos_sales ON customer_prepaid.customer_id = pos_sales.customer_id
JOIN pos_sales_product ON pos_sales.pos_sales_code = pos_sales_product.pos_sales_code
WHERE pos_sales_product.product_id = customer_prepaid.product_id
The resulting SQL executed on the server returns the right result, but I get no eloquent result, why might that be?
Oh gosh, took me forever to realize you misused ->where.
Change your ->where to ->whereColumn:
$data = CustomerPrepaid
::join('pos_sales', 'customer_prepaid.customer_id', '=', 'pos_sales.customer_id')
->join('pos_sales_product', 'pos_sales.pos_sales_code', '=', 'pos_sales_product.pos_sales_code')
->whereColumn('pos_sales_product.product_id', 'customer_prepaid.product_id')
->select(
'customer_prepaid.customer_id',
'customer_prepaid.created_at',
'pos_sales_product.pos_sales_product_code as reference_no',
'customer_prepaid.product_id', 'customer_prepaid.balance',
'last_used', 'expiry_date', 'customer_prepaid.amount as price'
)
->offset(($page-1)*$limit)
->limit($limit)
->get();
You have to use whereColumn instead of where to make column comparison. Else it's expecting a third parameter value to be set.
Check the documentation on how to use whereColumn: https://laravel.com/docs/5.5/queries#where-clauses

Column not found in laravel

I'm trying to create a self join in laravel using aliases, but it doesn't seem to want to pick it up, because I get an error saying that the columns p1.sub_menu and p2.5 are not found. Here is my join:
$menu = DB::table('pages AS p1')
->leftJoin('pages AS p2', 'p1.sub_menu', '=', 'p2.'.$id.'')->get();
Error because of your leftJoin statement
Params in leftJoin statement are columns which you want to use relationships.
$menu = DB::table('pages AS p1')->leftJoin('pages AS p2', 'p1.sub_menu', '=', **'p2.'.$id.''**)->get();
My solution is:
$menu = DB::table('pages AS p1')->leftJoin('pages AS p2', 'p1.sub_menu', '=', 'p2.sub_menu')->where("p2.menu_id", "=", $menu_id)->get();