如何在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
}
}
谢谢
在索引函数中,您可以通过它访问它
$this->sortable
为此,您必须将属性可访问性更改为public:
public $sortable = [
'first_name',
'last_name',
'email',
];
如果您坚持使用qazxsw poi辅助功能,则可以在模型中创建一个getter函数。
Dunno,如果我理解你,但如果你在你的DB中有名字,姓氏和电子邮件,你可以这样得到它们:
protected
如果没有,只需创建它:
$user = User::all();
用户模型
$user=new User();
UserController控制器
public $sortable = [
'first_name',
'last_name',
'email'
];