Here are the relationships:
A user has many skills, there is a join table user_skills. I need to search this table to return the profiles that have the particular skill. This is part of a bigger query that is being built, so that is why there is not a ->get() on here.
User Model
/**
* A user may have many skills
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function skills()
{
return $this->hasMany('App\Core\Platform\Models\UserSkill');
}
Below is the query that isn't doing what I need it to. I need it to return the users who have the particular skill, based on the ID being passed in the search (the $this->misc['search_skills'] value).
// Skills
$this->user = $this->user->whereHas('skills', function ($q)
{
foreach ($this->misc['search_skills'] AS $skill)
{
$q->orWhere('id', $skill);
}
});
Any thoughts as to what I am doing wrong or how I could execute this in a different way?
$skills = $this->misc['search_skills']; // assuming this is an array
$this->user = $this->user->whereHas('skills', function ($q) use ($skills)
{
$q->whereIn('id', $skills);
});
Any time you end up using orWhere multiple times on the same field, you should most likely be using whereIn.
Put the search skills into a variable ($skills), import that variable into your callback with use, then use whereIn.
Related
I have Three Models: Nationality, Nationality_Opportunity, Opportunity.
The Tables :
-------------------------------------------------------------------
nationalities | nationality_opportunities | opportunities
--------------------------------------------------------------------
id nationality_id id
name opportunity_id name
In Opportunity Model:
public function nationalities(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Nationality::class,'nationality_opportunities','opportunity_id','nationality_id');
}
In Nationality Model:
public function opportunities()
{
return $this->belongsToMany(Opportunity::class,'nationality_opportunities','opportunity_id','nationality_id');
}
-What do I want to do ?
I want to retrieve the opportunities records based on their selected nationalities which are sent as an array of ids through the request, so I want to check these ids in the pivot table in order to get their related opportunities records and display them in a Vue js component.
Scope Filter in Opportunity Model:
public function scopeWithFilters($query)
{
return $query->when(count(request()->input('categories', [])), function ($query) {
$query->whereIn('opp_cat_id', request()->input('categories'));
})->when(count(request()->input('nationalities',[])),function ($query){
$query->whereIn('nationalities.nationality_id', request()->input('nationalities'));
});
}
The parameter: nationalities = [1,2,3,5] .
Properties function in api controller:
public function opportunities()
{
$opportunities = Opportunity::withFilters()->get();
return PublicOpportunityResource::collection($opportunities);
}
Your Query is correct but you need to change the way you return the day please follow
$nationalities = [1,2,3,5]; //for temperory ids you can change it
$data= Nationality::with('opportunities')->whereIn('id', $nationalities)->get();
//return it as below
return response()->json($nationalities);
You can simply fetch opportunities with the code below
$nationalities = Nationality::with('opportunities')->whereIn('id', request()->input('nationalities'))->get();
And then you can access the opportunities by iterating over $nationalities or for the first row you can use $nationalities->first()->opportunities, not sure why you're using when in the callback function.
As per the edit you can make this query for your desired result
$nationalities = request()->input('nationalities');
$opportunities = Opportunity::with(['nationalities' => fn($q) => $q->whereIn('nationality_id', $nationalities)])->get();
So i have a films table and a conversations table and a pivot table film_conversation
Film (films)
'id'
'filmable_id',
'filmable_type',
Conversation (conversations)
'id'
'last_message_id'
FilmConversation (film_conversation)
'film_id'
'conversation_id'
I am wanting to create a GET request to grab all of the conversations that belong to that specific film, I have this query but unsure if I am grabbing it correctly and how would I write whats being returned in the response?
ConversationController:
/**
*
*/
public function conversations()
{
$this->user = Auth::user();
$film = Film::whereHas('conversations', function ($query) {
return $query->where('id');
})->get();
return $film;
}
I have an additional question, should you directly include this query into the request method or split it out into a private method and include it in to increase readability and clutter of the call? what would be the best practice? It's an endpoint I'm exposing for the front end.
first of all, you're not using the authenticated user, second, you're returning a collection of films that has any conversation, the query constraint isn't doing anything and you can just access $film->conversations to get the collection
public function conversations($id)
{
// Get all conversations for a specified film
return Film::find($id)->conversations;
// Get all conversations in all films that have a conversation
$films = Film::whereHas('conversations')->with('conversations')->get();
$conversations = $films->flatMap->conversations;
return $conversations;
}
Hope this helps
/**
* #param Film $film
* #return Collection|Conversations[]
*/
public function conversations(Film $film)
{
return $film->conversations;
}
in route file
Route::get('conversations/{film}');
this code will return json representation of conversations models collection https://laravel.com/docs/5.7/eloquent-serialization#serializing-to-json
or it return 404 if film id not exist.
I am trying to sort the serials by video views.
Relations:
The Serial has a hasMany relationship to series.
The Series has a hasMany relationship to episodes.
The Episodes has a hasOne relationship to video.
The Video has a hasMany relationship to viewcounts.
<?php
//sort method:
public function mostPopular()
{
$serials = Serial::with(['series.episodes.video' => function ($query) {
$query->withCount(['videoViews' => function($query) {
}])->orderBy('video_views_count', 'desc');
}])->get();
return $serials;
}
//Serial model:
public function series()
{
return $this->hasMany(Series::class);
}
//Series model:
public function episodes()
{
return $this->hasMany(Episode::class);
}
public function serial()
{
return $this->belongsTo(Serial::class);
}
//Episode model:
public function video()
{
return $this->hasOne(Video::class);
}
public function series()
{
return $this->belongsTo(Series::class);
}
//Video model:
public function videoViews()
{
return $this->hasMany(VideoView::class);
}
public function episode()
{
return $this->belongsTo(Episode::class);
}
?>
I expect the sorted serials by video views (series.episodes.video.videoViews), but the actual output is not sorted.
Laravel 5.8
PHP 7
This is a silly one actually but I've learnt that multiple ->sortBy on collections actually are possible with no workarounds. It's just that you need to reverse the order of them. So, to sort a catalogue of artists with their album titles this would be the solution...
Instead of :
$collection->sortBy('artist')->sortBy('title');
Do this :
$collection->sortBy('title')->sortBy('artist');
Because "With" queries run as seperate queries (not subqueries as previously suggested), exposing extrapolated fuax-columns from one query to the other gets rather tricky. I'm sure there's non-documented solution in the API docs but I've never come across it. You could try putting your with and withCount in the orderBy:
Serial::orderBy(function($query) { some combo of with and withCount })
But that too will get tricky. Since either approach will hit the database multiple times, it would be just as performant to do the separation yourself and keep your sanity at the same time. This first query uses a left join, raw group by and raw select because I don't want laravel running the with query as a separate query (the problem in the first place).
$seriesWithViewCounts = VideoView::leftJoin('episodes', 'episodes.id', '=', 'video_views.episode_id')
->groupBy(DB::raw('episodes.series_id'))
->selectRaw("episodes.series_id, count(video_views.id) as views")
->get();
$series = Series::findMany($seriesWithViewCounts->pluck('series_id'));
foreach($series as $s) {
$s->view_count = $seriesWithViewCounts->first(function($value, $key) use ($s) {
return $value->series_id = $s->id
})->video_views_count;
});
$sortedSeries = $series->sortBy('video_views_count');
This will ignore any series that has no views for all episodes, so you may want to grab those and append it to the end. Not my definition of "popular".
I'd love to see a more eloquent way of handling this, but this would do the job.
I've got two models, User and Seminar. In English, the basic idea is that a bunch of users attend any number of seminars. Additionally, exactly one user may volunteer to speak at each of the seminars.
My implementation consists of a users table, a seminars table, and a seminar_user pivot table.
The seminar_user table has a structure like this:
seminar_id | user_id | speaking
-------------|-----------|---------
int | int | bool
The relationships are defined as follows:
/** On the Seminar model */
public function members()
{
return $this->belongsToMany(User::class);
}
/** On the User model */
public function seminars()
{
return $this->belongsToMany(Seminar::class);
}
I am struggling to figure out how to set up a "relationship" which will help me get a Seminar's speaker. I have currently defined a method like this:
public function speaker()
{
return $this->members()->where('speaking', true);
}
The reason I'd like this is because ultimately, I'd like my API call to look something like this:
public function index()
{
return Seminar::active()
->with(['speaker' => function ($query) {
$query->select('name');
}])
->get()
->toJson();
}
The problem is that since the members relationship is actually a belongsToMany, even though I know there is only to ever be a single User where speaking is true, an array of User's will always be returned.
One workaround would be to post-format the response before sending it off, by first setting a temp $seminars variable, then going through a foreach and setting each $seminar['speaker'] = $seminar['speaker'][0] but that really stinks and I feel like there should be a way to achieve this through Eloquent itself.
How can I flatten the data that is added via the with call? (Or rewrite my relationship methods)
Try changing your speaker function to this
public function speaker()
{
return $this->members()->where('speaking', true)->first();
}
This will always give you an Item as opposed to a Collection that you currently receive.
You can define a new relation on Seminar model as:
public function speaker()
{
return $this->belongsToMany(User::class)->wherePivot('speaking', true);
}
And your query will be as:
Seminar::active()
->with(['speaker' => function ($query) {
$query->select('name');
}])
->get()
->toJson();
Docs scroll down to Filtering Relationships Via Intermediate Table Columns
Is it possible to have a hasMany relationship on two columns?
My table has two columns, user_id and related_user_id.
I want my relation to match either of the columns.
In my model I have
public function userRelations()
{
return $this->hasMany('App\UserRelation');
}
Which runs the query: select * from user_relations where user_relations.user_id in ('17', '18').
The query I need to run is:
select * from user_relations where user_relations.user_id = 17 OR user_relations.related_user_id = 17
EDIT:
I'm using eager loading and I think this will affect how it will have to work.
$cause = Cause::with('donations.user.userRelations')->where('active', '=', 1)->first();
I don't think it's possible to do exactly what you are asking.
I think you should treat them as separate relationships and then create a new method on the model to retrieve a collection of both.
public function userRelations() {
return $this->hasMany('App\UserRelation');
}
public function relatedUserRelations() {
return $this->hasMany('App\UserRelation', 'related_user_id');
}
public function allUserRelations() {
return $this->userRelations->merge($this->relatedUserRelations);
}
This way you still get the benefit of eager loading and relationship caching on the model.
$cause = Cause::with('donations.user.userRelations',
'donations.user.relatedUserRelations')
->where('active', 1)->first();
$userRelations = $cause->donations[0]->user->allUserRelations();
Compoships adds support for multi-columns relationships in Laravel 5's Eloquent.
It allows you to specify relationships using the following syntax:
public function b()
{
return $this->hasMany('B', ['key1', 'key2'], ['key1', 'key2']);
}
where both columns have to match.
I'd prefer doing it this way:
public function userRelations()
{
return UserRelation::where(function($q) {
/**
* #var Builder $q
*/
$q->where('user_id',$this->id)
->orWhere('related_user_id',$this->id);
});
}
public function getUserRelationsAttribute()
{
return $this->userRelations()->get();
}
If anyone landed here like me due to google:
As neither merge() (as suggested above) nor push() (as suggested here) allow eager loading (and other nice relation features), the discussion is still ongoing and was continued in a more recent thread, see here: Laravel Eloquent Inner Join on Self Referencing Table
I proposed a solution there, any further ideas and contributions welcome.
You can handle that things with this smart and easy way .
$cause = Cause::with(['userRelations' => function($q) use($related_user_id) {
$q->where('related_user_id', $related_user_id);
}])->where('active', '=', 1)->first();