Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5.5 multiple controller inside one view

How to show TeamController@index and ProductController@index both show list of team and product inside one view main.blade.php

like image 761
Vps_Little Avatar asked Mar 11 '26 17:03

Vps_Little


1 Answers

Looks like you want to show two datasets on one page. Basically, it means you have to execute two controller methods but it's not necessary to follow each and everything that official documentation says.

For example, if Products belong to a team, you can execute only TeamController@index and show products as given below.

@foreach($teams as $team)
  @foreach($team->products as $product)
   {{ $product->name }}
  @endforeach
@endforeach

If no teams and products are two different entities and does not have any relation, you can just pass teams and products like this:

TeamController.php

public function index()
{
  $teams = Team::all();
  $products = Product::all(); // Don't forget to include 'use App\Product'

  return view('index',compact(['teams','products']);
}

and then you can show teams and products like this:

index.blade.php

@foreach($teams as $team)
  {{ $team->name }}
@endforeach

@foreach($products as $product)
  {{ $product->name }}
@endforeach

Getting information from two different models does not mean you have to execute two different controller functions.

Still, if you want to get data from two different controllers, you can setup index.blade.php and create two ajax requests that will get data from two different URLs (two different controller methods).

Let me know if you have any more questions.

like image 136
p01ymath Avatar answered Mar 14 '26 07:03

p01ymath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!