Laravel - add key/value to response if ids are the same - php

I have a contents table with a lot of contents created by different users… I’m trying to verify if a content is created by the user that is logged in and then add a new key value pair to the response, so if the content was created by user_id 1 and I’m user 1 then the response has a field like isOwn : 1 on the content object that is his own, but nothing if the content was created by someone else.
this is the criteria I have:
$query = $model->select('contents.*', 'status_types.name as status', 'content_types.name as content_type')
->join('status_types', 'status_types.id', '=', 'contents.status_type_id')
->join('content_types', 'content_types.id', '=', 'contents.content_type_id')
->with('platforms')
->with('classifications')
->withCount(['favorite' => function ($q) {
$q->where('user_id', '=', $this->request->user()->getIdentifier());
}])
->withCount('likes')
->inRandomOrder();
return $query;
I've tried to use when and whereExist but this return me only the ones created by the user, not if the condition is true.
Any ideas? thanks in advance.

You need to join on the model and make a where for the total query:
$query = $model->select('contents.*', 'status_types.name as status', 'content_types.name as content_type')
->join('status_types', 'status_types.id', '=', 'contents.status_type_id')
->join('content_types', 'content_types.id', '=', 'contents.content_type_id')
->join('favorites', 'favorites.contents_id', '=', 'contents.id') // or whatever the relation is
->with(['platforms', 'classifications'])
->withCount(['favorite'])
->withCount('likes')
->where('favorites.user_id', '=', $this->request->user()->getIdentifier())
->inRandomOrder();
return $query;

Related

Laravel how to get only one record per user

In my laravel-application, I want to display all users/candidates, which have taken an education. Now it might happen, that a user/candidate has taken more than one education, so in this case, just the latest education should be displayed.
I've come to this:
$users = User::whereHas('roles', function ($q) {
$q->where('slug', 'candidate');
}
)->whereHas('educations')
->join('user_educations', 'user_educations.user_id', '=', 'users.id')
->join('educations', 'user_educations.education_id', '=', 'educations.id')
->join('education_levels', 'user_educations.education_level_id', '=', 'education_levels.id')
->select('users.id', 'users.name', 'users.surname', 'users.status', 'users.city', 'users.zipcode', 'users.birthday', 'users.avatar', 'educations.title as education', 'education_levels.title as education_level')
->where('user_educations.user_id', '=', 'users.id') // HERE IT FAILS - returns null
->first();
return response(['success' => true, "users" => $users], 200);
when I leave out the where('user_educations.user_id', '=', 'users.id')-clause, and do get() instead of first(), I get all users/candidates with educations, and also sometimes the same user multiple times, depending on how many educations he has taken.
how can I fix this?
The distinct method allows you to force the query to return distinct results, you can use it like this;
$users = DB::table('users')->groupBy('user_id')->distinct()->get();
If you want to take just the latest record, you should use "orderBy" to order your records, than pick a record with something like this :
$users = DB::table('users')->groupBy('user_id')->orderBy('created_at','desc')->distinct()->get();
You can look here for more detailed information about query builders in Laravel

Get datas from relation table (with()) with a condition

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();

cant get the data I want from two different tables using Laravel

I have a table called instructor_class: user_id, class_id and I have another table classes: id, time, active.
I would like to show classes for a single user but only those classes that active is 0 or 1.
My current code looks like this:
return InstructorClass::with('classes.session')->where('user_id', '=', $userId)->get();
This code is displaying me everything, then I tried the following code:
$active = 1;
return InstructorClass::with(['classes' => function ($q) use ($active) {
$q->where('active', '=', $active); // '=' is optional
}])
->where('user_id', '=', $userId)
->get();
This again returns me same records, but of course the class property is null for each record, which at some point looks correct, but my point is if the 'active' field does not corresponds at the classes table do not show the record, seems like the where() stm within with() is optional..
I am kinda stuck here...
Would appreciate your help, opinions!
You can use ::has('classes') to only return the models that have related classes
return InstructorClass::has('classes')->with(['classes' => function ($q) use ($active) {
$q->where('active', $active);
}])
->where('user_id', '=', $userId)
->get();
Never thought it could be this simple:
return InstructorClass::with('classes.session')
->join('classes', 'classes.id', '=', 'instructor_class.class_id')
->where('classes.active', '=', 1)
->where('user_id', '=', $userId)
->get();

