我正在尝试从我的数据库中获取一对多关系的“experiencias”,但它 returns 是空集合,我该如何解决?
I'm trying to get 'experiencias` from my data base with relationship one to many, but it returns empty collection, how can I fix it?
我正在用 Laravel 做一个小的网络应用程序,我将信息直接放在我的数据库中,在我的 table 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
数组。
你可能真的调用了错误的模型。
public function index($matricula)
{
$experiencia = Experiencia::with('usuario')->whereHas('usuario', function($usuario) use($matricula) {
return $usuario->where('matricula_id', $matricula);
})->get();
dd($experiencia->all());
}
你的模型关系也不对
public function usuario()
{
return $this->belongsTo(usuario::class);
}
试试下面的代码;
public function show($param = null)
{
if (!$param) {
return abort(404, "there is no id present");
}
$param = (int) $param;
$usuario = usuarios::with('experiencia')->where('matricula_id', $param)->first();
dd($usuario->experiencia);
}
对于 index 函数不需要参数,如果参数未发送,请保护您的代码,不要 运行 其余代码。
我正在用 Laravel 做一个小的网络应用程序,我将信息直接放在我的数据库中,在我的 table 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
数组。
你可能真的调用了错误的模型。
public function index($matricula)
{
$experiencia = Experiencia::with('usuario')->whereHas('usuario', function($usuario) use($matricula) {
return $usuario->where('matricula_id', $matricula);
})->get();
dd($experiencia->all());
}
你的模型关系也不对
public function usuario()
{
return $this->belongsTo(usuario::class);
}
试试下面的代码;
public function show($param = null)
{
if (!$param) {
return abort(404, "there is no id present");
}
$param = (int) $param;
$usuario = usuarios::with('experiencia')->where('matricula_id', $param)->first();
dd($usuario->experiencia);
}
对于 index 函数不需要参数,如果参数未发送,请保护您的代码,不要 运行 其余代码。