你好,我有一个拉拉模型:
class User extends Authenticatable
{
use SoftDeletes;
use Notifiable;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The database primary key value.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = [
'name',
'family',
];
}
在控制器中,我通过id获得一个用户,例如:
$user=User::where('id','=' ,'10')->first () ;
在刀片中,我显示了家庭价值:
{{$user->family}}
我正在使用:
{{$user[3] }}
以获得家庭价值。我们是否可以通过索引获得值,比如名称2,家庭3,而不是像这个$user->name,或者$user->family?
谢谢
发布于 2020-04-10 12:10:25
我希望你知道你在做什么。因为您希望按索引访问用户对象。
这是控制器的代码片段:-
$user = User::where('id','=' ,'10')->first();
$user = array_values($user->toArray());
现在,$user有了一个数组,您可以通过索引访问刀片文件。
注意:-这将在添加更多字段或删除某些字段时产生问题。
https://stackoverflow.com/questions/61139336
复制相似问题