【发布时间】:2019-08-21 08:10:36
【问题描述】:
我正在使用 Laravel 做一个小 Web 应用程序,我将信息直接放在我的数据库中,我的表 Experiencia 我有:
id matricula_id nombre_Emp puesto
1 201618131 1 1
这是我的Usuario 模型:
protected $table = 'usuarios';
protected $fillable = [
'id',
'matricula_id',
'nombres',
'email',
'password',
'facultad',
];
public function experiencia()
{
return $this->hasMany(Experiencia::class,'matricula_id');
}
还有我的Experiencia 模特:
protected $table = 'experiencias';
protected $fillable = [
'id',
'matricula_id',
'nombre_Emp',
'puesto',
'fecha_Ini',
'fecha_Fin',
];
public function usuario()
{
return $this->belongsTo(usuarios::class,'matricula_id');
}
两个模型都有可填写的matricula_id
而我的controller我要恢复的函数是:
public function index($matricula)
{
$usuario = usuarios::where('matricula_id', $matricula)->with('experiencia')->get();
dd($usuario->all());
}
当我做 dd 我的结果是这样的:
array:1 [▼
0 => usuarios {#282 ▼
#table: "usuarios"
#fillable: array:20 [▶]
#hidden: array:2 [▶]
#casts: array:1 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:24 [▶]
#original: array:24 [▶]
#changes: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"experiencia" => Collection {#279 ▼
#items: [] <<<<-------- Here is my problem
}
]
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
}
]
我希望我的数据库中有一个 experiencia 数组。
【问题讨论】:
标签: laravel eloquent laravel-5.8