简体   繁体   English

多个表联接并且仅使用一个刀片进行结果

[英]Multiple table joins and using only one blade for results

I'm using Laravel 5.4 to create a project. 我正在使用Laravel 5.4创建一个项目。 I'm using these 3 tables with that relation. 我正在使用这3个表与该关系。

图片描述

At my home page I'm doing a foreach on 9 elements. 在我的主页上,我正在对9个元素进行一次foreach And everything is working fine there. 那里一切正常。

图片描述

This is my route: 这是我的路线:

Route::get('/home', 'HomeController@index')->name('home');
Route::get("/game/{id_g}","HomeController@openGameHtml");
Route::get("/game/{id_g}","HomeController@openGameCss");

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

And i'm trying to foreach multiple results at one blade. 而且我正试图在一个刀片上提出多个结果。 Here: 这里:

 @foreach($chosenGame as $item)
    <li><a href="">
    {{$item->name}}</a></li>
@endforeach 

The problem is always one of these two function in controller are working in my last foreach. 问题总是控制器的这两个功能之一在我的最后一个foreach中工作。 I can't find a way to make my blade working for these 2 functions. 我无法找到一种方法使我的刀片服务器可以同时使用这两个功能。

You are passing the same variable in both functions, and in your @foreach you are looping thru that variable. 您在两个函数中传递了相同的变量,并且在@foreach中,您正在通过该变量循环。 Set different variables and pass them to the view, once in the view loop thru each variable extracting the properties you need. 设置不同的变量并将它们传递给视图,然后在视图循环中通过每个变量提取所需的属性。

Change the: 更改:

return view('openGame', compact('openGameHtml')); return view('openGame',compact('openGameHtml'));

and

return view('openGame', compact('chosenGameCss')); return view('openGame',compact('chosenGameCss'));

in the view use @foreach with each variable name. 在视图中,对每个变量名称使用@foreach。

The issue is that you want to extract different collections of results with the same variable which is not possible, you need to pass two different variables. 问题是您要使用相同的变量提取不同的结果集合,而这是不可能的,您需要传递两个不同的变量。 Post result if you have any inconvenience so we can provide a accurable answer. 如果您有任何不便,请发布结果,以便我们提供准确的答案。 Otherwise go to the Official Documentation - Passing Data to view 否则,请转到官方文档-传递数据以查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM