我正在使用 Laravel 5.4 创建一个项目。 我正在使用这 3 个具有该关系的表。
在我的主页上,我正在对 9 个元素执行 foreach
。那里一切都工作正常。
这是我的路线:
Route::get('/home', 'HomeController@index')->name('home');
Route::get("/game/{id_g}","HomeController@openGameHtml");
Route::get("/game/{id_g}","HomeController@openGameCss");
我的 Controller :
public function openGameHtml($chosenGame, Request $request)
{
$chosenGame = DB::table('games')
->join('listhtmlgames', 'games.id_g', '=', 'listhtmlgames.id_g')
->select('games.*', 'listhtmlgames.*')
->where('listhtmlgames.id_g', $chosenGame)->get();
//var_dump($chosenGame);
dd($chosenGame);
//print_r($chosenGame);
return view('openGame', compact('chosenGame'));
}
public function openGameCss($chosenGame, Request $request)
{
$chosenGame = DB::table('games')
->join('listcssgames', 'games.id_g', '=', 'listcssgames.id_g')
->select('games.*', 'listcssgames.*')
->where('listcssgames.id_g', $chosenGame)->get();
var_dump($chosenGame);
//($chosenGame);
//print_r($chosenGame);
return view('openGame', compact('chosenGame'));
}
我正在尝试在一个 Blade 上获取多个结果。这里:
@foreach($chosenGame as $item)
<li><a href="">
{{$item->name}}</a></li>
@endforeach
问题始终是 Controller 中的这两个函数之一在我的最后一个 foreach 中工作。我找不到一种方法让我的 Blade 发挥这两个功能。
最佳答案
您在两个函数中传递相同的变量,并且在 @foreach 中您正在循环该变量。 设置不同的变量并将它们传递给 View ,在 View 中循环通过每个变量提取您需要的属性。
更改:
return view('openGame',compact('openGameHtml'));
和
返回 View ('openGame',compact('chosenGameCss'));
在 View 中对每个变量名称使用@foreach。
问题是您想要使用同一变量提取不同的结果集合,这是不可能的,您需要传递两个不同的变量。如果您有任何不便,请发布结果,以便我们提供准确的答案。否则请转到 Official Documentation - Passing Data to view
关于php - 多个表连接并仅使用一个 Blade 获得结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46553468/