Please can anyone tell me the query? I use Laravel 5.7 with mysql.
My Tables:
users
id
name
recipes
id
user_id
name
flavours
id
name
flavour_recipe (pivot)
flavour_id
recipe_id
-A User can have many recipes(one-to-many)
-A recipe can have many flavours and a flavour can be in many recipes.(many-to-many)
How i can get all recipes(with his flavours) from a given user?
I want use the Laravel Querybuilder, not Eloquent.
I was trying this, but its not working:
$allRecipesFromUserX = DB::table('flavour_recipe')
->join('flavours', 'flavours.id', '=', 'flavour_recipe.flavour_id')
->join('recipes', 'recipes.id', '=', 'flavour_recipe.recipe_id')
->join('users', 'users.id', '=', 'recipes.id')
->get();
If i understand your question correctly, you want all recipes for a given user.
If i take a look at your query, you are starting from the pivot table, i dont think this is the right approach. Also you do never declare a WHERE clause to limit it to the specific user.
Take it step by step. You want to get everything for a specific user, so lets start with the User table
$allRecipesForUser = DB::table('users')->where('id', $id)->get();
A User can have many recipes
$allRecipesForUser = DB::table('users')
->join('recipes', 'users.id', '=', 'recipes.user_id')
->where('id', $id)
->get();
And a recipe can have many flavours and a flavour can be in many recipes
$allRecipesForUser = DB::table('users')
->join('recipes', 'users.id', '=', 'recipes.user_id')
->join('flavour_recipe', 'recipes.id', '=', 'flavour_recipe.recipe_id')
->join('flavours', 'flavours.id', '=', 'flavour_recipe.flavour_id')
->where('users.id', $id)
->get();
I hope this works out for you. Let me know if this is not what you need.
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();
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 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');
I have an eloquent query where I am not getting the expected results and I was hoping someone could explain to me what the correct way to write the query.
I have three tables:
records (belongsToMany users)
users (belongsToMany records)
record_user (pivot)
The record_user table also has a column for role.
I attempt to get all the records where the user has the role of either singer or songwriter:
$results = User::find(Auth::user()->id)
->records()
->wherePivot('role', 'singer')
->orWherePivot('role', 'songwriter')
->get();
Below is how the SQL syntax is generated:
select `records`.*, `record_user`.`user_id` as `pivot_user_id`,
`record_user`.`record_id` as `pivot_record_id` from `records`
inner join `record_user` on `records`.`id` = `record_user`.`property_id`
where
`record_user`.`user_id` = '1' and `record_user`.`role` = 'singer' or
`record_user`.`role` = 'songwriter'
The results for singer role are what is expected: All records where the user is the singer. The problem is the results for the songwriter: I am getting ALL songwriters and the query is not constrained by the user_id. For some reason I was expecting the songwriter role to also be constrained by the user_id - what is the correct way to write this using the eloquent syntax?
Hmm..I think you need to use an advanced where clause.
$results = Auth::user()
->records()
->where(function($query) {
$query->where('record_user.role', '=', 'singer')
->orWhere('record_user.role', '=', 'songwriter');
})
->get();
It is Laravel issue. In my case I solve it like this:
$results = Auth::user()
->records()
->wherePivot('role', 'singer')
->orWherePivot('role', 'songwriter')
->where('user_id', Auth::id())
->get();
Or use advance where clause
If your trying to get records I would do something similar to this:
User::find(Auth::user()->id)
->records()
->where(function($q){
$q->where('records.role', 'singer')
->orWhere('records.role', 'songwriter');
})->get();