[英]How to correctly use relationships with() in Laravel?
I just wanted to clarify using the relationship in tables.我只是想澄清使用表中的关系。 Right now, I wanted to fetch records of
designation names
from designation_id
in employees
table.现在,我想从
employees
表中的designation_id
中获取designation names
的记录。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use App\Models\{ Designations, Positions }; class Employees extends Model { use HasFactory; protected $table = 'employees'; protected $primaryKey = 'id'; public $timestamps = true; protected $casts = [ 'designation_id' => 'array', 'position_id' => 'array', 'basic_pay' => 'decimal:2', ]; protected $dates = ['created_at', 'updated_at']; protected $fillable = [ 'first_name', 'last_name', 'designation_id', 'position_id', 'basic_pay', ]; public function designations() { return $this->hasMany(Designations::class, 'id', 'designation_id'); } public function positions() { return $this->hasMany(Positions::class, 'id', 'position_id'); } }
Here's my designation model:这是我的编号 model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use App\Models\Employees; class Designations extends Model { use HasFactory; protected $table = 'designations'; protected $primaryKey = 'id'; public $timestamps = true; protected $dates = ['created_at', 'updated_at']; protected $fillable = [ 'name', 'description' ]; public function employees() { return $this->belongsTo(Employees::class, 'designation_id'); } }
Here's my EmployeeController.php
:这是我的
EmployeeController.php
:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\{ Employees, Designations }; class EmployeesController extends Controller { public function index() { $employees = Employees::with('designations', 'positions')->get(); return array_reverse($employees); } }
I checked my api url, http://localhost:8000/api/employees and got this error:我检查了我的 api url, http://localhost:8000/api/employees 并得到了这个错误:
SQLSTATE[HY093]: Invalid parameter number (SQL: select * from
designations where
designations .
id in (52))
SQLSTATE[HY093]: Invalid parameter number (SQL: select * from
.
where
designations .id in (52))
Your relationships parameters are wrong.你的关系参数是错误的。 It's
它是
hasMany(class, foreignKey, relatedPrimaryKey)
# Employee
public function designations()
{
return $this->hasMany(Designations::class, 'employee_id', 'id');
}
public function positions()
{
return $this->hasMany(Positions::class, 'employee_id', 'id');
}
If you're eager loading more than 1 relationship, use array notation.如果您渴望加载超过 1 个关系,请使用数组表示法。
Also, $employees
will be an instance of a Collection
, so you can't use it as an argument to array_reverse
.此外,
$employees
将是Collection
的一个实例,因此您不能将它用作array_reverse
的参数。
You can either use collection methods to achieve the same result, or use $employees->all()
to get the underlying array.您可以使用集合方法来获得相同的结果,或者使用
$employees->all()
来获取底层数组。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employees;
class EmployeesController extends Controller
{
public function index()
{
$employees = Employees::with(['designations', 'positions'])->get();
return $employees->reverse()->values()->all();
// OR
return array_reverse($employees->all());
}
}
This is assuming your tables have a structure like this:这是假设您的表具有如下结构:
Schema::create('employees', function (Blueprint $table) {
$table->id();
...
});
Schema::create('designations', function (Blueprint $table) {
$table->id();
$table->foreignId('employee_id')->constained('employees');
...
});
Schema::create('positions', function (Blueprint $table){
$table->id();
$table->foreignId('employee_id')->constained('employees');
...
});
Since you're using increments
instead of id()
, the code has to be a little different.由于您使用的是
increments
而不是id()
,因此代码必须略有不同。
Schema::create('employees', function (Blueprint $table) {
$table->increments('id')->unique();
...
});
Schema::create('designations', function (Blueprint $table) {
$table->increments('id')->unique();
$table->unsignedInteger('employee_id');
$table->foreign('employee_id')->references('id')->on('employees');
...
});
Schema::create('positions', function (Blueprint $table){
$table->increments('id')->unique();
$table->unsignedInteger('employee_id');
$table->foreign('employee_id')->references('id')->on('employees');
...
});
I would recommend you to install phpstorm, it gives you hints of function parameters and you won't have this kind of issues anymore.我会建议你安装 phpstorm,它会给你提示 function 参数,你不会再有这种问题了。
correct format is:正确的格式是:
return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
in your designations model:在您的指定 model 中:
public function DesignationNames() { return $this->hasMany(\App\Models\Employees::class, 'designation_id', 'id'); }
When you retrieve them in your controller you need to use the with() method as:当您在 controller 中检索它们时,您需要使用 with() 方法:
Designations::with('DesignationNames')->get();指定::with('DesignationNames')->get();
And then to access properties in the related employee collection you would need to:然后要访问相关员工集合中的属性,您需要:
$designation->DesignationNames->DesignationProperty $designation->DesignationNames->DesignationProperty
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.