I have problem using translatable library, I am trying to sort the companies by name, so as written in the doc, I need to use inner joins, I tried this code, everything is good but the data is messed up when I add voucher_translations, its like it get from different table or records, I dont know what I am doing wrong :(
$vouchers = Voucher::join('company_translations', 'vouchers.company_id', '=', 'company_translations.company_id')
->join('voucher_translations', 'voucher_translations.voucher_id', '=', 'vouchers.id')
->where('company_translations.locale', '=', 'en');
->where('voucher_translations.locale', '=', 'en')
->orderBy('company_translations.name', 'asc');
Related
I'm trying to call 2 columns from 2 different tables. applicants.ic_no and agents.ic_no. It have different values.
Using the following codes only displayed only ic_no from agents.ic_no
$claimLists = ClaimDetail::join('applicants', 'applicants.ic_no', '=', 'claim_details.ic_no')
->join('agents', 'agents.id', '=', 'claim_details.agent_id')
->where('claim_date', $cutt_off)
->groupBy('agents.id', 'claim_details.id', 'applicants.id')
->orderBy('agents.id')
->orderby('amount', 'desc')
->get();
How do i get both columns to display?
This is because, you have ic_no in both tables. By default MYSQL picks one of it to display, it is the same as having id in both tables and from your results, how would you know which table's ic_no you are accessing while they have the same name?
Alternatively you can use select and DB::raw to change the name of one of the ic_no fields, and similiarly for any other similiar fields. For example;
$claimLists = ClaimDetail::join('applicants', 'applicants.ic_no', '=', 'claim_details.ic_no')
->join('agents', 'agents.id', '=', 'claim_details.agent_id')
->select('claim_details.*', DB::raw('agents.ic_no as agents_ic_no'), 'agents.XXX', 'agents.YYYY', 'applicants.XXX')
->where('claim_date', $cutt_off)
->groupBy('agents.id', 'claim_details.id', 'applicants.id')
->orderBy('agents.id')
->orderby('amount', 'desc')
->get();
instead of XXX and YYY, you can put the fields that you would like to get and you can get as many as you want or remove them, if you don't want to get any field from the second table, but the main thing here is you are access the agents.ic_no as agents_ic_no or whatever name you would like to give it.
I solve this issue by adding select to rename the conflict column name. So this is the code:
$processed = ClaimDetail::join('applicants', 'applicants.ic_no', '=', 'claim_details.ic_no')
->join('agents', 'agents.id', '=', 'claim_details.agent_id')
->select('*')
->selectRaw('applicants.ic_no as nokp, agents.ic_no as agic')
->where('claim_details.agent_id',$agent->id)
->orderBy('claim_details.claimable', 'desc')
->orderby('claim_details.amount', 'desc')
->get();
Thanks guys for your time.
Code
$result = DB::table('disaster_rescue_data')
->join('users', 'disaster_rescue_data.username', '=', 'users.email')
->get();
Problem
I have this very simple join as mentioned in the above code. However when I render the data on a view or dd the $result, I get the same row duplicated and I can not figure out why. (The rows contain the same data.)
(I am using Laravel 5.4)
Can someone please help me figure out why? (I tried different techniques and none of them worked! Thanks.)
to prevent duplication, you should use distinct() method, this method Returns only unique items from the result.
in order to do that, first you must stricly select what you want:
$result = DB::table('disaster_rescue_data')
->select('disaster_rescue_data.*')
->join('users', 'disaster_rescue_data.username', '=', 'users.email')
->distinct()->get();
You could try using distinct in the query. Distinct removes duplicates in a result set.
For example
$result = DB::table('disaster_rescue_data')
->distinct()
->join('users', 'disaster_rescue_data.username', '=', 'users.email')
->get();
See https://laravel.com/docs/5.4/queries
EDIT:
Ok, I am thinking you can now use groupBy to get the unique results. Although it's not clear what your data structure is so this is a guess to use "users.email". Maybe it could be "disaster_rescue_data.username" to group by. Experiment and see.
$result = DB::table('disaster_rescue_data')
->distinct()
->join('users', 'disaster_rescue_data.username', '=', 'users.email')
->groupBy('users.email')
->get();
I'm working with Join in laravel, I'm getting result in one part, but now I need to pick up only some records that are active but I can not implement. My query would be this:
$queryBuilder = $query->parse(
$queryBuilderJSON, DB::table('tab_person')
->where('deleted_at', NULL)
->leftJoin('tab_user', 'tab_person.pac_r', '=', 'tab_user.reg_id'));
// 'tab_user.reg_status', '=', 'A' **I can not implement this condition, how can I do it?**
Ignore queryBuilderJSON, I'm working with it to use Query Builder Jquery, which is a search library. What I just need to know is how to filter the USER records that are only active (they are represented by the letter 'A') and the line is commented out in the code. Thanks in advance!
You can add multiple join conditions using a closure:
->leftJoin('tab_user', function ($query) {
$query->on('tab_person.pac_r', '=', 'tab_user.reg_id');
$query->where('tab_user.reg_status', '=', 'A');
});
You can learn more on advanced join clauses in the Laravel Docs.
SELECT * FROM user_fields WHERE (SELECT CITY FROM register_expert WHERE PERMISSION=1 AND ID=user_fields.ID_USER_FIELD)='$city_save'AND TITLE_USER_FIELD='$text_search_service';
Here is register_expert table image
And here is user_fields table mage
Assuuming,
userFields is your models.
userFields::selectRaw("user_fields.ID_USER_FIELD")
->join('register_expert', function($join) use ($city_save){
$join->on('register_expert.id', 'user_fields.ID_USER_FIELD')
->where('PERMISSION', '1')
->where('city', $city_save)
})->where('TITLE_USER_FIELD', $text_search_service);
Try this code.
I think the Eloquent query should look something like this but as I said in my comment the database and query is not well designed so I don't think it will work but just so you have an idea of how to make more complex queries in Eloquent.
$registerExperts = RegisterExperts::whereColumn('ID_USER_FIELD', 'user_fields.ID')
->Andwhere('PERMISSION', '=', 1)
->limit(1)
->select('city')
->get();
$users = User::where($registerExperts->get('city') , '=', $city_save)
->andWhere('TITLE_USER_FIELD', '=', $text_search_service)
->get();
And here's a good first article to see how to make complex queries using Eloquent ORM : Dynamic relationships in Laravel using subqueries by Jonathan Reinink
I have table (commandeclients, bonlivraisons, factureclientatvas) and I want to get all the bonlivraisons connected to commandeclients which is connected to factureclientatvas.
How can I make this work? Is it correct?
$bl = DB::table('bonlivraisons')
->join('commandeclients', 'bonlivraisons.commande_id', '=', 'commandeclients.id')
->join('factureclientatvas','commandeclients.id','=', 'commandeclients.id')
->join('factureclientatvas','commandeclients.id','=', 'bonlivraisons.commande_id')
->where('factureclientatvas.id','=',$id)
->select('bonlivraisons.reference')
->get();
You are joining factureclientatvas twice.
In the first you are trying to join it where
'commandeclients.id', '=', 'commandeclients.id'
Then, you are trying to join it where
'commandeclients.id', '=', 'bonlivraisons.commande_id'
--
Both seem incorrect as it should be something like
->join('factureclientatvas','factureclientatvas.id','=', 'commandeclients.factureclientatvas_id')
or something like
->join('factureclientatvas','factureclientatvas.id','=', 'bonlivraisons.factureclientatvas_id')
--
Joining of the factureclientatvas is dependant on how you have your tables are structured and the logic of your application.