How to make a reflexive relationship in Laravel migration and model - laravel

I would like to have a table that has a one to many with itself. Eg: I have like a people table that can have many other people.This is how my code looks like:
public function up()
{
Schema::create('people', function (Blueprint $table) {
$table->string('id')->primary();//Here my PK is a string
$table->string('name');
$table->string('title')->nullable();
$table->string('parent_id');//Here is the foreign key of another person
$table->timestamps();
});
}
And in my Person.php model I have this:
public function people()
{
return $this->belongsTo('App\Person')->withDefault();
}
public function person()
{
return $this->hasMany('App\Person');
}
Take a look to this image:

In Person.php model:
public function parent()
{
return $this->belongsTo('App\Person', 'parent_id');
}
public function child()
{
return $this->hasMany('App\Person', 'parent_id');
}

Add parent_id in your migration and define relationship in you model
public function people()
{
return $this->belongsTo('App\Person')->withDefault();
}
public function person()
{
return $this->hasMany(self::class, 'parent_id');
}

Related

Summary table for the model

There is a model ObjectType and a model GuestOptions
I want to create a pivot table, I do this
public function up()
{
Schema::create('object_type_options', function (Blueprint $table) {
$table->unsignedBigInteger('object_type_id');
$table->unsignedBigInteger('guest_option_id');
$table->foreign('object_type_id')->references('id')->on('object_types')->onDelete('cascade');
$table->foreign('guest_option_id')->references('id')->on('guest_options')->onDelete('cascade');
$table->primary(['object_type_id','guest_option_id']);
});
}
In the model I define the relation ObjectType , but there is no result
public function options()
{
return $this->belongsToMany(GuestOptions::class,'object_type_options');
}
What am I doing wrong?
Can give two basic and quick solutions.
public function options()
{
return $this->belongsToMany(GuestOptions::class,'object_type_options','object_type_id','guest_option_id');
}
you can also use the relation more clearly like below. but need to change some attributes in the database.
public function options()
{
return $this->belongsToMany(GuestOptions::class);
}
And Table need to change
public function up()
{
//table name changed
Schema::create('guest_options_object_type', function (Blueprint $table) {
$table->unsignedBigInteger('object_type_id');//actual table name
$table->unsignedBigInteger('guest_options_id'); //actual table name
$table->foreign('object_type_id')->references('id')->on('object_types')->onDelete('cascade');
$table->foreign('guest_options_id')->references('id')->on('guest_options')->onDelete('cascade');
$table->primary(['object_type_id','guest_option_id']);
});
}

on cascade delete on many to many relationship in laravel

I had used many to many relationship.i have three table named:product,tag,productstag. in here I want whenever I am going to delete a product it will also delete the relationship it had on productstag table.
public function up()
{
Schema::create('productstags', function (Blueprint $table) {
$table->integer('product_id');
$table->integer('tag_id');
$table->primary(['product_id','tag_id']);
$table->timestamps();
});
}
as you can see I have used product_id and tag_id as the primary key.
so I can't use this
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('products')->onDelete('cascade');
so what's the alternative?
on my product model
public function tags()
{
return $this->belongsToMany('App\Tag','productstags','product_id','tag_id')->withTimestamps();
}
on my tag model:
public function products()
{
return $this->belongsToMany('App\Product','Productstags','tag_id','product_id');
}
on my produduct destroy function:
public function destroy(Request $request,$id)
{
$product=Product::findOrFail($id);
if($request->file('image')==''){
$input=$request->except('photo_id');
}
else{
$input=$request->all();
unlink(public_path()."/images/".$product->image->name);
}
$product->delete($input);
Session::flash('deleted_user','the user has been deleted');
return redirect(route('product.index'));
}
It's not as pretty as cascade but you could override the delete method and do this:
public function delete(){
$this->name_of_the_relationship()->delete();
return parent::delete();
}

Laravel 5.7 Relationship One To Many Columns

