Laravel belongsTo and hasMany relationship on same model - laravel

How do you apply belongsTo and hasMany relationship on same model?
For example, I have one User and one Project model. Now Project model has method user() and User model has projects() method.
Now I want a user to share projects with other users. So that I can have methods like users() in Project model and shared_projects() in User model.
How I can achieve that?
Here is my current Project model
class Project extends \Eloquent {
protected $fillable = [];
public function user() {
return $this->belongsTo('User');
}
}
And this my User model
class Project extends \Eloquent {
protected $fillable = [];
public function projects() {
return $this->hasMany('Project');
}
}

In this case, User and Project have Many-to-Many relationship because one user has many projects and one project belongs to many user. Therefore, you should create a third table (project_user) which connect User table and Project table in the database.
And you set Many-to-Many relation to User Model and Project Model.
class User extends Eloquent {
public function projects()
{
return $this->belongsToMany('Project');
}
}
In Project model
class Project extends Eloquent {
public function users()
{
return $this->belongsToMany('User');
}
}
Then you can do something like this
//users in the same project
$users = Project::find($id)->users;

Okay, so here it is how I solved it. I used pivot table as you suggested but I even added hasMany relationship like this way. I am still not sure what I am doing is perfectly correct but it worked. :)
class Project extends \Eloquent {
protected $fillable = [];
public function users() {
return $this->belongsToMany('User');
}
public function user() {
return $this->belongsTo('User');
}
}
And here is the key ingredient in User model
class User extends \Eloquent {
protected $fillable = [];
public function projects_owned() {
return $this->hasMany('Project', 'user_id');
}
public function projects() {
return $this->belongsToMany('Project');
}
}

Related

How to elegantly use a remote one-to-many like association on a many-to-many in Laravel

For example, there are four models as follows.
class User extends Authenticatable
{
public function orders(): MorphMany
{
return $this->morphMany(Order::class, 'orderable');
}
}
class Partner extends Model implements Authenticatable
{
public function orders(): MorphMany
{
return $this->morphMany(Order::class, 'orderable');
}
}
class Order extends Model
{
public function orderable(): MorphTo
{
return $this->morphTo();
}
public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class)->withPivot('amount', 'price');
}
}
class Project extends Model
{
public function orders(): BelongsToMany
{
return $this->belongsToMany(Order::class)->withPivot('amount', 'price');
}
}
User、Partner and Order are one to many polymorphic relations。
Order and Project are many to many relations。
How to elegantly get all projects enrolled by a given user?
Thanks!

Bind Laravel table to other database connection

I am finding some difficulties to bind one relationship table to my second database.
In "db_main" I have tables, with the basic details about each object and than I have other tables: "db_site1", "db_site2" etc, in which tables I save detailed information about the same objects.
Everything is working find with that schema, except one situation in that I have relationship table (bookmaker_games), which must be only on "db_main". The table save the relations between bookmakers and games.
On my website1 I want to list the games of a bookmaker with the following models:
class Bookmaker extends Model {
public function games() {
// here the database is "db_site1"
return $this->belongsToMany('App\Models\Game', 'bookmaker_games', 'bookmaker_id', 'game_id');
}
}
class Game extends Model {
public function bookmakers() {
// here the database is "db_site1"
return $this->belongsToMany('App\Models\Bookmaker', 'bookmaker_games', 'game_id', 'bookmaker_id');
}
}
class BookmakerGame extends Model {
protected $connection = 'db_main';
}
When I try to return all games, it is thinking that "bookmaker_games"-table is on database "db_site1", which is wrong.
What is the best way to tell that relationship to look at the correct database ?
Declare BookmakerGame as a subclass of Illuminate\Database\Eloquent\Relations\Pivot
Then define the relationships to use it.
Prefixing the connection name
//...
use Illuminate\Database\Eloquent\Relations\Pivot;
class Bookmaker extends Model {
public function games() {
// here the database is "db_site1"
return $this->belongsToMany(
'App\Models\Game', 'db_main.bookmaker_games', 'bookmaker_id', 'game_id')
->using('App\Models\BookmakerGame');
}
}
class Game extends Model {
public function bookmakers() {
// here the database is "db_site1"
return $this->belongsToMany(
'App\Models\Bookmaker', 'db_main.bookmaker_games', 'game_id', 'bookmaker_id')
->using('App\Models\BookmakerGame');
}
}
class BookmakerGame extends Pivot {
protected $connection = 'db_main';
}
Using the class path to the pivot
use Illuminate\Database\Eloquent\Relations\Pivot;
class Bookmaker extends Model {
public function games() {
// here the database is "db_site1"
return $this->belongsToMany(
'App\Models\Game', 'App\Models\BookmakerGame', 'bookmaker_id', 'game_id');
}
}
class Game extends Model {
public function bookmakers() {
// here the database is "db_site1"
return $this->belongsToMany(
'App\Models\Bookmaker', 'App\Models\BookmakerGame', 'game_id', 'bookmaker_id');
}
}
class BookmakerGame extends Pivot {
protected $connection = 'db_main';
protected $table = 'bookmaker_games';
}

