I'm trying to tease out a logical problem that I'm having and I didn't know where else to ask!
I have two Objects whose relationship I'm trying to describe; the User and the Game. So, right now, I have that a User belongs to many Games, and that a Game belongs to many Users. What I'm trying to describe is the special instance when a User owns a Game. Presumably, this would simply be a column in the table for the owner_id. I am, however, struggling to establish how I can represent this in Eloquent. Do I need to create a new Object for the Game owner? Or can I use some kind of User role to describe this?
Game
class Game extends Eloquent
{
protected $guarded = array();
public static $rules = array();
// Game belongsToMany User
public function users()
{
return $this->belongsToMany('User');
}
// Need to identify the owner user.
}
User
class User extends Eloquent
{
protected $guarded = array();
public static $rules = array();
// User belongsToMany Game
public function games()
{
return $this->belongsToMany('Game');
}
}
I'm having difficulty even figuring out how to ask this in a clear and concise way, so if there's any more detail needed, please don't hesitate to ask.
What you need is thid table: games_owners. This is a migration schema for it:
Schema::create('games_owners', function($table)
{
$table->increments('id');
$table->integer('user_id');
$table->integer('game_id');
$table->timestamps();
});
This would be your User model:
class User extends Eloquent
{
protected $guarded = array();
public static $rules = array();
// User belongsToMany Game
public function games()
{
return $this->belongsToMany('Game', 'games_owners', 'user_id');
}
}
And you game model:
class Game extends Eloquent
{
protected $guarded = array();
public static $rules = array();
// Game belongsToMany User
public function users()
{
return $this->belongsToMany('User', 'games_owners', 'game_id');
}
// Need to identify the owner user.
}
And then you'll be able to do things like this:
$user = User::find(1);
foreach($user->games as $game) {
echo $game->name;
}
Related
I have a scenario where User has a belongsToMany relation with PortalBreakdown, PortalBreakdown has a belongsTo relation with Portal. Portal has order column in it. I have a method listing_quota($id) in UserController which returns all breakdowns of the user. I want to sort these breakdowns based on order column of the portal. Below are the code of classes and a method I have tried.
class User extends Model {
protected $table = 'user';
public function listing_quota() {
return $this->belongsToMany('App\PortalBreakdown', 'user_listing_quota')->withPivot(['quota']);
}
}
class PortalBreakdown extends Model {
protected $table = 'portal_breakdown';
public function portal() {
return $this->belongsTo('App\Portal');
}
}
class Portal extends Model {
protected $table = "portal";
protected $fillable = ['name', 'description', 'order'];
}
Below is the method where I am trying to return sorted by order. I tried few things some of which can be seen in commented code but not working.
class UserController extends Controller {
public function listing_quota($id)
{
$user = User::with(['listing_quota' => function ($query) use ($id) {
// $query->sortBy(function ($query) {
// return $query->portal->order;
// });
}, 'listing_quota.portal:id,name,order'])->findOrFail($id);
// $user = User::with(['listing_quota.portal' => function ($q) {
// $q->select(['id', 'name',order']);
// $q->orderBy('order');
// }])->findOrFail($id);
return $this->success($user->listing_quota);
}
}
I also tried chaining orderBy directly after relation in Model class but that's also not working from me. Thank you in advance.
NOTE: I am using Laravel Framework Lumen (5.7.8) (Laravel Components 5.7.*)
I have Table Vendors (for test Auth relationship) and Table Vendor_users,
But i use Auth::user() it not relationship
And This Database
In Vendor model
protected $table = 'vendor_users';
public function Vendor_test(){
return $this->belongsTo(Vendor_test::class);
}
And Vendor_test model
protected $table = 'vendors';
public function Vendor(){
return $this->hasMany(Vendor::class);
}
from chat and your current table structure, you should have relation like this
in Vendor Model
public function vendor_contact()
{
return $this->belongsTo(Vendor_contact::class, 'vendor_contact_id');
}
in Vendor_contact Model
protected $primaryKey = 'vendContactId'; //check this
public function vendor()
{
return $this->hasOne(Vendor::class, 'vendor_contact_id');
}
Now use lazy eager loading for loading vendor_contact relationship
Auth::user()->load('vendor_contact');
dd(Auth::user());
As per the discussion and the table structure you have,
Add the relation function in your model vendor_users.
protected $table = 'vendor_users';
public function vendor_contact()
{
return $this->belongsTo(Vendor_contact::class, 'vendor_contact_id');
}
get the user with the vendor_contact and check
$user = Auth::user()->with('vendor_contact')->first(); // As you asked with for auth
//OR
$user = Auth::user()->load('vendor_contact'); // From the rkj answer as I found this good.
// OR
$user = Vendor::find(1)->with('vendor_contact')->first();
dd($user);
I'm trying to easily check if a given user_id is a friend of the current user so I can simply do a check within a blade #if statement. My friendship functionality is two-way, and accepting a friendship is not required.
I have seem to have some setup going, but am not confident that I'm doing it properly.
Friendship Table
Schema::create('friendshipLinks', function ($table) {
$table->increments('id');
$table->integer('userId_1'); // foreign key
$table->integer('userId_2'); // foreign key
$table->timestamps();
$table->foreign('userId_1')->references('id')->on('users')->onDelete('cascade');
$table->foreign('userId_2')->references('id')->on('users')->onDelete('cascade');
});
User Model (Trimmed)
class User extends Eloquent implements UserInterface, RemindableInterface {
use EloquentTrait, UserTrait, RemindableTrait;
protected $table = 'users';
// RELATIONSHIPS
public function posts() {
return $this->hasMany('Post');
}
// FRIENDSHIP
function friends() {
return $this->belongsToMany('User', 'friendshipLinks', 'userId_1', 'userId_2');
}
function friendsWith() {
return $this->belongsToMany('User', 'friendshipLinks', 'userId_2', 'userId_1');
}
}
Friendship Model
class Friendship extends Eloquent {
protected $table = 'friendshipLinks';
protected $fillable = ['userId_1', 'userId_2'];
}
At the moment I can get an array of all the users friends with (Two-way)
$friends = User::find($id)->friends;
$friendsWith = User::find(Auth::user()->id)->friendsWith;
$result = $friends->merge($friendsWith);
How can I easily implement something that will allow me to call a function that return a boolean like:
$isFriend = User::find($id)->isFriend($friend_id);
Here is the solution method in user model's class:
public function isFriend($friendId) {
return (boolean) $this->friends()->where('users.id', $friendId)->count();
}
I am trying to create one-to-many relationship with 2 models, User and Role. So, I would like that one User can only have one role, and the Role can be assigned to more than User. I tried to follow the offical tutorial on https://laravel.com/docs/5.1/eloquent-relationships#one-to-many, and ended up with this:
class User extends Model
{
public function role()
{
return $this->belongsToMany('App\Admin\Role');
}
}
class Role extends Model
{
protected $table = 'roles';
public function users()
{
return $this->hasMany('App\Admin\User');
}
}
Based on that link, I think the Role is the same thing as the Post, one Role can be assigned to many User. However, this doesn't quite work, here is how I tried to access the role name for a specific User
$role_first = $user->role; // Eloquent\Collection
$role_second = $user->role(); // Eloquent\Relations\BelongsToMany
$role_first->role_title // 'Undefined property: Illuminate\Database\Eloquent\Collection::$role_title'
$role_second->role_title // exception 'ErrorException' with message 'Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$role_title'
What is exactly wrong here?
In the User class
public function role()
{
// not $this->belongsToMany('App\Admin\Role');
return $this->belongsTo('App\Admin\Role');
}
Because you want a oneToMany not manyToMany relationship.
This should do it. Please give it a try
class User extends Model
{
public function role()
{
return $this->hasOne(App\Admin\Role::class);
}
}
// user belongs to one role
class Role extends Model
{
protected $table = 'roles';
public function users()
{
return $this->belongsTo(App\Admin\User::class);
}
}
User Model
class User extends Authenticatable{
public function roles(){
return $this->belongsTo('App\Admin\Role');
}
}
Role Model
class Role extends Model{
public function users(){
return $this->hasMany('App\Admin\User');
}
}
The user can have only one role, and the role can be assigned to many users.
Just make sure the relation
A User hasOne Role
A Role belongsToMany Users
so, in this condition
class User extends Model
{
public function role()
{
return $this->hasOne('App\Admin\Role');
}
}
class Role extends Model
{
protected $table = 'roles';
public function users()
{
return $this->belongsToMany('App\Admin\User');
}
}
and must check your user_id foreign constraint be 'unsigned' in your role migration table.
I have 3 models: User, Role, Tool where each user could have many roles, and each role could have many tools.
The many to many relationships work well in each case. I can access:
User::find(1)->roles
Tool::find(1)->roles
Role::find(1)->tools
Role::find(1)->users
My tables are:
users
id
name
roles
id
name
tools
is
name
role_user
id
role_id
user_id
role_tool
id
role_id
tool_id
In each model:
//In User Model
public function roles()
{
return $this->belongsToMany('Rol');
}
//In Role Model
public function users()
{
return $this->belongsToMany('User');
}
public function tools()
{
return $this->belongsToMany('Tool');
}
//In Tool Model
public function roles()
{
return $this->belongsToMany('Rol');
}
I need to get all the tools of a single user like: User::find(1)->roles()->tools. How can I do that?
Get all the roles of the user, then in a loop you get all tools of the role and merge them to an array where you store all tools.
$tools = array();
foreach(User::find(1)->roles as $role)
$tools = array_merge($tools, $role->tools->toArray());
This runs a query for every iteration, so for better performance you should use eager loading.
$tools = array();
foreach (User::find(1)->load('roles.tools')->roles as $role) {
$tools = array_merge($tools, $role->tools->toArray());
}
Now you can place this to a function called tools() in your User model.
public function tools()
{
$tools = array();
foreach ($this->load('roles.tools')->roles as $role) {
$tools = array_merge($tools, $role->tools->toArray());
}
return $tools;
}
You can call it like this: User::find(1)->tools().
I don't think that the framework has a better solution. One other method is to use the Fluent Query Builder and create your own query but I don't see how that would be better.
Define a hasManyThrough relationship in User::find(1)->roles()->tools
class User extends Eloquent {
public function tools()
{
return $this->hasManyThrough('Tool', 'Role');
}
}
Then you can access straight forward:
$user->tools