有很多表。一个描述了滑块,另一个描述了这些滑块内部的信息。我的任务是将表中的所有值转移到一个模板中。也就是说,当从可能的情况中选择所需的滑块时,我们会得到相关的信息。
已完成的操作:
主要型号
class AdminSlider extends Model
{
public function aboutUs()
{
return $this->hasMany('App\AboutUs');
}
public function mainSlider()
{
return $this->hasMany('App\MainSlider');
}
}
从属模型如下
class MainSlider extends Model
{
public function adminSlider()
{
return $this->belongsTo('App\AdminSlider', 'slider_id', 'id');
}
}
2。在控制器中创建了与模板一起使用的变量
public function index()
{
$adminSlidersItems = $this->getSliderInfo();
$sliderInfo = view('admin.adminEditText')->with('sliderInfo',$adminSlidersItems)->render();
$this->vars = array_add($this->vars, 'sliderInfo', $sliderInfo);
return $this->renderOutput();
}
public function getSliderInfo() {
$sliderInfoItems = $this->as_rep->get();
return $sliderInfoItems;
}
当我在控制器中查看变量admin时,我看到了
{#230 ▼
#items: array:6 [▼
0 => AdminSlider {#231 ▼
#connection: "mysql"
#table: "admin_sliders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▼
"id" => 1
"title" => "Main Slider"
"path" => "http://jinol/admin/sliders/mainslider"
"img" => "mainslider.jpg"
"alias" => "mainslider"
"created_at" => null
"updated_at" => null
]
#original: array:7 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
1 => AdminSlider {#232 ▶}
2 => AdminSlider {#233 ▶}
3 => AdminSlider {#234 ▶}
4 => AdminSlider {#235 ▶}
5 => AdminSlider {#236 ▶}
]
}
template.layout
@extends('admin.site')
@section('admin.adminNavigation')
{!! $adminNavigation !!}
@endsection
@section('admin.adminEditText')
{!! $sliderInfo !!}
@endsection
@if(count ($sliderInfo) > 0) <br>
<div id="content-page" class="content group"><br>
@foreach($sliderInfo as $info)<br>
<tr><br>
<td class="align-left">{{$info->adminSlider->id}}</td><br>
</tr><br>
@endforeach<br>
</div><br>
@endif
试图获取非对象的属性“ id”(查看: W:\ domains \ jinol \ resources \ views \ admin \ adminEditText.blade.php)。
如何找到我的错误?
答案 0 :(得分:0)
尝试
$info->adminSlider['id']
装有
$info->adminSlider->id
我认为这将解决您的问题
答案 1 :(得分:0)
在您的控制器中进行更改:
public function index()
{
$adminSlidersItems = $this->getSliderInfo();
$sliderInfo = view('admin.adminEditText')->with('sliderInfo',$adminSlidersItems)->render();
$this->vars = array_add($this->vars, 'sliderInfo', $sliderInfo);
return $this->renderOutput();
}
收件人:
public function index()
{
$adminSlidersItems = $this->getSliderInfo();
return view('admin.adminEditText', ['sliderInfo' => $adminSlidersItems]);
}
您认为更改
<td class="align-left">{{$info->adminSlider->id}}</td><br>
收件人
<td class="align-left">{{optional($info->adminSlider)->id}}</td><br>
显然,并非所有$ sliderInfo都定义了-> adminSlider,并且在将其设置为null时尝试访问其id时会出错。可选的帮助程序仅在-> adminSlider不为null时尝试访问ID。