Php 为laravel控制器/视图创建函数以加载数据库值
我正在更新一个laravel刀片模板,以便将一些数据库信息插入html表中。为了做到这一点,我必须为这个刀片服务器的控制器添加新的数据,这就是我遇到一些问题的地方 我仍在尝试了解laravel的更多内容,因此我认为我的语法或创建此数据的方法是不正确的,但我现在无法指出这一点 在我下面的函数中,Php 为laravel控制器/视图创建函数以加载数据库值,php,sql,laravel,Php,Sql,Laravel,我正在更新一个laravel刀片模板,以便将一些数据库信息插入html表中。为了做到这一点,我必须为这个刀片服务器的控制器添加新的数据,这就是我遇到一些问题的地方 我仍在尝试了解laravel的更多内容,因此我认为我的语法或创建此数据的方法是不正确的,但我现在无法指出这一点 在我下面的函数中,$calls\u allowed部分已经存在,并且当前可以在页面上工作。我创建了函数的$contact\u events部分,这就是我的问题所在 在我看来,我围绕讨论中的html表创建了一个foreach循
$calls\u allowed
部分已经存在,并且当前可以在页面上工作。我创建了函数的$contact\u events
部分,这就是我的问题所在
在我看来,我围绕讨论中的html表创建了一个foreach循环和if语句。该表已加载,但它是空的,即使数据库中有经销商的记录
我想说
如果$dealer->id与contact\u events.dealer\u num匹配,则加载该经销商的所有记录
contact\u events
是表,dealer\u num
是我要匹配的列,然后我尝试将该表中的列(updated\u 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
查看代码:
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
联系日期
接触类型
通话记录
@foreach($dealer->contact_事件作为$events)
@如果($events->dealer\u num=$dealer->id)
{{$events->updated_at}
{{$events->method}
{{$events->notes}
@恩迪夫
@endForeach
从数据库检索后,您似乎没有将数据分配给对象
$contact_events = DB::table('dealers.contact_events')->
where('dealer_num', $id)->
pluck('updated_at', 'method', 'notes');
// add this
$d->contact_events = $contact_events;
这似乎是一个完美的时间来使用拉雷维尔的雄辩ORM的力量 这需要根据您的需要进行一些精细化处理,但具体如下:
$d = Dealer::where('id', '=', $id)
->with('contact_events')->first();
这将使用Eloquent获取属于具有$id的经销商的所有contact_事件
然后你可以做这样的事情
注意:这假设允许的呼叫是经销商表上的记录。如果我误解了这一点,你仍然可以跑步,而不是像你现在这样把它包括进去
@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
@如果(!为空($dealer->允许呼叫)
@foreach($dealer->contact_事件作为$events)
{{$events->updated_at}
{{$events->method}
{{$events->notes}
@endForeach
@恩迪夫
这里有一种类型:@if($events->dealer\u num=$dealer->id)应该是==我确实捕捉到了,不幸的是,尽管使用dd()它仍然没有解决问题要查看$calls\u allowed和$contact\u events是否正在获取经销商表上的dealers.dealers和dealers.contact\u events列的值?@逆行经销商是模式的名称。contact\u events和dealers在该模式中都是一个表OK,我添加了该行,但没有修改任何其他内容,并且它仍然没有为某些re加载该数据我添加了dd($dealer->contact_events),它确实转储了一条记录,但由于某种原因,它仍然没有显示在html表中/view@TomN.如果将dd内容添加到答案中,可能会有所帮助。pull
只接受两个参数