I try load relations like:
return $task->load('users', 'creator', 'attachments', 'messages.creator');
Relation 'users' many to many with some fields
user
id
name
...
task
id
name
...
task_user
user_id
task_id
task_status_id
my pivot model
<?php
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class TaskUser extends Pivot
{
public $incrementing = true;
public function status()
{
return $this->hasOne(TaskStatus::class);
}
}
How to correctly load 'status' relation?
If Laravel provided a way to pass Closures in pivots it wouldn't be hard at all however there are 2 solutions at the moment to load pivot relationships
1. Lazy eager load everything.
# App\Task.php
public function users()
{
return $this->belongsToMany(App\User::class)->using(App\TaskUser::class)->withPivot('task_status_id');
}
$task->load('users', ...);
$task->users->each(function ($user) {
$user->pivot->load('status');
});
The problem: This makes a query for each User related to Task. Unless you paginate your results, it's the classic N+1 problem.
2. Use hasMany and belongsTo instead.
Define $user->tasks_user relationship (User hasMany TaskUser)
Define $task->tasks_user relationship (Task hasMany TaskUser)
Define the inverse (TaskUser belongsTo User, TaskUser belongsTo Task)
Define the last task_user relationship (TaskUser hasOne TaskStatus) (looks like a belongsTo to me though)
Load relationships $task->load('task_user.user', 'task_user.status')
There's no real problem with this implementation besides not using belongsToMany.
Related
I am using laravel framework to develop apis.we are not using migration files instead of that one we are hardcoding table name in models,is there any chance i can use relationships without migrations.
Let’s consider user hasMany books in this scenario how can i relate them in model.
Users table
u_id ,u_name ,u_role
books table
b_id, b_uid ,b_name
Note:- u_id is related to b_uid
Migrations and models are not related in any way. Migrations are needed to make changes to the database structure. If you change the database structure in any other way, this should not interfere with the use of models and relationships
class Book extends Model
{
protected $primaryKey = 'b_id';
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'b_uid', 'u_id');
}
}
class User extends Model
{
protected $primaryKey = 'u_id';
public function books(): HasMany
{
return $this->hasMany(Book::class, 'b_uid', 'u_id');
}
}
I have 2 models: User and Role.
A user can have many roles.
A role can have many users.
I have a custom pivot model between these 2 models. This custom pivot model only exists because it uses a trait that listens/logs for events such as created, updated, deleted.
Let's say I have a role called moderator. When I attach() (or detach()) that role to 5 users, it does successfully fire 5 created (or deleted) events for the pivot table.
$roleModerator->users()->attach($anArrayOfFiveUsersHere);
So far so good.
My problem is the following: when I delete the moderator role itself, it does delete all pivot rows associated to the role, but it does not fire any deleted event for each deleted pivot rows.
Expected behavior: I want Laravel to fire deleted events for each rows it deletes in the pivot table when I ask it to delete the role.
Environment: PHP 7.3 / Laravel 6
One weird thing I noticed, if I add this to my Role model :
public static function boot()
{
parent::boot();
static::deleting(function (self $model)
{
//$model->users()->detach(); // <-- this fails firing deleted events.
//MyCustomPivot::query()->where('role_id', $model->id)->get()->each->delete(); // <-- this fails firing deleted events.
$model->users()->sync([]); // <--- this works!
});
}
sync([]) will work great and fire as many deleted events as it deletes pivot rows.
but detach(), although it accomplishes the same thing, won't fire any deleted event. Why is that? They are both from InteractisWithPivotTable.php and sync() does even call detach() itself!
Not 100% sure it's applicable to your situation, but according to this issue on Github, you need to do some setup in your models.
First, make sure you have a primary key column in your table and defined on your pivot model.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class MyCustomPivot extends Pivot
{
public $primaryKey = "id";
public $incrementing = true;
}
Second, make sure you include your custom pivot model in your relationships.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function users()
{
return $this->belongsToMany(User::class)
->using(MyCustomPivot::class)
->withPivot('id');
}
}
class User extends Model
{
public function roles()
{
return $this->belongsToMany(Role::class)
->using(MyCustomPivot::class)
->withPivot('id');
}
}
When I use belongsTo without default keys it won't connect..
My users table has user_id which is some rand and unique string that presents that user. My urls table has user_id and uri columns where user_id contains users table user_id.
In Url model I have:
public function user() {
return $this->belongsTo('App\User','user_id','user_id');
}
In User model I have:
public function uri() {
return $this->hasOne('App\Url', 'user_id', 'user_id')->first()->uri;
}
By using $user->uri() I get uri from urls table connected with user_id.
But when I use $url->user() I get return null or BelongsTo class inside laravel tinker.
Anyone know why?
Your User::uri() method is not a relationship method.
You are utilizing a relationship method inside of it, but you are querying it for the first result and returning the uri property.
Your Url::user() method IS a relationship method because you are actually returning a relationship (BelongsTo) instance. Eloquent relationships are used as follows:
// To get a related entity on a BelongsTo relationship, you access it as a property:
$url->user
// To query a relationship, you use it as a method:
$url->user()->where(...)->first();
Can any body tell me what is the main difference between
the BelongsTo and HasOne relationship in eloquent.
The main difference is which side of the relation holds relationship's foreign key. The model that calls $this->belongsTo() is the owned model in one-to-one and many-to-one relationships and holds the key of the owning model.
Example one-to-one relationship:
class User extends Model {
public function car() {
// user has at maximum one car,
// so $user->car will return a single model
return $this->hasOne('Car');
}
}
class Car extends Model {
public function owner() {
// cars table has owner_id field that stores id of related user model
return $this->belongsTo('User');
}
}
Example one-to-many relationship:
class User extends Model {
public function phoneNumbers() {
// user can have multiple phone numbers,
// so $user->phoneNumbers will return a collection of models
return $this->hasMany('PhoneNumber');
}
}
class PhoneNumber extends Model {
public function owner() {
// phone_numbers table has owner_id field that stores id of related user model
return $this->belongsTo('User');
}
}
BelongsTo is a inverse of HasOne.
We can define the inverse of a hasOne relationship using the belongsTo method.
Take simple example with User and Phone models.
I'm giving hasOne relation from User to Phone.
class User extends Model
{
/**
* Get the phone record associated with the user.
*/
public function phone()
{
return $this->hasOne('App\Phone');
}
}
Using this relation, I'm able to get Phone model data using User model.
But it is not possible with Inverse process using HasOne. Like Access User model using Phone model.
If I want to access User model using Phone, then it is necessary to add BelongsTo in Phone model.
class Phone extends Model
{
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo('App\User');
}
}
You can refer this link for more detail.
One-to-one relationship: You, as a User, can have one (hasOne) Profile. And of course the inverse also applies. Profile (belongsTo) a User. A user can't have more than one profile and a profile can't belong to multiple users.
If you want to make One TO one relationship between two table then first you have to make "hasOne" Relation and If you want to make inversely table relationship then you make " "Belongs to"... IT is a simple difference between HasOne and Belongs to the relationship if you want to know about this
One To Many (Inverse)
Now that we can access all of a post's comments, let's define a relationship to allow a comment to access its parent post. To define the inverse of a hasMany relationship, define a relationship function on the child model which calls the belongsTo method:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* Get the post that owns the comment.
*/
public function post()
{
return $this->belongsTo('App\Post');
}
}
Here you can see a good example and see what the difference is between BelongsTo and HasOne relationship in eloquent.
Eloquent Relationships Cheat Sheet by Mahmoud Zalt https://link.medium.com/9lj9BAG8lR
I'm having a tedious problem with Laravel's ORM.
I have a model with multiple relationships, like this:
class Pages extends Eloquent {
protected $guarded = array('id','config_id');
public function template()
{
return $this->belongsTo('Templates', 'templates_id');
}
public function updateUser()
{
return $this->belongsTo('Users', 'updated_by');
}
Now I can access the template related item in a simple way, like this:
$this->template->name;
And it works out of the bat, because Laravel's ORM assumes it is a one-to-one relationship and internally calls the first() method.
But when I try the same with updateUser it fails, returning an error, saying that it can't call name on a non-object.
But if I try this:
$this->updateUser()->first()->name;
it works, but it doesn't feel right to me.
So my question is, how Laravel's ORM decide if a relationship defined with belongsTo() is one-to-one or one-to-many? Is there a way to force a needed behaviour?
Thanks
You need to define the relationship. You can define 'different' relationships on the perspective.
The ->belongsTo() function is an inverse function - but you havent defined anything on the users table - so it is wrongly assuming the inverse is one-to-many.
Just add this to your users class:
class Users extends Eloquent {
public function pages()
{
return $this->hasMany('Pages');
}
}