Home > database >  Get foreign key data based on shop id
Get foreign key data based on shop id

Time:06-12

I have two tables: shop and menu.

shop

$table->id();
$table->string('name');

menu

$table->id();
$table->string('name');
$table->unsignedBigInteger('shop_id');
$table->foreign('shop_id')->references('id')->on('shop')->onDelete('cascade');

I need to print out menus name when you enter shop id (for example url /shop{id})

I have controller but i'm sure it's not the correct code.

MeniuController

public function menu($id)
{
    $men = shop::select('name')->where('id', $id)->get();
    return view('meniu', compact('men')); 
}

CodePudding user response:

Controller method:

public function menu($id)
{
   return view('menu', [
      'menus' => Menu::where('shop_id', $id)->get(),
   ]);
}

Blade template:

@foreach($menus as $menu)
   <div>{{ $menu->name }}</div>
@endforeach
  • Related