Is there anyway to use startWith in LeftJoin Laravel? - laravel

I have the following code:
$get_order = DB::table('orders')
->leftJoin('postcodes', 'orders.postcode', '=', 'postcodes.postcode')
->where('order_ref', '=', $order_ref)
->first();
What I am trying to achieve, is do a left join if postcodes.postcode starts with orders.postcode . Is there any way to make this?
Thanks in advance.

$get_order = DB::table('orders')
->leftJoin('postcodes', 'postcodes.postcode','LIKE', DB::raw( "CONCAT(orders.postcode, '%')" ))
->where('order_ref', '=', $order_ref)
->first();

Try This
->leftJoin('postcodes', function($join) {
$join->on('postcodes.postcode', '=', 'orders.postcode')
->on('postcodes.postcode', 'LIKE', "startWith%"));
})

Related

Where clause wtih double OR operator in laravel 8 controller

I need to lookin' for data that the where clause have 3 condition, so its need OR operator twice, here is the code in my controller, I use laravel 8
$salesTransactions = SalesTransaction::when($request->keyword, function ($query) use ($request) {
$query
->where('tanggal_transaksi', 'like', "%{$request->keyword}%")
->orWhere('name', 'like', "%{$request->keyword}%")
->orWhere('status', 'like', "%{$request->keyword}%");
})->join('distributors', 'distributors.id', '=', 'sales_transactions.distributor_id')
->join('users', 'users.id', '=', 'distributors.user_id')
->select('sales_transactions.*', 'users.name')
->orderBy('tanggal_transaksi', 'DESC')->latest('sales_transactions.id')->paginate(25);
I got the error, but dont know how to solve, the problem is I cant use OR operator twice, any idea ? Thanks
If you need to group an "or" condition within parentheses, you may pass a closure as the first argument to the orWhere method. Like:
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere(function($query) {
$query->where('name', 'Abigail')
->where('votes', '>', 50);
})
->get();

how laravel join three tables with advance join clause using variable

Larvel have a question about inner joins with multiple tables and on values. I did build my code like this in laravel.
$clients = clients::leftjoin('clients_payment_type','clients_topup', function($join) {
$join->on('clients_payment_type.user_id', '=', 'clients.id') AND
$join->on('clients_topup.user_id', '=', 'clients.id');
})->where('clients.reference_id','=',$reference_id)->get();
Try this :
$clients = clients::leftjoin('clients_payment_type','clients_topup')
->join('clients','clients_payment_type.user_id', '=', 'clients.id')
->join('client_toup','clients.id', '=', 'clients_topup.user_id')
->where('clients.reference_id','=',$reference_id)
->get();
Regards
Working Answer
$clients1 = DB::table('clients')
->leftjoin('clients_payment_type','clients_payment_type.user_id', '=', 'clients.id')
->leftjoin('clients_topup', 'clients_topup.user_id', '=', 'clients.id')
->where('clients.reference_id','=',$reference_id)
->get();

Laravel 5.2 : How to write query builder with 2 ON in this right join

I have this sql syntax
SELECT vehicles.license_plate, vehicles.year, vehicles.status, vehicles.zone_id, vehicles.car_class_id, harga_sewa.value, harga_sewa.description
FROM `vehicles`
RIGHT JOIN `harga_sewa` ON vehicles.zone_id = harga_sewa.zone_id
AND vehicles.car_class_id = harga_sewa.car_class_id
How I can write something like this?
DB::table('vehicles')
->rightJoin('harga_sewa', 'vehicles.zone_id', '=', 'harga_sewa.zone_id', AND vehicles.car_class_id = harga_sewa.car_class_id)
->select(*)
Try below code
DB::table('vehicles')
->rightJoin('harga_sewa', function ($join) {
$join->on('vehicles.zone_id', '=', 'harga_sewa.zone_id');
$join->on('vehicles.car_class_id', '=', 'harga_sewa.car_class_id');
})
->get();
On mobile, so can't test, but from memory this should work:
DB::table('vehicles')
->rightJoin('harga_sewa', 'vehicles.zone_id', '=', 'harga_sewa.zone_id')
->rightJoin('harga_sewa', 'vehicles.car_class_id', '=', 'harga_sewa.car_class_id')
->select('*')
->get();

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();

Laravel Clear Model Queries

I have this code but I am not sure if is possible be more compact or I´m not sure if this is the way should looks this type of queries in laravel.
public function getRoleTransactionNameByID($id)
{
$role_transaction= $this->model->select(
'roles_transactions.id',
'roles.id as role_id',
'roles.role_name',
'modules.module_name',
'transactions.transaction_name',
'transactions.transaction_description',
'roles_transactions.transaction_action_id',
'transaction_actions.transaction_action_name',
'roles_transactions.created_at',
'roles_transactions.updated_at')
->join('roles', 'roles_transactions.role_id', '=', 'roles.id')
->join('transactions', 'roles_transactions.transaction_id', '=', 'transactions.id')
->join('transaction_actions', 'roles_transactions.transaction_action_id', '=', 'transaction_actions.id' )
->leftjoin('modules','transactions.module_id', '=', 'modules.id')
->where('roles_transactions.id', '=', $id)
->where('roles_transactions.deleted_at','=', null)
->orderby('roles.role_name', 'ASC')
->orderby('modules.module_name', 'ASC')
->orderby('transactions.transaction_name', 'ASC')
->get();
return $role_transaction;
}
Thank you for the help.

Resources