Laravel 5.2:如何在Controller中获取Model属性

问题描述 投票:2回答:3

如何在Laravel中的Controller中访问Model的属性?

在我的用户模型中,我有这个数组:

protected $sortable = [
    'first_name',
    'last_name',
    'email',
];

然后,在我的UserController中,我有:

namespace App\Http\Controllers;
...
use App\User;

class UserController extends Controller
{
    public function index()
    {
        // here I'd like to get the $sortable array
    }
}

谢谢

laravel-5.2
3个回答
0
投票

在索引函数中,您可以通过它访问它

$this->sortable

为此,您必须将属性可访问性更改为public:

public $sortable = [
    'first_name',
    'last_name',
    'email',
];

如果您坚持使用qazxsw poi辅助功能,则可以在模型中创建一个getter函数。


0
投票

Dunno,如果我理解你,但如果你在你的DB中有名字,姓氏和电子邮件,你可以这样得到它们:

protected

如果没有,只需创建它:

$user = User::all();

0
投票

用户模型

 $user=new User();

UserController控制器

public $sortable = [
    'first_name',
    'last_name',
    'email'
];
© www.soinside.com 2019 - 2024. All rights reserved.