Laravel: Get all projects and check if user has liked them - laravel

I want to get all projects, and foreach project I want to get a property for example is_liked which indicates that if the current_user (I've his Id) has liked this project or not.
User Model
class User extends Model
{
public function projects()
{
return $this->hasMany(Project::class);
}
public function favorite_projects()
{
return $this->belongsToMany(Project::class, 'favorite_projects', 'user_id', 'project_id')->withTimestamps();
}
}
Project Model
class Project extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
User Table
- id
- name
Project Table
- id
- user_id
- title
favorite_projects table
- user_id
- project_id
What is the best way to get this done ? the data I want to get is something like this:
[
{
"id": 1,
"user_id": 3,
"title": "my title",
"is_liked": true
}
]
Note: the current_user->id might or might not be same as the project->user_id.

$projects = DB::table('projects')
->join('favorite_projects', 'favorite_projects.project_id', '=', 'projects.id')
->select('projects.*', 'favorite_projects.*')
->where('projects.user_id', Auth::id())
->get();
This join statement allows you to join 2 tables together and for each project that is being favorited by the user, you will see the favorite record attached with the record.
Perform a dd($projects) after that SQL query and see what the result is like, and then for each result, you set $project['is_liked'] accordingly.

Related

How to add to my Index/Show, so that it displays a table, from another table. (Laravel)

First post here, hope all is well with everyone! I am working on my senior project and struggling with this concept. Maybe it isn't possible, or it is and I'm thinking about it the wrong way.
I currently have this- This displays the student, with all its foreign keys in the other tables. In settings, there is a foreign key 'conversion_id.', I would like settings on the student call(code below) to ALSO display the conversion table, from the FK in settings.
$student = Student::with('studentIntroSurveys', 'settings', 'giftsSurveyResults',
'studentGiftSurveys', 'devotionals', )->get();
If this does not make sense, I am sorry. I am still l new to the language.
(Code to display students. It shows settings, but not the conversions within settings.)
$student = Student::with('studentIntroSurveys', 'settings', 'giftsSurveyResults',
'studentGiftSurveys', 'devotionals', )->get();
if (!$student) {
return response('No Data', 400);
} else {
return response($student);
}
I would like it to display this settings, but WITH the conversion_id table!!
What it displays =
settings: { setting_id: 4, student_id: 1, dark_mode: 1, conversion_id: 1,
notification_enabled: 1, notification_time: "08:00:00" } `
my has-one method =
public function settings()
{
$settings = $this->hasOne(Settings::class, 'student_id', 'student_id');
return $settings;
}
You can use the dot syntax for nested eager loading: 'settings.conversation' e.g.
$student = Student::with('studentIntroSurveys', 'settings.conversation', 'giftsSurveyResults', 'studentGiftSurveys', 'devotionals', )->get();
Alternatively, you could set up a belongsToMany relationship on the Student model and use settings as the pivot table:
public function conversations()
{
return $this->belongsToMany(Conversation::class, 'settings')
}
Just a few FYIs:
Your if statement is never going to return the 400 response as $student is always going to be a collection. You could instead do if ($student->isEmpty()).
I would also recommend changing the variable to $students as it will be a collection of students rather than a single student.
You can simple your settings relationship by removing the temporary variable and just returning the relationship:
public function settings()
{
return $this->hasOne(Settings::class, 'student_id', 'student_id');
}

Eloquent Api Resource: show one column from pivot table

I have a vehicle database with a many to many relation with my variant and country table. I only want to show the title that's in my pivot (countries_variants) table. But when I set the relation in my api resource file, it shows all the columns from variants, countries_variants and countries table. Is there a way to only get the title from the pivot table?
Here is my Variant Model:
public function countries(): BelongsToMany
{
return $this->belongsToMany(Country::class, 'countries_variants')->withPivot('title');
}
VariantResource.php
public function toArray($request)
{
return [
'title' => $this->countries->first->title,
];
}
VariantController.php
public function index()
{
return new VariantCollection(Variant::paginate(50));
}
The output I'm getting is:
{
"data": [
{
"title": {
"id": 1,
"title": "Nederland",
"country_code": "NL",
"language_code": "NL_nl",
"pivot": {
"variant_id": 1,
"country_id": 1,
"title": "3/5 deurs"
}
}
}
]
}
I just want to show "title": "3/5 deurs" and not the other data.
I thought that if I set withPivot('title') in my model, it will only show that title and not the foreign keys (variant_id and country_id). Apparently thought wrong..
I tried adding this as well:
'variant_title' => $this->whenPivotLoaded('countries_variants', function () {
return $this->pivot->title;
}),
But the data then returns empty.
Any help would be very much appreciated :)
Try changing
$this->countries->first->title
To
$this->countries->first->title->pivot->title
When you do withPivot('title') you are telling Eloquent to also get title column, if you do not do that, you will only get the keys (variant_id and country_id in your case).
More info here.
Okay so I finally figured it out. I looked through the official laravel docs again and I found this.
So I changed my CountryVariant model class to extend Pivot instead of Model.
Like so:
use Illuminate\Database\Eloquent\Relations\Pivot;
class CountryVariant extends Pivot {
public function variant(): BelongsTo
{
return $this->belongsTo(Variant::class);
}
}
Now in my VariantResource.php I added this:
public function toArray($request): array
{
$items = [];
foreach ($this->countries()->get() as $country) {
$items[$country->language_code] = $country->pivot->only('title');
}
return $items;
}
I also changed the pivot table names to be singular like: country_variant (I had it plural).
Apparently the problem was that in my model it was looking for a primary key which I didn't have in my pivot table. I only had foreign keys and an additional column: 'title'. It took one of my foreign keys and used it as a primary one. So when I extended with extends pivot it would ignore the primary key and I could collect my alternate column 'title' by using country->pivot->only('title') in my resource file.
I hope this helps some one else.

Retrieve grouped relation with Eloquent ORM

I have a relationship which looks like this:
User
- id
Menu
- id
- appetizer_id
- main_course_id
- dessert_id
User_Menu
- user_id
- menu_id
and corresponding models:
class User
{
public function menus()
{
return $this->belongsToMany('App\Models\Menu', 'user_menu', 'user_id', 'menu_id');
}
}
class Menu
{
public function dessert()
{
return $this->belongsTo('App\Models\Dessert', 'dessert_id');
}
}
In other words there are users and menus with a many to many relationship. Now I would like to retrieve all the distinct desserts for a given user. I've tried using group by, but it does not allow me to select and group by a single column, because the pivot columns are always included in the query:
$desserts = User::find(1)->menus()
->select('dessert_id')
->groupBy('dessert_id')
->with('dessert')
->get();
throws the following error:
'User_Menu.user_id' isn't in GROUP BY
$desserts = User::join('User_Menu', 'User.id', '=', 'User_Menu.user_id')
->join('Menu', 'User_Menu.menu_id', '=','Menu.id')
->get();

How can I access model Relation with where condition of child and parent table in laravel?

I have Games table and which has the following schema
id | status | name
status column has 2 values (Active, Pending)
And GamePlayer table which has the following schema
id | game_id | player_id | request_status
request_status column has 3 values (Pending, Confirm, Rejected)
Now I have to select all game in which the player is involved but with the following constraints:
If the game is Pending state then it will be shown to all game_players
If the game is in Active state then it will be only shown to the game_player whose request_status is Confirm.
Game(Model)
public function GamePlayer()
{
return $this->hasMany('App\Models\GamePlayer', 'game_id', 'id');
}
public function getGames($playerId)
{
$gameList = Game::with(['GamePlayer','Category:id,name'])
->whereHas('GamePlayer', function ($q) use ($playerId) {
$q->where('player_id', $playerId);
})->get();
return $gameList;
}
Controller
$this->gameObj = new Game();
$gameList = $this->gameObj->getGames($player_id);
Please help me out how can I populate data from another table based on condition(parent table as well as the child).
You can use condition in your relationship to get only confirmed game players
public function activeGamePlayers()
{
return $this->hasMany('App\Models\GamePlayer', 'game_id', 'id')
->where('request_status', 'confirm');
}
You can as well use scopes (https://laravel.com/docs/7.x/eloquent#local-scopes) to select only active games
public function scopeActive($query)
{
return $query->where('status', 'active');
// You can then use $game->active()->...
}
So your getGame would look like:
public function getGames($playerId)
{
$player = GamePlayer::find($playerId);
if ($player->status == 'active') {
$gameList = Game::with(['GamePlayer', 'Category:id,name'])
->get();
} else {
$gameList = Game::with(['GamePlayer', 'Category:id,name'])
->active()->get();
}
return $gameList;
}
NOTE:
in your specific case, I would instead get started from the GamePlayer Model to get the games, instead of coming from the Game Model - you can use scopes and conditions in relationships as well to make your code more readable.
You can use Laravel method whereHas. You should read this part of Laravel the documentation.
Note that you should have relationships declared on every model.

Get Collection With HasOne Relationship

In my User model in Laravel 5.2 I have a relationship setup with their status to the company.
public function companyStatus()
{
return $this->hasOne('CompanyUser')->select('status');
}
The CompanyUser table has a company_id, user_id, and status field
Then in my controller I do the following:
$company = Company::find($company_id);
$users = CompanyUser::where('company_id', $company_id)->pluck('user_id')->toArray();
$user_data = User::with('companyStatus')->find($users);
but when I dump the user_data array it has all of the users related to the company, but just shows null for their status relationship
{
"id":2,
"name":"Moderator",
"email":"mod#company.com",
"created_at":"2016-09-08 15:26:20",
"updated_at":"2016-09-08 15:26:25",
"company_status":null
}
If I however return just the User collection to the view, and iterate over each user and run
$user->companyStatus->status
the value displays, but I am trying to include this within the collection for a JSON API to consume.
UPDATE
I tried adding the foreign key to the select call on my relationship method:
public function companyStatus()
{
return $this->hasOne('CompanyUser')->select('status', 'user_id');
}
and it now returns the following:
{
"id":2,
"name":"Moderator",
"email":"mod#company.com",
"created_at":"2016-09-08 15:26:20",
"updated_at":"2016-09-08 15:26:25",
"company_status": {"status":"1","user_id":"2"}
}
Not sure if this is the best/correct method or not though.
Okay I figured it out.
I tried adding the foreign key to the select call on my relationship method:
public function companyStatus()
{
return $this->hasOne('CompanyUser')->select('status', 'user_id');
}
Then that returns:
{
"id":2,
"name":"Moderator",
"email":"mod#company.com",
"created_at":"2016-09-08 15:26:20",
"updated_at":"2016-09-08 15:26:25",
"company_status": {"status":"1","user_id":"2"}
}
Without the foreign key Laravel obviously can't determine the related data on the other table.

Resources