我正在努力更新Laravel刀片模板,以将一些数据库信息插入HTML表中。为了做到这一点,我必须在此刀片的控制器中添加新数据,这就是我遇到一些麻烦的地方。
我仍在尝试使用Laravel了解更多,因此我认为我的语法或创建此数据的方法是不正确的,但我现在无法将手指放在上面。
在下面的功能中,$calls_allowed
部分已经存在,并且目前在页面上起作用。我创建了功能的$contact_events
部分,这就是我的问题。
在我看来,我在相关的HTML表周围创建了一个foreach循环和if语句。该表加载,但是即使数据库中有经销商的记录,也是空的。
我想说的 if $dealer-> id matches contact_events.dealer_num, load all records for that dealer
contact_events
是表, dealer_num
是我要匹配的列,然后我试图从该表(updated_at
,method
,notes
)中加载列到HTML表中。
受影响的代码如下。视图/路由/控制器工作,只是我创建的只是此功能,而不是加载数据。非常感谢任何帮助。
控制器代码:
public function show($id)
{
$d = Dealer::find($id);
if(!$d){
Session::flash('warning_message', 'Sorry that resource can not be found.');
return redirect()->route('account.dealer.index');
}
$calls_allowed = DB::table('dealers.dealers')->
where('dealer_num', $id)->
pluck('calls_allowed');
$contact_events = DB::table('dealers.contact_events')->
where('dealer_num', $id)->
pluck('updated_at', 'method', 'notes');
if(!empty($calls_allowed)){
$d->calls_allowed = $calls_allowed[0];
} else {
$d->calls_allowed = null;
}
return view('Account.Dealer.show')->with('dealer', $d);
}
查看代码:
<thead>
<tr>
<th>Contacted Date</th>
<th>Type of Contact</th>
<th>Call Notes</th>
</tr>
</thead>
@foreach($dealer->contact_events as $events)
@if($events->dealer_num = $dealer->id)
<tbody>
<tr>
<td>{{$events->updated_at}}</td>
<td>{{$events->method}}</td>
<td>{{$events->notes}}</td>
</tr>
</tbody>
@endif
@endForeach
从数据库检索后,您似乎没有将数据分配给对象。
$contact_events = DB::table('dealers.contact_events')->
where('dealer_num', $id)->
pluck('updated_at', 'method', 'notes');
// add this
$d->contact_events = $contact_events;
这似乎是使用Laravel雄辩ORM的力量的绝佳时机...
与Laravel Docs一起查看并具有
这将需要根据您的需求进行一些技巧,但这将是这样的:
$d = Dealer::where('id', '=', $id)
->with('contact_events')->first();
这使用雄辩来获取带有$ id的所有属于经销商的Contact_events。
然后你可以做这样的事情注意:这是假设call_allowed是经销商表上的记录。如果我误解了这一点,那么您仍然可以跑步,而不是像您一样包含它。
@if(!is_null($dealer->calls_allowed)
@foreach($dealer->contact_events as $events)
<tbody>
<tr>
<td>{{$events->updated_at}}</td>
<td>{{$events->method}}</td>
<td>{{$events->notes}}</td>
</tr>
</tbody>
@endForeach
@endif