Eloquent - where has pivot users orWhere has none

I'm trying to write a function that will get all "buckets" that are assigned to the auth'd user and/or buckets that have NO USERS assigned.
Relations and such, work as they should. Unless I'm missing something?
How can I get all buckets user is assigned too - and also include buckets where no users (including the auth user) are assigned.
Buckets user is assigned to
Buckets where NO users have been assigned. i.e. pivot table contains no rows for bucket, etc.
My issue very likely stems from the orWhere query...
$buckets = Team::currentTeam()->buckets()->with('user')->whereHas('user', function($query) {
$query->where('user_id', Auth::user()->id)
->orWhere('user_id', function() {
$query->count();
}, '<', 0);
})->get();
Didn't tested this but I think this should work. You're looking to remove that orWhere query and add orHas('user', '=', 0).
$buckets = Team::currentTeam()->buckets()->with('user')->whereHas('user', function($query) {
$query->where('user_id', Auth::user()->id);
})->orHas('user', '=', 0)->get();
Another possible solution you might consider is using a left join.
Heads-up: this might not be accurate as I don't know your db schema.
Team::currentTeam()
->buckets()
->leftJoin('users', 'users.bucket_id', '=', 'buckets.id')
->where(function($query) {
$query->where('users.id', $user_id)
->orWhereNull('user.id');
});
Please also check this blog post
AND-OR-AND + brackets with Eloquent
https://laraveldaily.com/and-or-and-brackets-with-eloquent/

Selecting and ordering data from a query using Laravel Eloquent

I am trying to use Laravel and eloquent to return results based on the following query.
$blogPosts = BlogPosts::with('blog_user', 'blog_categories', 'blog_comments', 'tags')
->orderBy('created_at', 'desc')
->paginate(5);
Ok, so that is fine, it return exactly what it should, all the blog posts with associated relations to other tables.
What I now want to do is return only the $blogPosts where a tag is clicked by the user. So let's say there is a tag "PHP", so I pass in that value as $tag to the method. I then have something like this.
public function tag_search($tag)
{
$blogPosts = BlogPosts::with('blog_user', 'blog_categories', 'blog_comments', 'tags')
->where('tags', $tag)
->orderBy('created_at', 'desc')
->paginate(5);
$categories = BlogCategories::with('blog_posts')->get();
$data = array('blogPosts' => $blogPosts, 'categories' => $categories,
);
return view('blog.index')->with($data);
}
Now my issue is actually relatively simple I guess, if the where clause was a column in the BlogPosts table it would work, I know this because I tried that.
However the above won't work as is, I can only use;
->where('x', y)
Where x is a field in the BlogPosts table. I want to return a set of values where the submitted $tag is the same as one associated to the tags attached to the blog posts.
Make sense? I think I am over thinking it the point I am just not thinking now :)
Add the column field 'tags' to your table and then these queries:
$blogPosts = BlogPosts::with('blog_user', 'blog_categories', 'blog_comments', 'tags')
->where('tags', '=', $tag)
->orderBy('created_at', 'desc')
->paginate(5);
$categories = BlogCategories::with('blog_posts')->get();
return view('blog.index', compact ('categories', 'blogposts'));
Ok so the answer was to use whereHas like this.
$blogPosts = BlogPosts::whereHas('tags', function ($query) use ($tag) {
$query->where('name', $tag);
})->orderBy('created_at', 'desc')
->paginate(5);
That returns a search based on the associated elements.

Categories