【问题标题】:Laravel 6, Method Eloquent\Collections::links does not existLaravel 6,方法 Eloquent\Collections::links 不存在
【发布时间】:2019-10-03 18:14:55
【问题描述】:

我正在编写一个小程序,我想根据当前用户 customer_id 显示所有位置。 在这种逻辑中,客户是公司向我们购买的产品,因此客户与用户不同。客户可以有多个位置,我想根据当前用户的 customer_id 显示位置。

为此,我编写了以下代码: 这是引发错误的代码。

if (isset(auth()->user()->customer_id) && !auth()->user()->hasRole('admin')) {
    $customer = Customer::find(auth()->user()->customer_id);
    $locations = $customer->locations()->get();
} else {
    $locations = Location::all();
}
return view('locations.index', ['locations' => $locations]);

我检查用户是否有 customer_id 并且不是管理员。 如果两者都是真的,我会让客户连接到 customer_id。 然后我检索与该客户相关的所有位置。 这一切都有效,当我用 dd($var) 检查它时,它总是返回预期值。

index.blade.php

<table id="locations" class="table align-items-center table-flush" style="100%">
                            <thead class="thead-light">
                            <tr>
                                <th scope="col">{{ __('Customer') }}</th>
                                <th scope="col">{{ __('Location Name') }}</th>
                                <th scope="col">{{ __('Address') }}</th>
                                <th scope="col">{{ __('Postal Code') }}</th>
                                <th scope="col">{{ __('City') }}</th>
                                <th scope="col">{{ __('Country') }}</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach ($locations as $location)
                                <tr>
                                    <td>{{ $location->customer->name}}</td>
                                    <td>{{ $location->name}}</td>
                                    <td>{{ $location->address}}</td>
                                    <td>{{ $location->postalcode}}</td>
                                    <td>{{ $location->city}}</td>
                                    <td>{{ $location->country}}</td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>

现在,当我返回视图时,我收到以下错误: Method Illuminate\Database\Eloquent\Collection::links does not exist

这里奇怪的是,我已经以相同的方式返回了其他实体的数据,而且效果很好。 这段代码做同样的事情并且确实有效。

if (isset(auth()->user()->distributor_id) && !auth()->user()->hasRole('admin')) {
    $distributor = Distributor::find(auth()->user()->distributor_id);
    $customer = $distributor->customers()->get();
} else {
    $customer = Customer::all();
}
return view('customers.index', ['customers' => $customer]);

在互联网上环顾四周后,我发现了这个问题:

Laravel 4 Call to undefined method Illuminate\Database\Eloquent\Collection::links()。 这个问题看起来很像我的问题,但遮阳篷并没有解决我的问题。我试图实现他们的 awnser,但我仍然遇到同样的异常。

谁能解释我为什么会收到这个错误?

【问题讨论】:

  • 你用这个变量来分页吗?
  • 也添加您的刀片文件。你在那里使用links,这导致了问题
  • 不,我只是将变量返回到视图并循环显示数据。就像我说的,在第二个代码块中它工作正常。
  • @Collin 在您的$locations 数组中是否有可用的links 元素?
  • @AmitSenjaliya 不,没有任何可用的链接元素

标签: php laravel


【解决方案1】:

也许这个工作

$locations = $customer->locations->get();

删除位置后的括号,您需要在 Customer.php 中定义函数 locations

public function locations()
{
    return $this->hasMany('App\Locations', 'user_id', 'id');
}

【讨论】:

  • 问题出在我尝试从指定客户那里检索所有位置的第一个代码块中。
  • 我明白了,然后我改变了我的答案,再检查一次。谢谢
  • 我实际上可以很好地检索数据,当我调试代码时,我得到了预期的值。我认为问题出在返回视图部分。
  • 已经尝试过 paginate()。这是首先想到的。奇怪的是,两段相同且执行相同操作的代码却输出不一样。
  • 我在locations.index和customers.index上面看到了2个块代码的区别。你能仔细检查一下 2 个刀片文件,也许是一个在某处使用 links() 的文件(例如:在主刀片等中)
【解决方案2】:

似乎在您的模板文件中,您为locations 调用了links 方法。 该方法用于分页实现。所以,你应该添加paginate 方法来访问links 方法。

试试这个:

$customer = Customer::where('id', auth()->user()->customer_id)->paginate(10);

【讨论】:

  • 仍然得到相同的异常,为什么我的经销商/客户代码可以工作而客户/位置代码不行。我的意思是它的代码是一样的吗?
  • 您能否为客户和位置提供 dd() 的输出?
【解决方案3】:

你应该使用 分页($numerOfLocationPerPage) 代替 get()

在 $locations = $customer->locations()->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 2016-10-12
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2021-05-30
    相关资源
    最近更新 更多