2017-02-16 65 views
0

我有Laravel与Mongodb和里面的集合文件是这样的;Laravel,Mongo,@foreach逻辑在视图

}, 
     "et_score": null, 
     "starting_time": "13:00:00", 
     "competition": { 
      "active": true, 
      "id": NumberInt("46"), 
      "name": "Liga 1" 
     }, 
     "awayTeam": { 
      "venue_id": NumberInt("2454"), 
      "name": "Team away -> Liga1", 
      "coach_id": NumberInt("140772"), 
      "twitter": "" 
     }, 
     "homeTeam": { 
      "venue_id": NumberInt("0"), 
      "name": "Team home -> Liga1", 

     }, 
     "home_score": NumberInt("1"), 
     "starting_date": "2016-12-24", 
     "away_score": NumberInt("1") 
    } 
}) 

.....

而且我.blade有这个;

.... 
    @foreach($data_user as $league) 

     <table class='table table-striped table-bordered table-condensed'> 

     <thead><tr><th>{{$league{"competition.name"} }}</th></tr></thead> 
      @endforeach 

     <tbody> 
      <tr> 
       @foreach($data_user as $match) 


        <th>{{$match{"starting_date"} }}</th> 
        <th>{{$match{"starting_time"} }}</th> 
        <th>{{$match{"status"} }}</th> 
        <th>{{$match{"homeTeam.name"} }}</th> 
        <th>{{$match{"home_score"} }} : {{$match{"away_score"} }} </th> 
        <th>{{$match{"awayTeam.name"} }}</th> 
      </tr> 
      @endforeach 
     </tbody> 
     </table> 
..... 

此打印输出表如下所示;

table.jpg

正如你看到我在团队中的一个“luega表”,并会拨出队“团队首页 - > Liga1”去表Liga1也是“团队首页 - >超级联赛”应在超级联赛等

我知道我缺少@foreach一些逻辑,但我没有这样的事情解决了足够的经验,谢谢

回答

0

我不明白,正是你想做的事,但是从我看到他们打开或关闭您的foreach错误,除了不好的模型使用外,还会产生很糟糕的html。

查看以下

@foreach($data_user as $league) 
<table class='table table-striped table-bordered table-condensed'> 
    <thead> 
     <tr> 
      <th>{{$league->competition->name }}</th> 
     </tr> 
    </thead> 
    <tbody> 
     @foreach($data_user as $match) 
     <tr> 
      <th>{{$match->starting_date }}</th> 
      <th>{{$match->starting_time }}</th> 
      <th>{{$match->status }}</th> 
      <th>{{$match->homeTeam.name }}</th> 
      <th>{{$match->home_score }} : {{ $match->away_score }} </th> 
      <th>{{$match->awayTeam.name }}</th> 
     </tr> 
     @endforeach 
    </tbody> 
</table> 
@endforeach 
+0

嗨曼努埃尔,得到错误你的建议:试图让4号线 – Ronnin

+0

非对象的属性信息是模型,集合,数组的结果?检查,或添加更多的信息来帮助你,但要记住你正在关闭foreach错误,并生成错误的 –

+0

ErrorException在f1da8afe98aba2781763f1e975725bb2aaa771cf.php第5行: 试图获取非对象的属性来获取非对象的属性(查看:/Volumes/WEB/test/CRUD-Laravel-5.3-with-MongoDB/resources/views/myhome.blade.php) – Ronnin

0

也许,如果有更多的帮助,如果你看到的模型和控制器

型号:

<?php 

namespace App\Models; 

use Moloquent; 

class PrimaryModels extends Moloquent 
{ 
    protected $collection = 'football.live'; 
} 

控制器:

<?php 

namespace App\Http\Controllers; 

use App\Models\PrimaryModels as Moloquent; 
use Illuminate\Http\Request; 
use Input, Redirect; 

class PrimaryController2 extends Controller 
{ 
    function index(){ 
     return Redirect::to('myhome'); 
    } 

    function pages($template){ 
     $getAllData = Moloquent::all(); 
     return view($template, ['data_user' => $getAllData]); 
    } 

} 
0

但是,如果我去像这样有上午概率,所有球队都在所有联赛

@foreach($data_user as $league) 
    <table class='table table-striped table-bordered table-condensed'> 
     <thead> 
     <tr> 
      <th>{{$league{"competition.name"} }}</th> 
     </tr> 
     </thead> 
     <tbody> 
     @foreach($data_user as $match) 
      <tr> 
       <th>{{$match->starting_date }}</th> 
       <th>{{$match->starting_time }}</th> 
       <th>{{$match->status }}</th> 
       <th>{{$match{"homeTeam.name"} }}</th> 
       <th>{{$match->home_score }} : {{ $match->away_score }} </th> 
       <th>{{$match{"awayTeam.name"} }}</th> 
      </tr> 
     @endforeach 
     </tbody> 
    </table> 
@endforeach