如何访问laravel 5.6中的关系船属性

时间:2018-07-02 17:01:44

标签: php laravel

我正在使用hasmany关系,并且我有一系列要循环浏览并回显属性的项目,这是我使用的代码,并且遇到诸如null查询之类的错误,并尝试获取非对象

 public function sellman(){
    return $this->hasMany('App\User' , 'id','sellman');
}

此处控制器

$clients = Client::all();
    $sellman = Client::with('sellman')->get();
    return view('admin.client.index',compact('clients','sellman'));

这是视图

@foreach($sellman as $sellmans)
                                <td>
                                    {{{$sellmans->user->name}}}
                                </td>
                            @endforeach

这是$ sellman的dd

Collection {#748 ▼


#items: array:2 [▼
    0 => Client {#694 ▼
      #fillable: array:15 [▶]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:18 [▶]
      #original: array:18 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "sellman" => Collection {#746 ▼
          #items: array:1 [▼
            0 => User {#741 ▶}
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
    1 => Client {#695 ▶}

或者我想知道我是否也无法使用$ clients获得它的$ clients dd:

Client {#666 ▼


 #fillable: array:15 [▶]
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:18 [▶]
  #original: array:18 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [▼
    "sellman" => Collection {#718 ▼
      #items: array:1 [▼
        0 => User {#712 ▼
          #fillable: array:3 [▶]
          #hidden: array:2 [▶]
          #connection: "mysql"
          #table: null
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:7 [▶]
          #original: array:7 [▶]
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #visible: []
          #guarded: array:1 [▶]
          #rememberTokenName: "remember_t

被选中”

2 个答案:

答案 0 :(得分:1)

首先,sellmanhasMany,所以您不能像这样访问它:

$sellmans->user->name

所以需要像这样

@foreach($sellman as $sellmans)
   @foreach($sellmans->sellmans as $x)
       $x->name
   @endforeach   
@endforeach

此外,您没有user attribute。您有User模型,但是模型定义为sellmans

此行: $sellman = Client::with('sellman')->get();您将得到Clients和许多salesman

答案 1 :(得分:0)

我更改了方法(sellman)的名称,因为该方法的名称与字段名称相同,并导致发生冲突。

public function sellmanList(){
    return $this->hasMany(User::class , 'id','sellman');
}

controller

$clients = Client::all();
$sellman = Client::with('sellmanList')->get();
return view('admin.client.index',compact('clients','sellman'));

view

@foreach($sellman as $sellmans)
    @foreach($sellmans->sellmanList as $key=>$user)
        {{$user->name}}
    @endforeach
@endforeach