donglie1898 2017-11-04 06:32
浏览 270
已采纳

Laravel Backpack - 显示关系功能的特定属性

I have the registered Comment model which has a User reference, like this:

public function user() {
    return $this->belongsTo('App\User');
}

This function returns an instance of a User, and that's correct, but I don't know how to register the User column the get que user property using Backpack. Instead, I'm get a JSON representation of my model:

enter image description here

So, how do I get a specific field from my relationship function?

  • 写回答

2条回答 默认 最新

  • duanjucong3124 2017-11-06 04:29
    关注

    Sounds like the select column is a perfect match for you. Just use it in your EntityCrudController's setup() method, like so:

    $this->crud->addColumn([
       // 1-n relationship
       'label' => "User", // Table column heading
       'type' => "select",
       'name' => 'user_id', // the column that contains the ID of that connected entity;
       'entity' => 'user', // the method that defines the relationship in your Model
       'attribute' => "user", // foreign key attribute that is shown to user
       'model' => "App\Models\User", // foreign key model
    ]);
    

    The "attribute" tells CRUD what to show in the table cell (the name, the id, etc).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部