Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get foreign key data based on shop id

I have two tables: shop and menu.

shop

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

menu

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$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')); 
}

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading