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.
Related
I am building a simple private chat app with laravel.
I have a messages table with a column : id/sender_id/receiver_id and body
I am trying to show chat history related to user when i select to any user but unfortunately my query is not working properly all users seeing all message please help me how can i resolved that thank u.
Controller
Message::where('sender_id', Auth::user()->id)
->orWhere('receiver_id', Auth::user()->id)
->where('sender_id', $userId)
->orWhere('receiver_id', $userId)
->get();
make subquery where like this:
Message::where(function($q){
$q->where('sender_id', Auth::user()->id)
->orWhere('receiver_id', Auth::user()->id);
})->where(function($q){
$q->where('sender_id', $userId)
->orWhere('receiver_id', $userId)
})->get();
Just add ->where(function($q){}) case orWhere breakdown all other conditions
try this code
Message::where(function ($q) {
$q->where('sender_id', Auth::user()->id)
->orWhere('receiver_id', Auth::user()->id);
})->where(function ($q) {
$q->where('sender_id', $userId)
->orWhere('receiver_id', $userId);
})->get();
I have just created some payment system for a school and I need to filter my data to Paid or Not Paid category.
$queryBuilder = Student::query()
->leftJoin('fee_scheem_maps', 'students.students_id', '=', 'fee_scheem_maps.sfc_student_id')
->leftJoin('fee_scheems', 'fee_scheem_maps.sfc_feescheem_id', '=', 'fee_scheems.fee_scheems_id')
->leftJoin('individual_fee_scheems', 'fee_scheem_maps.sifc_feescheem_id', '=', 'individual_fee_scheems.ifs_id')
->leftJoin('fee_groups', 'fee_scheems.fee_scheems_id', '=', 'fee_groups.fg_fee_scheem_id')
->leftJoin('school_fee_collectors', 'students.students_id', '=', 'school_fee_collectors.fc_student_id')
// ->Where('school_fee_collectors.fc_fee_group_id', $fromDate)
// ->orWhereNull('school_fee_collectors.fc_fee_group_id', $pstatus)
// ->orWhereNull('school_fee_collectors.fc_fee_group_id', $fromDate)
->when($fromDate, function ($query) use ($fromDate) {
return $query->where('school_fee_collectors.fc_fee_group_id', $fromDate);
})
->select('*', DB::raw('count(students_id) as total'));
It is working fine with one clause: I need to randomly choose where or notWhere in my case...
$queryBuilder = Student::query()
->leftJoin('fee_scheem_maps', 'students.students_id', '=', 'fee_scheem_maps.sfc_student_id')
->leftJoin('fee_scheems', 'fee_scheem_maps.sfc_feescheem_id', '=', 'fee_scheems.fee_scheems_id')
->leftJoin('individual_fee_scheems', 'fee_scheem_maps.sifc_feescheem_id', '=', 'individual_fee_scheems.ifs_id')
->leftJoin('fee_groups', 'fee_scheems.fee_scheems_id', '=', 'fee_groups.fg_fee_scheem_id')
->leftJoin('school_fee_collectors', 'students.students_id', '=', 'school_fee_collectors.fc_student_id')
->when($fromDate, function ($query) use ($fromDate) {
return $query->where('school_fee_collectors.fc_fee_group_id', $fromDate);
})
->where(function($query) use ($request){
if($request->paid_status){
$query->where('table_name.columName',$request->paid_status)
}
})->select('*', DB::raw('count(students_id) as total'));
you can use where like this , when your $request->paid_status is null means have no filter about paid not paid thats time query is return everything but when is have filter thats time only this where is working
->where(function($query) use ($request){
if($request->paid_status){
$query->where('table_name.columName',$request->paid_status)
}
})
In my Laravel-5.8 I have these queries:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', $employee_with_goals)
->get();
$employee_with_goals = DB::table('hr_employees')
->join('appraisal_goals', 'hr_employees.employee_code', '=', 'appraisal_goals.employee_code')
->select('hr_employees.employee_code')
->where('hr_employees.company_id', $userCompany)
->where('hr_employees.hr_status',0)
->where('appraisal_goals.is_published', '=', '1')
->where('appraisal_goals.is_approved', '=', '3')
->groupBy('hr_employees.employee_code')
->get();
$manager is the main query
How do I combine these two queries as one using $employee_with_goals as the variable in whereIn()?
You can use the pluck method to retrieve all values for a given key in a collection:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', $employee_with_goals->pluck('employee_code'))
->get();
You can actually use a subquery too:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', function ($query) use ($userCompany) {
$query
->table('hr_employees')
->select('hr_employees.employee_code')
->join('appraisal_goals', 'hr_employees.employee_code', '=', 'appraisal_goals.employee_code')
->where('hr_employees.company_id', $userCompany)
->where('hr_employees.hr_status',0)
->where('appraisal_goals.is_published', '=', '1')
->where('appraisal_goals.is_approved', '=', '3')
->groupBy('hr_employees.employee_code')
})
->get();
The code above is untested.
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();
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();