你好,我有laravel模型:
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吗?
谢谢
答案 0 :(得分:1)
好吧,我希望你知道自己在做什么。您要按索引访问User对象。
以下是控制器的代码段:-
$user = User::where('id','=' ,'10')->first();
$user = array_values($user->toArray());
现在$ user有一个数组,您可以按索引访问刀片文件。
请注意:-当您添加更多字段或删除某些字段时,这将产生问题。