Laravel 5 如何在视图中显示有说服力的查询?

Laravel 5 如何在视图中显示有说服力的查询?,laravel-5,eloquent,eloquent-relationship,Laravel 5,Eloquent,Eloquent Relationship,我正在从“itemregistration”表中查询与“section”表具有良好关系的员工列表。但我无法将截面表中的信息显示到视图刀片 My controller按如下方式获取查询: $itemregistrations = Itemregistration::with('section')->get(); 当我使用以下命令检查阵列时: dd($itemregistrations->toArray()); 我得到这个结果: 0 => array:133 [▼

我正在从“itemregistration”表中查询与“section”表具有良好关系的员工列表。但我无法将截面表中的信息显示到视图刀片

My controller按如下方式获取查询:

  $itemregistrations = Itemregistration::with('section')->get();
当我使用以下命令检查阵列时:

 dd($itemregistrations->toArray());  
我得到这个结果:

 0 => array:133 [▼
"ItemRegistrationID" => 1
"RegistrationDate" => "2005-12-01"
"SectionID" => 12
"name" => "A SANTESH"
"section" => array:2 [ …2]
]
节数组应包含“SectionID”和“sectionname”字段

如果我和你核对一下 dd($项目注册)

它产生

 Collection {#1948 ▼
 #items: array:1125 [▼
 0 => Itemregistration {#1990 ▼
  #primaryKey: "ItemRegistrationID"
  #hidden: array:1 [ …1]
  +timestamps: false
  #connection: "mysql"
  #table: null
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:133 [ …133]
  #original: array:133 [ …133]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [ …1]
  #touches: []
  #visible: []
  #fillable: []
  #guarded: array:1 [ …1]
我想在刀片中显示结果,但无法获取节数组值:

 @foreach($itemregistrations as $index => $value)                                                                
    <td>{{ $value->name }}</td>
    <td>{{ $value->section->sectionname }}</td>
 @endforeach 
如果我用以下内容显示结果: @foreach($itemregistrations as$item) {{$item->name}}
@endforeach

我可以得到名字列表,但要得到这个部分的名字

    @foreach ($itemregistrations as $item)
     <tr>
       <td>{{ $item->section->sectionname }}</td>                           
     </tr>
    @endforeach 
项目注册模式:

  class Section extends Authenticatable
{
protected $table = 'sections';
protected  $primaryKey = 'SectionID';
 /**
 * Get the user that have agama.
 */
public function itemregistrations()
{
    return $this->hasMany('App\Itemregistration', 'SectionID', 'ItemregistrationID');
}

}
  class Itemregistration extends Model 
{
protected  $primaryKey = 'ItemRegistrationID';

/*
 * Get all of the owning models.
 */

public function section()
{
    return $this->belongsTo('App\Section', 'SectionID');

}

如何使用节数组值显示查询?

您可以使用以下代码:

@foreach($itemregistrations as $index => $value)
  <td>{{ $value->name }}</td>
  @foreach($value->section as $section) 
    <td>{{ $section->sectionname }}</td>
  @endforeach 
@endforeach 
@foreach($itemRegistrationas$index=>$value)
{{$value->name}
@foreach($value->section as$section)
{{$section->sectionname}
@endforeach
@endforeach

您可以使用以下代码:

@foreach($itemregistrations as $index => $value)
  <td>{{ $value->name }}</td>
  @foreach($value->section as $section) 
    <td>{{ $section->sectionname }}</td>
  @endforeach 
@endforeach 
@foreach($itemRegistrationas$index=>$value)
{{$value->name}
@foreach($value->section as$section)
{{$section->sectionname}
@endforeach
@endforeach

我假设,
@foreach
集合上调用
toArray()
,那么您应该以命名数组的形式处理数据,所以请尝试以下操作:

@foreach($itemregistrations as $index => $value)                                                                
  <td>{{ $value['name'] }}</td>
  @foreach($value['section'] as $section => $val) 
    <td>{{ $val['sectionname'] }}</td>
  @endforeach 
@endforeach 
@foreach($itemRegistrationas$index=>$value)
{{$value['name']}
@foreach($value['section']作为$section=>$val)
{{$val['sectionname']}
@endforeach
@endforeach

我假设,
@foreach
集合上调用
toArray()
,那么您应该以命名数组的形式处理数据,所以请尝试以下操作:

@foreach($itemregistrations as $index => $value)                                                                
  <td>{{ $value['name'] }}</td>
  @foreach($value['section'] as $section => $val) 
    <td>{{ $val['sectionname'] }}</td>
  @endforeach 
@endforeach 
@foreach($itemRegistrationas$index=>$value)
{{$value['name']}
@foreach($value['section']作为$section=>$val)
{{$val['sectionname']}
@endforeach
@endforeach

感谢其他人对我的问题的帮助

我已经配置了正确的语法来获取section数组中sectionname的值。我不知道为什么不能使用->helper来获取这些值。我尝试了以下语法,它可以在blade视图中获取值

 @foreach ($itemregistrations as $item)
 <tr>
   <td>{{ $item->name }}</td> //also can <td>{{ $item['name'] }}</td>
   <td>{{ $item['section]['sectionname'] }}</td>                             
 </tr>
@endforeach  
@foreach($items注册为$item)
{{$item->name}//也可以{{$item['name']}
{{$item['section]['sectionname']}
@endforeach

感谢其他人对我的问题的帮助

我已经配置了正确的语法来获取section数组中sectionname的值。我不知道为什么不能使用->helper来获取这些值。我尝试了以下语法,它可以在blade视图中获取值

 @foreach ($itemregistrations as $item)
 <tr>
   <td>{{ $item->name }}</td> //also can <td>{{ $item['name'] }}</td>
   <td>{{ $item['section]['sectionname'] }}</td>                             
 </tr>
@endforeach  
@foreach($items注册为$item)
{{$item->name}//也可以{{$item['name']}
{{$item['section]['sectionname']}
@endforeach

owh..why need if else here?也得到同样的错误,它显示在-->{{{$section->sectionname}}是的..但它显示在section->sectionname上同样的错误我认为这是你关系中的问题。你是如何得到这个数组的?你的关系是一对一。您必须修复关系,否则您将在节关系中获得单个项。我的一对多关系..itemregistration可以有一个节,但一个节可以属于多个itemregistration。我不熟悉雄辩的关系,所以我是根据教程中提供的示例这么做的。为什么这里需要if else?也会得到相同的错误,它显示在-->{{{$section->sectionname}}上的错误是。但是它显示在section->sectionname上的错误我认为这是关系中的问题。你是如何得到这个数组的?你的关系是一对一。您必须修复关系,否则您将在节关系中获得单个项。我的一对多关系..itemregistration可以有一个节,但一个节可以属于多个itemregistration。我不熟悉雄辩的关系,所以根据教程中提供的示例,我这样做了。有一个错误是“非法字符串偏移量‘sectionname’”。这个sectionname中没有输入错误吗?请检查它sectionname是正确的字段名。我找到了如下答案的解决方案。也许你对此有一个解释……我不是很明白,但我只是尝试了一些可能的方法,结果成功了。错误是“非法字符串偏移量‘sectionname’”。你没有输入错误吗?请检查它sectionname是正确的字段名。我找到了如下答案的解决方案。也许你对此有一个解释。我不是真的理解它,但我只是尝试了可能的方法,它起了作用。我编辑了我的问题,并添加了一些线索来识别错误。有人能帮我找出我的代码中缺少了什么吗?ThanksI编辑了我的问题并添加了一些线索来识别错误。有人能帮我找出我的代码中缺少了什么吗?谢谢