【问题标题】:Laravel 5.5 RelationshipsLaravel 5.5 关系
【发布时间】:2018-03-28 21:59:25
【问题描述】:

我正在尝试实现模型之间的关系,但我收到“尝试获取非对象的属性‘产品’”,但我不明白为什么,因为我之前以同样的方式使用过它,而且效果很好。

关系的逻辑是1 Merchant hasMany Products

这是我正在使用的代码:

商家模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Merchant extends Model {
    protected $table = "merchants";

    protected $fillable = [
        'merchant_id', 'merchant_name', 'secret_key', 'merchant_address', 'merchant_phone', 'merchant_admin',
        'merchant_contact', 'merchant_mail', 'merchant_description', 'enable', 'created_at', 'updated_at'];

    public function users() {

        //many to many
        return $this->belongsToMany('App\User');
    }

    public function branchOffices() {
        return $this->hasMany('App\BranchOffice', 'merchant_id', 'merchant_id');
    }

    public function products() {
        return $this->hasMany('App\Products', 'merchant_id', 'merchant_id');
    }

    public function transactions() {
        return $this->hasMany('App\Transaction', 'merchant_id', 'merchant_id');
    }

    public function readers() {
        return $this->hasMany('App\Reader', 'merchant_id', 'merchant_id');
    }

}

产品型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model {
    protected $table = "products";

    protected $fillable = [
        'id', 'barcode', 'description', 'price', 'currency_id', 'merchant_id', 'branch_id',
        'enable', 'created_at', 'updated_at'];

    public function merchants() {
        return $this->belongsTo('App\Merchant', 'merchant_id', 'merchant_id');
    }

    public function currencies() {
        return $this->belongsTo('App\Currency', 'iso_4712', 'currency_id');
    }

    public function branch_sectors() {
        return $this->belongsToMany('App\BranchSector');
    }

}

这是 ProductController 中的方法:

public function merchantProducts() {

        $products = Merchant::find('merchant_id')->products;
        return $products;
    }

如果有人可以帮助我,我将非常感激。

提前致谢!!

【问题讨论】:

  • 如果你这样做,你会得到什么 public function products() { dd($this->hasMany('App\Products', 'merchant_id', 'merchant_id')); } ?
  • 嗨!我不明白...我的意思是...我可以直接从模型中使用 dd 吗?因为,当我尝试从控制器使用它时,由于某种原因,我收到“尝试获取非对象的属性‘产品’”,我无法从控制器调用 products()
  • 您可以在代码中的任何位置使用 dd()。我只是想找出原因。
  • 顺便说一句,你可以删除 $table = 'whatevers' 只要 Model 是 Whatever 并且 table 是模型名称的复数
  • 是的...但我不知道如何调用它,因为我不能调用 products()

标签: laravel laravel-5 laravel-5.5


【解决方案1】:

假设商家在提供商家ID的数据库中不能保证存在,最好检查商家是否存在,如果存在则检索其产品。

$products = collect();
$merchant = Merchant::find($merchant_id);
if($merchant) {
    $products = $merchant->products;
}
return $products;

【讨论】:

    【解决方案2】:

    全部!!

    最后我以这种方式使用资源解决了这个问题:

    public function merchantProducts(Request $request) {
    
            $merchant_id = $request->merchant_id;
    
            $products = Product::where('merchant_id', $merchant_id)->paginate(15);
    
            return ProductResource::collection($products);
    
        }
    

    谢谢大家!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-11
      • 2018-03-07
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 2018-10-06
      相关资源
      最近更新 更多