laravel 9.0 Eloquent all() 方法没有 return 关系
laravel 9.0 Eloquent all() method doesn't return relationship
我有两个表 Department 和 InspectionSite。
部门迁移
public function up()
{
Schema::create('departments', function (Blueprint $table) {
$table->id();
$table->string('department_name', 100);
});
}
InspectionSite 迁移:
public function up()
{
Schema::create('inspection_sites', function (Blueprint $table) {
$table->id();
$table->string('site_title');
$table->foreignIdFor(\App\Models\Department::class);
$table->timestamps();
});
}
部门模型:
class Department extends Model
{
use HasFactory;
protected $fillable = ['depatment_name'];
public function sites() {
return $this->hasMany(InspectionSite::class);
}
}
InspectionSite 模型
class InspectionSite extends Model
{
use HasFactory;
protected $guarded = [];
public function department() {
return $this->belongsTo(Department::class, 'department_id');
}
}
正在从控制器获取数据
public function get() {
$selector = ['site_title AS title', 'site_type' ];
$sites = InspectionSite::all();
return response()->json($sites->department, 200);
}
当我调用 find() 方法时它 returns 关系数据但不在 all() 方法中?
public function get() {
$departments = Department::all();
return response()->json($departments->sites, 200);
}
Error details
all()
方法 returns Collection
个模型,每个模型应该有 department
关系。
当您尝试 return 时:
return response()->json($sites->department, 200);
您正在 Collection
实例上访问 department
属性。
相反,您应该在该集合的每个模型上调用它。
在这里您可以尝试解决方案,具体取决于您想要实现的目标
解决方案一:(推荐)
$sites = InspectionSite::with('department')->get();
return response()->json($sites, 200);
// result
[
{
...
department: ...
}
...
]
解决方案 2:(Returns 只有部门,没有 InspectionSite
属性)
$sites = InspectionSite::with('department')->get()
->map(function($s) {
return $s->department;
});
return response()->json($sites, 200);
// result
[
{
[department]
}
{
[department]
}
]
我有两个表 Department 和 InspectionSite。
部门迁移
public function up()
{
Schema::create('departments', function (Blueprint $table) {
$table->id();
$table->string('department_name', 100);
});
}
InspectionSite 迁移:
public function up()
{
Schema::create('inspection_sites', function (Blueprint $table) {
$table->id();
$table->string('site_title');
$table->foreignIdFor(\App\Models\Department::class);
$table->timestamps();
});
}
部门模型:
class Department extends Model
{
use HasFactory;
protected $fillable = ['depatment_name'];
public function sites() {
return $this->hasMany(InspectionSite::class);
}
}
InspectionSite 模型
class InspectionSite extends Model
{
use HasFactory;
protected $guarded = [];
public function department() {
return $this->belongsTo(Department::class, 'department_id');
}
}
正在从控制器获取数据
public function get() {
$selector = ['site_title AS title', 'site_type' ];
$sites = InspectionSite::all();
return response()->json($sites->department, 200);
}
当我调用 find() 方法时它 returns 关系数据但不在 all() 方法中?
public function get() {
$departments = Department::all();
return response()->json($departments->sites, 200);
}
Error details
all()
方法 returns Collection
个模型,每个模型应该有 department
关系。
当您尝试 return 时:
return response()->json($sites->department, 200);
您正在 Collection
实例上访问 department
属性。
相反,您应该在该集合的每个模型上调用它。
在这里您可以尝试解决方案,具体取决于您想要实现的目标
解决方案一:(推荐)
$sites = InspectionSite::with('department')->get();
return response()->json($sites, 200);
// result
[
{
...
department: ...
}
...
]
解决方案 2:(Returns 只有部门,没有 InspectionSite
属性)
$sites = InspectionSite::with('department')->get()
->map(function($s) {
return $s->department;
});
return response()->json($sites, 200);
// result
[
{
[department]
}
{
[department]
}
]