Pre-requisites
We'll assume 3 things:
- That the model was created using the approach in this blog post.
- That the route was created using the approach in this blog post.
- That a migration file has been set using this blog post.
- That an Eloquent API Resource has been set up using this blog post.
We're going to create another table called Second which contains options. These options can be set when we create a YourModel record, but we'll also need to retro-set the existing YourModel records with the new Second model values
Part 1: The New Model
We'll begin with a new model. The -m creates a migration file for the model.
php artisan make:model Second -m
Now we'll open the newly created migration file.
Inside the function up()
, add the new fields e.g.
$table->string('name')->unique();
This time we're going to specify the record values to be migrated. At the end of the function up()
, add:
DB::table('second')->insert([
[
'name' => 'Type 1'
'created_at' => now(),
'updated_at' => now(),
],
[
'name' => 'Type 2'
'created_at' => now(),
'updated_at' => now(),
],
[
'name' => 'Type 3'
'created_at' => now(),
'updated_at' => now(),
],
]);
Part 2: The Connecting Migration
Now we need to make another migration to add SecondModel values to the original YourModel records:
php artisan make:migration add_secondmodel_id_to_yourmodel
A foreign key will be required to link a record in YourModel, to a record on the SecondModel. In this newly created migration we can populate the
Schema::table within the function up()
with:
$table->foreignId('second_id')->nullable()->after('id')->constrained();
Now we'll append the function drop()
, by populating the Schema::table:
$table->dropForeign('second_id');
$table->dropColumn('second_id');
Part 3: Updating the First Model
Now in the YourModel file, we need to add 'second_id'
to the $fillable array
.
The $fillable array is used to prevent unauthorised data manipulation by limiting which attributes can be set on a model.
Now in the YourModel file, we need to add a function second()
, which returns
$this->belongsTo(Second::class);
to make the link.
Now we need to open the YourFactory.php
and within the function definition()
return array, add:
'second_id' => and(0, 1) === 0 ? NULL : Second::pluck('id')->random(),
This will populate the foreign keys of existing records using YourModel with random values from the SecondModel.
We should also update the StoreYourRequest file, by adding to the return array of function rules()
'second_id' => 'nullable|exists:second,id'
then creating:
public function attributes()
{
return [
'second_id' => 'second'
];
}
Part 4: The Second Model Resource
We'll now need to create a resource, so that we only communicate the fields we need through the API:
php artisan make:resource SecondResource
Then reset the return values of the function toArray()
return [
'id' => $this->id,
'name' => $this->name,
];
Part 5: Connecting 2 Models
Now on the YourResource, add append Second to the return values of the function toArray()
, thus
'second' => SecondResource::make($this->whenLoaded('second'))
Finally, it's now time to run the migrations:
php artisan migrate:fresh --seed