this query is for codeigniter how to make it in laravel eloquent
$this->db->select('*');
$this->db->from('countries cun');
$this->db->join('cities cit', 'cit.country_code=cun.country_code', 'left');
$this->db->join('city_states cits', 'cits.country_code=cun.country_code', 'left');
$this->db->join('timezone timz', 'timz.country_code=cun.country_code', 'left');
$this->db->where('cun.country',$id);
return $query = $this->db->get()->result();
Try this
return DB::table('countries AS cun')
->leftJoin('cities AS cit', 'cit.country_code', '=', 'cun.country_code')
->leftJoin('city_states AS cits', 'cits.country_code', '=', 'cun.country_code')
->leftJoin('timezone AS timz', 'timz.country_code', '=', 'cun.country_code')
->where('cun.country', $id)
->get();
Eloquent Model
return Country::leftJoin('cities AS cit', 'cit.country_code', '=', 'countries.country_code')
->leftJoin('city_states AS cits', 'cits.country_code', '=', 'countries.country_code')
->leftJoin('timezone AS timz', 'timz.country_code', '=', 'countries.country_code')
->where('countries.country', $id)
->get();
See here
Countries::with(["cities", "city_states", "timezone"])->where("country", $id)->get();
Countries - Eloquent Model.
cities, city_states, timezone - relationships in the Countries model.
Related
Assume I have book models and borrow models. They are one-to-many relationships. I want to get a collection of all the books but return the result if the book is borrowed within the current week. But if it's not, then don't display. I've been trying whereHas(), but I think it didn't suit what I'm looking for.
$displayed_books = Books::orderBy('updated_at', 'DESC')
->ifHasborrow('borrow', function ($query) {
$query->where('start_date', '>=', currentweek())
->where('end_date', "<=", currentweek());
})
$displayedBooks = Books::orderBy('updated_at', 'DESC')
->where(function ($query) {
$query->whereHas('borrow', function($query) {
$now = Carbon::now();
$query->where([
['start_date', '>=', $now->startOfWeek()],
['end_date', '<=', $now->endOfWeek()]
]);
$query->orWhereDoesntHave('borrow');
})
})->get();
When I try to connect my table using a query. But I need the building relationship using Laravel eloquent.
This is my query
{
$students = Student::with('FeeScheem')
->leftJoin('fee_scheems', 'fee_scheems.fs_id', '=', 'students.stu_fee_scheem_id')
->leftJoin('individually_fee_scheems', 'individually_fee_scheems.id', '=', 'students.stu_ind_fee_scheem_id')
->leftJoin('fee_masters', 'fee_masters.fee_master_ind_fee_scheem_id', '=', 'individually_fee_scheems.id')
->leftJoin('fee_groups', 'fee_groups.fg_id', '=', 'fee_masters.fee_master_fg_id')
->leftJoin('school_fee_collectors', 'school_fee_collectors.fc_fg_id', '=', 'fee_groups.fg_id')
->where('students.stu_id', $students)
->get();
$this->students = $students;
}
I have a dropdown box that shows only the user with collector and borrower's role. Here's my code
public function create()
{
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');
})
->with('profile')
->get()
->pluck('name','id');
return view('dashboard.documents.create', compact('users'));
}
this code is fine but what I what to pluck is the column first_name, last_name and user_id from 'profile'.
How can I achieve that?
Thank you so much in advance!
You do one of these
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');})
->with('profile')
->select('name','id')
->get();
Or
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');})
->with('profile')
->get(['name','id']);
Both ways will return a collection with related models each model record will has the selected columns, if you want to convert it to an array you can add ->toArray() after the two ways and it will convert the collection to array.
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();
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.