Laravel: Eloquent How to get property of model if column name contains a dash?

后端 未结 2 996
情话喂你
情话喂你 2021-02-08 16:56

I have the following in my root route:

$user = User::all();
return $user->column-one;

Which returns the exception Use of undefin

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-08 17:31

    I haven't tried this, but am curious if using camelCase for it would work. That's how it works for things like routes. For example: $user->columnOne.

    I would however recommend renaming that column. That really doesn't map well in a PHP app.

    Update - Try this:

    $user->{"column-one"}

提交回复
热议问题