inserting with db::seed
use Illustrate/…/DB
DB::table(‘tblname’)->insert([
..])
Massive input in db::seed
User::factory()->count(10)->create();
One to One realtionship
Belong to method
Phone ->foreign key= User_id to User-> primary key= ID
IN Phone class-
public function user(){
return $this->belongsTo(User::class);
}
e.g
Route::get('/connect',function (){
$phone = Phone::find(2);
$result = $phone->user;
return $result;
}); Or User to Phone (useer has many phones but you wanna need one) public function phone(){
return $this->hasOne(Phone::class);
}One to Many
User to Phone (useer has many phones )
public function phone(){
return $this->hasMany(Phone::class);
}
Many to Many
Many User have Many roles create pivot table like Note name must be alphabetical role_user table(r is prior to u) user_id< - > role_id
public function roles(){
return $this->BelongtoMany(Role::class,’pivotableName’);
}