<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hamid09430 / direct-nested-relation-object example snippets
class Country extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function comments()
{
return $this->hasManyDeep('App\Comment', ['App\User', 'App\Post']);
}
}
class Country extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function comments()
{
return $this->hasManyDeep(
'App\Comment',
['App\User', 'App\Post'], // Intermediate models, beginning at the far parent (Country).
[
'country_id', // Foreign key on the "users" table.
'user_id', // Foreign key on the "posts" table.
'post_id' // Foreign key on the "comments" table.
],
[
'id', // Local key on the "countries" table.
'id', // Local key on the "users" table.
'id' // Local key on the "posts" table.
]
);
}
}
class Country extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function comments()
{
return $this->hasManyDeep('App\Comment', ['App\User', 'App\Post'], [null, 'custom_user_id']);
}
}
class User extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function permissions()
{
return $this->hasManyDeep('App\Permission', ['role_user', 'App\Role']);
}
}
class User extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function permissions()
{
return $this->hasManyDeep(
'App\Permission',
['role_user', 'App\Role'], // Intermediate models and tables, beginning at the far parent (User).
[
'user_id', // Foreign key on the "role_user" table.
'id', // Foreign key on the "roles" table (local key).
'role_id' // Foreign key on the "permissions" table.
],
[
'id', // Local key on the "users" table.
'role_id', // Local key on the "role_user" table (foreign key).
'id' // Local key on the "roles" table.
]
);
}
}
class User extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function postComments()
{
return $this->hasManyDeep(
'App\Comment',
['App\Post'],
[null, ['commentable_type', 'commentable_id']]
);
}
}
class User extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function postTags()
{
return $this->hasManyDeep(
'App\Tag',
['App\Post', 'taggables'],
[null, ['taggable_type', 'taggable_id'], 'id'],
[null, null, 'tag_id']
);
}
}
class Tag extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function postComments()
{
return $this->hasManyDeep(
'App\Comment',
['taggables', 'App\Post'],
[null, 'id'],
[null, ['taggable_type', 'taggable_id']]
);
}
}
class Tag extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function postAuthors()
{
return $this->hasManyDeep(
'App\User',
['taggables', 'App\Post'],
[null, 'id', 'id'],
[null, ['taggable_type', 'taggable_id'], 'user_id']
);
}
}
class Country extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function comments()
{
return $this->hasManyDeepFromRelations($this->posts(), (new Post)->comments());
}
public function posts()
{
return $this->hasManyThrough('App\Post', 'App\User');
}
}
class Post extends Model
{
public function comments()
{
return $this->hasMany('App\Comment');
}
}
class Country extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function latestComment()
{
return $this->hasOneDeep('App\Comment', ['App\User', 'App\Post'])
->latest('comments.created_at');
}
}
public function comments()
{
return $this->hasManyDeep('App\Comment', ['App\User', 'App\Post'])
->withIntermediate('App\Post');
}
foreach ($country->comments as $comment) {
// $comment->post->title
}
public function comments()
{
return $this->hasManyDeep('App\Comment', ['App\User', 'App\Post'])
->withIntermediate('App\Post', ['id', 'title']);
}
public function comments()
{
return $this->hasManyDeep('App\Comment', ['App\User', 'App\Post'])
->withIntermediate('App\Post', ['id', 'title'], 'accessor');
}
foreach ($country->comments as $comment) {
// $comment->accessor->title
}
public function comments()
{
return $this->hasManyDeep('App\Comment', ['App\User', 'App\Post'])
->withIntermediate('App\Post')
->withIntermediate('App\User', ['*'], 'post.user');
}
foreach ($country->comments as $comment) {
// $comment->post->title
// $comment->post->user->name
}
public function permissions()
{
return $this->hasManyDeep('App\Permission', ['role_user', 'App\Role'])
->withPivot('role_user', ['expires_at']);
}
foreach ($user->permissions as $permission) {
// $permission->role_user->expires_at
}
public function permissions()
{
return $this->hasManyDeep('App\Permission', ['role_user', 'App\Role'])
->withPivot('role_user', ['expires_at'], 'App\RoleUserPivot', 'pivot');
}
foreach ($user->permissions as $permission) {
// $permission->pivot->expires_at
}
class Post extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function childComments()
{
return $this->hasManyDeep('App\Comment', ['App\Comment as alias'], [null, 'parent_id']);
}
}
class Comment extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasTableAlias;
}
class Country extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function comments()
{
return $this->hasManyDeep('App\Comment', ['App\User', 'App\Post'])
->withTrashed('users.deleted_at');
}
}
class User extends Model
{
use SoftDeletes;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.