Last active
January 31, 2022 13:50
-
-
Save dominiquevienne/a17d51c369b0bc9fd86cde01c04f4a25 to your computer and use it in GitHub Desktop.
Laravel Many to Many relationship example - Vehicles and Options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
class CreateOptionsTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('options', function (Blueprint $table) { | |
$table->id(); | |
$table->string('name')->unique(); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('options'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
class CreateOptionVehicleTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('option_vehicle', function (Blueprint $table) { | |
$table->unsignedBigInteger('option_id'); | |
$table->unsignedBigInteger('vehicle_id'); | |
$table->unique(['option_id', 'vehicle_id', ]); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('option_vehicle'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Model; | |
class Option extends Model | |
{ | |
use HasFactory; | |
protected $fillable = [ | |
'name', | |
]; | |
/** | |
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | |
*/ | |
public function vehicles() | |
{ | |
return $this->belongsToMany(Vehicle::class); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Model; | |
class Vehicle extends Model | |
{ | |
use HasFactory; | |
protected $fillable = [ | |
'vehicle_type', | |
'brand', | |
/** Everything you would need about a vehicle */ | |
]; | |
/** | |
* @return \Illuminate\Database\Eloquent\Relations\HasOne | |
*/ | |
public function license() | |
{ | |
return $this->hasOne(License::class); | |
} | |
/** | |
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | |
*/ | |
public function insurance() | |
{ | |
return $this->belongsTo(Insurance::class); | |
} | |
/** | |
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | |
*/ | |
public function options() | |
{ | |
return $this->belongsToMany(Option::class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment