I have a Laravel application with a MySQL database that has 3 tables: Publication, Comment and User.
When I display a publication to user I call WS GET Publication By Id and WP GET Comment By Publication Id to display all comments related to that publication.
I want to know if there's a way to avoid Calling WP GET User By Comment Id for each Comment, because when I display a comment I also need to display some information for user who commented.
Can I add multiple user Foreign Keys in table Comment and use them?
Thanks in advance.
This is a very generic usecase. Can you see if this answers your question.
Publication model{
whatever fields you have,
//Comments --> One to many relation with Comment model
public function comments()
{
return $this->hasMany('App\Comment');
}
}
Comment Model{
whatever fields you have,
//User --> One to one relation with User model
public function user()
{
return $this->hasOne('App\User');
}
}
User Model{
fields...
}
In your controller, you can get the publications with comments and users by using with function.
$pub = Publication::first()->with('comments.user');
I hope this helps.
Related
I know this question has been asked but my situation is different.
I have Post model with relationship to Comment model defined:
/*Post Model*/
public function comments(){
return $this->hasMany('comment');
}
and Comment model which each comment belong to one user :
/comment model/
public function user(){
return $this->belongto('user');
}
now I want to query all post and eager load comments (of each post) along with user information who post the comment.
anyway to make it work please ?
thank you.
What you want is nested eager loading, scroll down a bit and you will see it.
Quoting the docs:
To eager load nested relationships, you may use "dot" syntax. For
example, let's eager load all of the book's authors and all of the
author's personal contacts in one Eloquent statement:
$books = Book::with('author.contacts')->get();
In your case
$posts = Post::with('comments.user')->get();
I use laravel framework . I have two table. users and points . that has one to many relationship between them.in points table saved many records related to a one user that keep all of the ponits we assign to this user.I want to keep sum this points to one field as sumpoints filed in user table.how can i do it in laravel
Do something like this in your Points model:
public static function boot()
{
parent::boot();
self::created(function($model){
$user = Auth::user();
$user->sumfields = Points::where('user_id',$user->id)->sum('points');
$user->save();
});
}
The self::created function gets called after a new instance of that model has been created.
However if this was my project, I would probably do what Ivan Jelev suggested to you (doing the sum only when you need it).
I have this setup as my database structure which already works pretty well, but I feel like it could be better, but cannot figure out how.
Database Structure:-
Events can have multiple sub-events. Users can join these events and optionally their sub-events. With this structure I have all constraints, but the one that links the event attending with the sub event attendings => there can be sub event attendings left without having the main event attending (this shouldn't be possible as users who don't attend the main event cannot attend it's sub events).
I am currently working with Laravel so it looks like this:
User:
hasMany Event (as organizer of these events)
belongsToMany EventAttending (which events is a user joining)
belongsToMany SubEventAttending (which sub-events is a user joining)
Event:
belongsTo User
hasMany SubEvent
belongsToMany EventAttending (which users are attending this event)
SubEvent:
belongsTo Event
belongsToMany SubEventAttending (which users are attending this sub-event)
The problem arises when trying to manage the sub-event attendings of a user. How would I make this constraint between the event and sub-event attendings exist, while keeping it clean with the capabilities of Laravel / What can you give as advice for a better structuring / How would you guys do it?
Edit
To clarify, the events and sub-events store different things, have different properties. Also I need to store information for the event attendings (like did he attend in real life) and sub-event attendings, most likely differing ones.
Edit
Edward Haber has the better structure, but as my two pivot tables (the connection between User-Event and User-SubEvent) store additional diferent type of information, currently chose to remain with the initial design.
One of the biggest problems I am facing (which does exist with both structure) is querying for a User while getting the attended Events with the attended SubEvents. Trying to achive this result:
User[]:{
...,
attending_events[]:{
...,
attending_sub_events[]:{
...
}
}
}
Been thinking for hours for a clean solution, couldn't get anything else to work. Wouldn't like to write the whole SQL query manually. My current implementation with the two pivot table looks like this (result not nested, code messy):
$users = User::withCount(['attendingEvents' => function($query) use($eventId){
$query->where('event_id', $eventId);
}])
->with(['attendingSubEvents' => function($query) use($eventId){
$query->select('id')->whereHas('event', function($query) use($eventId){
$query->where('id', $eventId);
});
}]);
With this approach I get the sub-events separated from the main event. (querying only for the main attending-event count, because I only need to determine whether he is joining or not).
The pattern for this type of solution is to use a Polymorphic Relationship. This solution focuses just on connecting the User to the Event and SubEvent - the rest of the relations look correct.
<?php
class User extends Model {
public function attending() {
return $this->morphTo();
}
}
<?php
class Event extends Model {
public function attendees() {
return $this->morphMany('App\User', 'attending')
}
}
<?php
class Subevent extends Model {
public function attendees() {
return $this->morphMany('App\User', 'attending')
}
}
The users table needs these fields:
*users*
-----
id
attending_id
attending_type
This is all untested - but the basic idea. (Edited: changed name of polymorphic method to attending from attendee)
Hello i just realized how very confused i am by these relations, I have a question i need to ask. Assuming i have two table with records.
Roles
Administrator
Manager
Employee
Users
User 1
User 2
User 3
Now i want to create a relationship between these two tables, in this situation each user can have only one role but how do i express this relation?
To be more specific is it the user row in the table that can have only one role or the users table as a whole?
If it is the users table as a whole that can have only one role then in a situation where User 1 and User 2 are both administrators will the relationship then become many to many?
You might start by reviewing the documentation on relationships in Laravel/Eloquent: http://laravel.com/docs/5.1/eloquent-relationships
Your classes will look something like this:
class User extends Model
{
public function role()
{
return $this->hasOne('App\Role');
}
}
class Role extends Model
{
}
This means that each User has one Role - it doesn't prevent the same role from belonging to multiple users.
I seems it will become.
Role model
public function users()
{
return $this->hasMany('App\User');
}
User model
public function role()
{
return $this->belongsTo('App\Role');
}
so means, a Role can have many users and a user belongs to a role.
so User1 and User2 can be both Administrators. Check out laracast for additional info on relationship
I think, will be better, to use ManyToMany in Role-User reations, because, if you want now to use one role for one user, in future, probably, you will need to user more than one role to user. Also, ManyToMany not slower, than One-to-Many, but it more powerfull decision.
Role model
public function users()
{
return $this->belongsToMany('App\User');
}
User model
public function role()
{
return $this->belongsToMany('App\Role');
}
So I have a weird question.
I'm making an application in Laravel and Angular. I'm using Eloquent models.
Right now I have 2 tables, 1 users table (with the users info), and 1 events table (with the name, date, ... of an event).
These 2 are binded by a oneToMany relation (Every event belongs to an user). But now I would like people to sign up for that event. ( a switch in the front end, so they can apply for the event).
So, many users will have many events.
The catch is also, the owner of the event can manually set an maximum # users. (This is an tinyINt in my event table).
So now my question. How can I pull this off?
Options I have thought off:
1. I make a pivot table
-> Is this possible? Because the User and the event are already binded by a one to many realtion? So how can I bind them with a many to many relation again?
2. Every time a user signs op, I add his ID to an array in the events table.
-> Is this possible? I could then be able to set the maximum # users by checking the length of the array?
Thank you friends!
Using a pivot table is the correct approach here. The fact that the two tables are already bound by a different relation should not deter you.
class Events extends Eloquent
{
public function owner()
{
return $this->belongsTo(App\User::class);
}
public function subscribers()
{
return $this->belongsToMany(App\User::class, 'event_user');
}
public function canAddSubscriber()
{
if ( ! $this->max_users) return true;
return $this->max_users < $this->subscribers()->count();
}
}
As you can see, you can use the relationship to determine if a subscriber can still be added.