Laravel 5.4 table relationship between 3 small tables

I have a recently started learning Laravel 5.4, I am having trouble with my 3 way table relationship, I've looked at a few articles online regarding many to many, but this relationship is just a "hasOne" on both sides.
Could anyone give me a helpful hint as to how to structure my table relationship, here is the PK/FK relationship:
Users table (id)
Listings table (id, user_id)
Insights table (id, listing_id) - one insight row per listing only.
And the models below:
Users Model
class User extends Model
{
public function listing()
{
return $this->belongsTo('App\Listing');
}
}
Listing Model
class Listing extends Model
{
public function insight()
{
return $this->hasOne('App\Insight');
}
}
Insight Model
class Insight extends Model
{
public function listing()
{
return $this->hasOne('App\Listing');
}
}
And what I am trying to achieve is to query the users own listings, with each listings current insights.
Thanks a bunch.
Simon.
User model
class User extends Model
{
public function listing()
{
return $this->hasOne('App\Listing');
}
}
Listing Model
class Listing extends Model
{
public function insight()
{
return $this->hasOne('App\Insight');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
Insight Model
class Insight extends Model
{
public function listing()
{
return $this->belongsTo('App\Listing');
}
}
And if you want query users with Listing and Insight
$users = User::with(['listing', 'listing.insight'])->get();
foreach($users as $user) {
$user->listing->insight;
}
class User extends Model
{
public function listing()
{
return $this->hasMany(Listing::class);
}
}
class Listing extends Model
{
public function insight()
{
return $this->hasOne(Insight::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
class Insight extends Model
{
public function listing()
{
return $this->belongsTo(Listing::class);
}
}
$users = User::with('listing.insight')->get();
foreach($users as $user) {
$user->listing->insight;
}

Set hasOne relation to new instance of child model

Using Laravel 4.2, how can I set a hasOne relation on a model to an instance of a new model without touching the database?
I want to do the following, but Laravel is treating the instance as a property, not a child relation.
class ParentClass extends \Eloquent {
public function child() {
return $this-hasOne('ChildClass', 'child_id', 'parent_id');
}
}
$parent = new ParentClass();
$parent->child = new ChildClass();
To get through my problem, I have created a Trait class that I added to my models that allows me to set a relation. Doing this does not automatically set my parent/child relation values so I have to be careful to do this manually before I save/push.
trait ModelSetRelationTrait
{
public function setRelation($key, $model)
{
$this->relations[$key] = $model;
}
}
class ParentClass extends \Eloquent {
use ModelSetRelationTrait;
public function child() {
return $this-hasOne('ChildClass', 'child_id', 'parent_id');
}
}

Not able to create eloquent relationship with table which have custom primary key

When i'm trying to get user details from company Model i'm getting NULL value.
class User extends Model
{
protected $primaryKey = 'uid';
}
class Company extends Model
{
$table = 'company';
public function users(){
return $this->hasMany('App\User','user_id','uid');
}
}
class RepoController{
public function index(){
$user = Company::where('id',1)->with('user')->get();
/// Returning value but without Users
}
}`
users table should contain company_id and you should define id as company ID because company table doesn't use custom ID:
public function users()
{
return $this->hasMany('App\User', 'company_id', 'id');
}
Or just this:
public function users()
{
return $this->hasMany('App\User');
}
Also, you're trying to load user relationship, but it should be users(), so do this instead:
Company::where('id', 1)->with('users')->get();
I think your ids are in wrong position
try swapping them like
return $this->hasMany('App\User','uid','user_id');

Resources