I have an 'implementation' table that contains relationships to retrieve projects and scores.
The scores table contains a" user_id "field.
I would like to collect all the implementations but with only the score which contains the user_id.
My original query.
public function getImplementations($id, $student)
{
$data = Implementation::where('event_id', $id)->where('student_id', $student)->with('project', 'score')->get();
return response()->json($data);
}
My test for get only the score from specific user (only one score per user per implementation, so no conflict possible)
$data = Implementation::where('event_id', $id)
->where('student_id', $student)->with('project', 'score')
->whereHas('score', function ($query) {
$query->where('user_id', Auth::user()->id);
})->get();
But that does not give the expected result. This is not the right method and I do not know the right one.
Thanks for your help
I am not sure if there's a need to eager-load and constrain a relationship simultaneously. Does the following provide you with the expected result?
$data = Implementation::where('event_id', $id)
->with([
'project',
'score' => function ($query) {
$query->where('user_id', Auth::id());
}
])
->where('student_id', $student)
->get();
Related
Help me please.
I'm trying to write a function where I get all the categories of my forum with the 3 most recently updated Topics in the given categories.
But according to the result, take(3) filters by id (where the id is not higher than 3), and I need to get the last 3 records.
public function index()
{
$forums = Category::with(['posts' => function ($q){
return $q->take(3)->get();
}])->get();
dd($forums);
}
you should order your complete query by update_at descending, only after you can take the first 3.
$q->orderBy('update_at', 'desc')->take(3)->get();
Your Categories table seems to be a different table from posts, so when a post is created or updated you should also set update_at of its category to now.
As far as I know you can not use take() or limit() inside with();
EDIT: solution that was selected by mr.Sardov is to use package staudenmeir/eloquent-eager-limit.
Link is provided below this answer.
So for you need to do is by limit it from model relationship.
For example:
class Category extends Model {
public function posts()
{
return $this->hasMany('App\Models\Post');
}
public function limitPosts()
{
return $this->hasMany('App\Models\Post')
->limit(3);
}
public function limitLatestPosts()
{
return $this->hasMany('App\Models\Post')
->orderBy('created_at', 'desc'). // or use ->latest()
->limit(3);
}
}
And that use it like this:
Category::query()
->with(['limitPosts' => function($query) {
$query->orderBy('created_at', 'desc'); // the last records
}])
->where('id', '<=', 3) // id not higher than 3
->get();
Or
Category::query()
->with('limitLatestPosts')
->where('id', '<=', 3) // id not higher than 3
->get();
Hope this can help you out.
I have a one to many relation between Person and Visit tables like this:
public function visits()
{
return $this->hasMany('App\Models\Visit');
}
And want to get the persons who has a sickness_id of 1 in the relation like this:
$persons = Person::whereHas('visits', function ($query) {
$query->where('sickness_id', 1);
})->get();
And it works fine but I want to search just last visit of each person.
I mean if a person has two visits, one with sickness_id of 1 and other with sickness_id of 2, do not return this person because last visit is sickness_id of 2.
you can use hasOne relation for that:
public function lastVisit()
{
return $this->hasOne('App\Models\Visit')->latest();
}
then you can load it:
$persons = Person::whereHas('lastVisit', function ($query) {
$query->where('sickness_id', 1);
})->get();
I think the above answer is gonna work. Using addSelect, this might also work:
Person::addSelect('last_visit_id', Visit::select('id')
->whereColumn('person_id', 'persons.id')
->latest()
->limit(1)
)
->where('last_visit_id', 1)
->get();
$person = modelname::query()
->where(['sickness_id' => '1' ])
->select('visits')
->orderBy('id', 'DESC')
->first();
I have the following query:
$countries = Country::where('code', '=', $code)
->with(array('cities.details' => function ($query) use ($user) {
$query->where('cities.name', '=', 'This is a city name');
}))->first();
Lets see the example: I have three tables, Country, City and CityDetails. I want to get all countries, then get all cities (INCLUDING the details information) but I also want to filter cities by name and fetch details table which belongs to the city table.
If I want to use with to get the child and another table using with(cities.details), how can I filter using CITIES attributes?
The main question is: How can I fetch two tables in a with statement like secondTable.OtherTable and filter the query using secondTable attributes?
Just for make it clearer, if I use statement like this:
$countries = Country::where('code', '=', $code)
->with(array('cities.details' => function ($query) use ($user) {
$query->where('name', '=', 'This is a detail name');
}))->first();
I can access only details table attributes. The question is: How can I access the city table attribute to filter inside a with a statement?
Thanks in advance
I just found a solution. Basically, I should apply the filter for city table and then call wit function on the subquery. This code solved my problem:
$countries = Country::where('code', '=', $code)
->with(array('cities' => function ($query) use ($user) {
$query->where('name', '=', 'San Francisco')->with('details');
}))->first();
Note that I called with('details') on city only after filter in subquery.
The most straightforward one is using join query:
(new Country)
->join('cities', 'cities.id', '=', 'countries.city_id')
->join('city_details', 'cities.id', '=', 'city_details.city_id')
->where('cities.name', 'TheName')
->where('city_details.info', 'info')
->select('...');
But the result has only one level. So,
$query = (new Models\Country())
->with([
'cities' => function ($query) {
$query->where('name', 'xxx')->with([
'details' => function ($query) {
$query->where('info', 'info');
}
]);
}
]);
$result = $query->get()->filter(function ($item) {
return count($item['cities']);
});
The result gives countries with empty cities. So use Laravel collection to filter in the last.
I have one to many relation based two tables users and games and there is also bridge table users_games (linking user_id to games).
I want to fetch a single record from games table based on provided game_id for specific user. I did some research and found whereHas() which is returning all games which are belongs to specific user. But I need to fetch one based on game_id. Can some one kindly let me know how can I fix issue in below script
$GameInfo = User::with('games')->whereHas('games', function ($query) use($request)
{
$query->where('game_id', '=', $request->game_id);
})->find(request()->user()->id);
Is this what you're trying to do?
$GameInfo = $request
->user()
->games()
->where('game_id', $request->game_id)
->first();
try this:
$GameInfo = User::with(['games' => function ($query) use($request)
{
$query->where('game_id', $request->game_id);
}])->whereHas('games', function ($query) use($request)
{
$query->where('game_id', '=', $request->game_id);
})->find(request()->user()->id);
If your relation 'games' is a hasMany() with table 'users_games', You can try this code
$GameInfo = User::with(['games' => function ($query) use($request)
{
$query->where('game_id', $request->game_id);
}])
->where('users.id', <user_id_variable>)
->first();
And the relation 'games' in User Model as
public function games()
{
return $this->hasMany('App\Models\UserGames', 'user_id', 'id');
}
I'm using Laravel 4 and Eloquent ORM. I'm currently pulling records from my database using the following code:
public function index($type, $id)
{
$asset = Asset::where('public_id', '=', $id)
->with('attachments')
->with('attachments.attachment')
->with('tracks')
->with('tracks.track')
->where('locale', '=', Config::get('app.locale'))
->first();
return View::make('view-asset.index')
->with('asset', $asset)
->with('type', $type);
}
The table joined to this query using the with('tracks') statement has a column in it called track_order - I would like to be able to sort the rows returned by this part of the query by that column so that the tracks stored in the database are returned in the correct order.
Is it possible to do this and if so, how should I do it? So far I've tried something like this:
public function index($type, $id)
{
$asset = Asset::where('public_id', '=', $id)
->with('attachments')
->with('attachments.attachment')
->with('tracks')
->with('tracks.track')
->where('locale', '=', Config::get('app.locale'))
->orderBy('tracks.track_order', 'ASC')
->first();
return View::make('view-asset.index')
->with('asset', $asset)
->with('type', $type);
}
Which doesn't work and I can't figure out a way of doing this other than splitting things up into multiple queries.
You most certainly can:
with(['tracks' => function($query)
{
$query->orderBy('track_order', 'asc');
}])
Refer to Eager Loading Contraints in the documentation for more.