1

I'm trying to seed my table with some data with a belongsTo relationship, but I don't know how I should handle this relationship. Can anybody tell me what a belongsTo relationship should look like with seeding real data?

Seed file

public function run()
{
    CarSchema::create([
        'type_of_car' => 'small',
        'horse_power' => 98,
        'brand'       => 3 // Want to get the brand of id 3 here
    ])
 }

The result I want is that "brand" is what corresponds to id 3 in the brand table so in the front End I've got the brand as well and not just the id of it.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
nahoj
  • 285
  • 1
  • 4
  • 16

1 Answers1

1

Your Car model:

public function brand()
{
    return $this->belongsTo(\App\Brand::class, 'brand_id');
}

Your Brand model:

public function cars()
{
    return $this->hasMany(\App\Car::class);
}

Your cars migration:

public function up()
{
    Schema::create('cars', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->enum('type_of_car', ['small', 'medium', 'large']); // just extra example
        $table->year('year');
        $table->mediumInteger('horse_power');

        // car's brand id below
        $table->unsignedInteger('brand_id');
        $table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade');
    });
}

To seed your cars table, you can insert any id from brands table into brand_id in your cars table, for example:

Car::create([
    'name'        => 'S4',
    'type_of_car' => 'medium',
    'year'        => '2014',
    'horse_power' => 333,
    'brand_id'    => 3 // <-- this id should exist in brands table
]);

If you are seeding cars with random brands, you can insert a random brand id instead of hardcoding the id (like I did above with brand_id being 3):

...
    'brand_id' => \App\Brand::all('id')->random()->id
]);
emotality
  • 12,795
  • 4
  • 39
  • 60
  • Thanks, I will give this a try :) – nahoj Jan 26 '19 at 16:06
  • What if u had "brands" instead of "brand_id" as json, and in "brands" you had multiple "brand_id". How should u connect the relationship then? – nahoj Jan 26 '19 at 16:42
  • 1
    Don't think a car can have more than 1 brand? – emotality Jan 26 '19 at 17:04
  • And why are you storing it as json if you wanna reference it to a row in brands table? Do you know what you can do with relationships or are you new to it? – emotality Jan 26 '19 at 17:06
  • With the above, if you have a `name` field in your `brands` table, you can simply call `$car->brand->name` to get the car's brand name, its literally that easy and convenient. – emotality Jan 26 '19 at 17:13
  • Im pretty new to Laravel, Do u have slack or something, I can show u what I mean? – nahoj Jan 26 '19 at 17:14
  • Sure, check my profile and contact me via anything @emotality – emotality Jan 26 '19 at 17:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187366/discussion-between-nahoj-and-emotality). – nahoj Jan 26 '19 at 17:24