【问题标题】:Cannot display related entities in one-to-many relationship无法以一对多关系显示相关实体
【发布时间】:2018-11-26 10:45:02
【问题描述】:

我的代码中有一个一对多的关系,我想在我的视图中显示,但我收到了 NULL 错误。下面是定义关系的模型。

public function products()
{
    return $this->hasMany('App\Product', 'id', 'product_id');
}

这是我的控制器方法:

$products = Invoice::with('products')->get();
return view('admin.invoices.show', 
    compact('invoice', $invoice),
    compact('clients', $clients))->with(compact('products', $products));

这是我的看法,但它总是显示没有产品

@foreach ($products as $product)
    <td>{{ $product->product->name ?? 'no products' }}</td>
@endforeach

当我 dd() $products 数组时,我得到以下信息:

#relations: array:1 [▼
    "products" => Collection {#321 ▼
      #items: array:1 [▼
        0 => Product {#318 ▼
          #fillable: array:5 [▶]
          #connection: "mysql"
          #table: null
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:8 [▼
            "id" => 1
            "name" => "asd"

编辑

这是我的完整控制器,我可以毫无问题地与用户建立关系,这是一个 hasOne 关系,但我无法访问产品关系,这是主要问题

public function show(Invoice $invoice)
{
    $clients = Invoice::with('user')->get();
    $products = Invoice::with('products')->get();
    return view('admin.invoices.show', compact('invoice', $invoice),compact('clients',$clients))->with(compact('products',$products));
}

【问题讨论】:

    标签: php laravel laravel-5 eloquent laravel-5.6


    【解决方案1】:

    {{ $product-&gt;product-&gt;name ?? 'no products' }}替换为:

    {{ $product-&gt;name ?? 'no products' }}

    还将@foreach($products as $product) 编辑为@foreach($products-&gt;products-&gt;all() as $product)

    【讨论】:

    • 同样的 null 返回
    • 此集合实例上不存在属性 [产品] :(
    • 此集合实例上不存在属性 [products]。
    • @foreach($products->products as $product) {{ $product->name ?? '没有产品' }} @endforeach
    • 即使我添加了 all() 并再次添加了相同的输出
    【解决方案2】:

    您现在已经包含了您的控制器代码。所以实际上不是

    public function show(Invoice $invoice)
    {
        $clients = Invoice::with('user')->get();
        $products = Invoice::with('products')->get();
        return view('admin.invoices.show', compact('invoice', $invoice),compact('clients',$clients))->with(compact('products',$products));
    }
    

    您只能使用:

    public function show(Invoice $invoice)
    {
        return view('admin.invoices.show', compact('invoice', $invoice));
    }
    

    并在您的视图中显示您可以使用的发票产品:

    @forelse ($invoice->products as $product) 
      {{ $product->name }}
    @empty
       no products
    @endforelse
    

    【讨论】:

    • 没有朋友,我正在尝试打印发票,并且该发票可以包含 2 种产品,例如我目前正在开发 1 种产品 :)
    • 所以我将发票与产品的关系设置为 hasmany,因此我的发票可以包含许多产品
    • @Farshad 这就是我在代码中展示的内容,但您可能没有为此包含整个控制器代码。使用完整的控制器方法代码更新您的问题
    • @Farshad 你也可以看看我编辑的答案
    • @marchin nabialek,我亲爱的朋友,我想你误会了,看看我编辑的问题提前谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多