Laravel One to Many Eloquent Relationship Example
Apr 27, 2021 . Admin
Hello Friends,
Now let's see example of how to use hasmany relationship in laravel. This is a short guide on laravel if hasmany relationship. We will use how to use hasmany relationship in laravel. Here you will learn how to use hasmany relationship in laravel. We will use how to use if hasmany relationship in laravel. Let's get started with how to hasmany relationship use in laravel.
Here i will give you many example how you can check hasmany relationship in laravel.
Create Migrations:Now we have to create migration of "users" and "services" table. so let's create like as below:
user table migration:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } ?>
services table migration:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateServicesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('services', function (Blueprint $table) { $table->id(); $table->string('name'); $table->text('description'); $table->string('comment'); $table->string('icon'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('services'); } } ?>Create Models:
Here, we will create User and Service table model. we will also use "hasMany()" for relationship of both model.
User Model:
<?php namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use HasFactory, Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'is_active', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; /** * Write code on Method * * @return response() */ public function services() { return $this->hasMany(Service::class, 'user_id', 'id'); } } ?>
Service Model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Service extends Model { use HasFactory; public $table = 'services'; public $fillable = ['name','description','comment','icon','user_id']; /** * Write code on Method * * @return response() */ public function user(){ return $this->hasOne(User::class, 'id', 'user_id'); } } ?>
Retrieve Records:
$user = User::find(1); $services = $user->services; dd($services);
$service = Service::find(1); $user = $service->user; dd($user);
Create Records:
$user = User::find(1); $service = new Service; $service->service = "Hi MyWebTuts.com"; $user = $user->services()->save($service);
$user = User::find(1); $service1 = new Service; $service1->service = "Hi MyWebTuts.com service 1"; $service2 = new service; $service2->service = "Hi MyWebTuts.com service 2"; $user = $user->services()->saveMany([$service1, $service2]);
$service = Service::find(1); $user = User::find(2); $service->user()->associate($user)->save();
It will help you....