0

I'm trying to display the value of brand_id column from brands table. Here's so far what I've done:

Car model

use App\Brand;

class Car extends Model
{
    public function brands(){
        return $this->belongsToMany(Brand::class);
     }
}

Brand model

use App\Car;

class Brand extends Model
{
    protected $fillable = [
        'brand_name'
    ];

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

ShowroomController

use App\Car;

class ShowroomController extends Controller
{
    public function details($name){
        $data = Car::where('car_name' , '=', $name)->first();

        if ($data == null){
            return redirect(route('index'));
        }else{
            return view('showroom')->with('detail', $data);
        }
    }
}

showroom view

@if (isset($detail))
  {{ $detail }} 
  {{ $detail->brands->brand_name }} //this doesn't work
@endif

Database

Brands table:

+----+------------+
| id | brand_name |
+----+------------+
|  1 | Brand1     | 
|  2 | Brand2     | 
+----+------------+

Cars table:

+----+----------+----------+
| id | car_name | brand_id |
+----+----------+----------+
|  1 | Car      |        1 |
+----+----------+----------+

I got lost on this point. Is this the right way to do the belongstomany and hasmany relationship? Thanks.

Kevin
  • 315
  • 2
  • 5
  • 19
  • Try eager loading, you will get the brands data `$data = Car::with('brands')->where('car_name' , '=', $name)->first();` – STA Jun 12 '20 at 08:08
  • SQLSTATE[42S02]: Base table or view not found: 1146 Table 'databasename_db.brand_car' doesn't exist (SQL: select `brands`.*, `brand_car`.`car_id` as `pivot_car_id`, `brand_car`.`brand_id` as `pivot_brand_id` from `brands` inner join `brand_car` on `brands`.`id` = `brand_car`.`brand_id` where `brand_car`.`car_id` in (1)) – Kevin Jun 12 '20 at 09:14
  • It shows this error. – Kevin Jun 12 '20 at 09:15

2 Answers2

2

Change
return $this->belongsToMany(Brand::class); to return $this->belongsTo(Brand::class); on the Car model

Also rename name function to brand. because car have only single brand After it you can do $detail->brand->brand_name

lmboom
  • 189
  • 4
0

Hi I know it seems simple, thanks to @Imboom I got a hint to fix my problem. I made some changes on Car model:

  1. return $this->belongsToMany(Brand::class); to return $this->belongsTo(Brand::class)
  2. rename name function to brand
  3. Lastly, I just added 'brand_id' to specify the column in cars table.

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

In ShowroomController, I changed my return statement detail to car. See the code below:

public function details($name){
    $data = Car::where('car_name' , '=', $name)->first();

    if ($data == null){
        return redirect(route('index'));
    }else{
        return view('showroom')->with('car', $data);
    }
}

Then in showroom view, $car->brand->brand_name .

        @if (isset($car))
            {{ $car->car_name }}
            {{ $car->brand->brand_name }} // Output is Brand1
        @endif

Thank you!

Kevin
  • 315
  • 2
  • 5
  • 19