【问题标题】:Creating function for laravel controller/view to load database values为 laravel 控制器/视图创建函数以加载数据库值
【发布时间】:2018-06-08 07:10:20
【问题描述】:

我正在更新 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

【问题讨论】:

  • 这里有一个类型:@if($events->dealer_num = $dealer->id) 应该是==
  • 我确实发现了,但不幸的是它仍然没有解决问题
  • 使用 dd() 查看 $calls_allowed 和 $contact_events 是否正在获取值
  • dealers.dealers 和deals.contact_events 列在dealer 表上吗?
  • @retrograde 经销商是模式的名称。 Contact_events 和 Dealers 都是该架构中的一个表

标签: php sql laravel


【解决方案1】:

这似乎是使用 Laravel 的 Eloquent ORM 强大功能的最佳时机......

Check out the with and has in the Laravel docs

这将需要根据您的需要进行一些调整,但它会是这样的:

$d = Dealer::where('id', '=', $id)
     ->with('contact_events')->first();

这使用 Eloquent 来获取属于具有 $id 的经销商的所有联系事件。

然后你可以做这样的事情 注意:这假定 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

【讨论】:

    【解决方案2】:

    从数据库中检索数据后,您似乎没有将数据分配给对象。

    $contact_events = DB::table('dealers.contact_events')->
    where('dealer_num', $id)->
    pluck('updated_at', 'method', 'notes');
    
    // add this
    $d->contact_events = $contact_events;
    

    【讨论】:

    • 好的,我添加了该行而没有修改任何其他内容,但由于某种原因它仍然没有加载该数据。
    • 我添加了 dd($dealer->contact_events) 并且它确实转储了一条记录,但由于某种原因它仍然没有显示在 html 表/视图中
    • @TomN。如果您将 dd 内容添加到您的答案中,这可能会有所帮助。
    • pluck 只需要 2 个参数
    猜你喜欢
    • 1970-01-01
    • 2016-07-21
    • 2018-02-19
    • 2013-08-05
    • 1970-01-01
    • 2018-08-30
    • 2016-09-03
    • 2017-12-02
    • 1970-01-01
    相关资源
    最近更新 更多