i have team and match table. All match information save on match table with two team, winner team, match time etc.
on match table i have three field team_1, team_2, winner_team
i want to relation those field with team table
Here is my code
Team Model
class Team extends Model
{
public function league()
{
return $this->belongsTo('App\League');
}
public function matchs()
{
return $this->hasMany('App\Match');
}
}
Match Model
class Match extends Model
{
public function team_1()
{
return $this->belongsTo('App\Team','id');
}
public function team_2()
{
return $this->belongsTo('App\Team','team_2');
}
public function winner()
{
return $this->belongsTo('App\Team','winner');
}
public function league()
{
return $this->belongsTo('App\League');
}
public function teams() {
return $this->team_1()->merge($this->team_2());
}
}
Migration File
Schema::create('matches', function (Blueprint $table) {
$table->increments('id');
$table->integer('team_1')->unsigned()->index();
$table->integer('team_2')->unsigned()->index();
$table->integer('league_id')->unsigned()->index();
$table->integer('winner')->nullable();
$table->dateTime('match_time');
$table->timestamps();
$table->foreign('team_1')->references('id')->on('teams')
->onDelete('restrict')
->onUpdate('cascade');
$table->foreign('team_2')->references('id')->on('teams')
->onDelete('restrict')
->onUpdate('cascade');
$table->foreign('league_id')->references('id')->on('leagues')
->onDelete('restrict')
->onUpdate('cascade');
$table->foreign('winner')->references('id')->on('teams')
->onDelete('restrict')
->onUpdate('cascade');
});
public function team_1()
{
return $this->belongsTo('App\Team', 'team_1');
}
public function team_2()
{
return $this->belongsTo('App\Team', 'team_2');
}

One to Many Relationship not working on Laravel

I have three models that are related. First I have User that belongs to a Role. On the other hand, Role has many roles. Role belongs to many permissions and Permissions belongs to many Role. I am using the AuthServiceProvider as suggested by jeffrey way . However, when I'm fetching all the permissions of a User I am having error which is, "Call to a member function getKey() on boolean". Can someone please help me on this. Please refer to the codes below.
User.php
public function role()
{
return $this->belongsTo('App\Role');
}
public function assignRole($role)
{
return $this->roles()->save(
Role::whereName($role)->firstOrFail()
);
}
public function hasRole($role)
{
if(is_string($role)){
return $this->role->contains('name', $role);
}
return !! $role->intersect($this->role)->count();
}
Role.php
class Role extends Model
{
public function users()
{
return $this->hasMany('App\User');
}
public function permissions()
{
return $this->belongsToMany('App\Permission');
}
public function givePermissions(Permission $permission)
{
return $this->permissions()->save($permission);
}
}
Permission.php
class Permission extends Model
{
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
AuthServiceProvider
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
//get all permissions
foreach ($this->getPermissionTo() as $permission ) {
$gate->define($permission->name, function($user) use ($permission){
return $user->hasRole($permission->roles);
});
}
}
public function getPermissionTo()
{
return Permission::with('roles')->get();
}
and lastly, heres the user table that has a foreign key of role_id
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned();
$table->string('id_no')->unique()->index();
$table->string('u_first_name');
$table->string('u_middle_name');
$table->string('u_last_name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});

Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

Trying to get a list of users associated with an event. Here are my eloquent models:
User.php:
public function fbevents()
{
$this->belongsToMany('Fbevent', 'fbevent_user');
}
Fbevent.php:
public function users()
{
$this->belongsToMany('User', 'fbevent_user);
}
I get this error when I try to find the list:
$event = Fbevent::find(10);
var_dump($event->users->lists('userId'));
I've set up a pivot table in the db with the following migration:
$table->increments('id');
$table->integer('fbevent_id')->unsigned()->index();
$table->foreign('fbevent_id')->references('id')->on('fbevents')->onDelete('cascade');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
And added an entry in the fbevent_user table with fbevent_id = 10 and user_id = 1.
You need to return a result from your relations
public function fbevents()
{
return $this->belongsToMany('Fbevent', 'fbevent_user');
}
public function users()
{
return $this->belongsToMany('User', 'fbevent_user');
}

Resources