繁体   English   中英

如何在 Laravel 中正确使用 with() 关系?

[英]How to correctly use relationships with() in Laravel?

我只是想澄清使用表中的关系。 现在,我想从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'); } }

这是我的编号 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'); } }

这是我的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); } }

我检查了我的 api url, http://localhost:8000/api/employees 并得到了这个错误:

SQLSTATE[HY093]: Invalid parameter number (SQL: select * from . where designations .id in (52))

你的关系参数是错误的。 它是

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');
}

如果您渴望加载超过 1 个关系,请使用数组表示法。

此外, $employees将是Collection的一个实例,因此您不能将它用作array_reverse的参数。

您可以使用集合方法来获得相同的结果,或者使用$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());
    }
}

这是假设您的表具有如下结构:

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');
    ...
});

由于您使用的是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');
    ...
});

我会建议你安装 phpstorm,它会给你提示 function 参数,你不会再有这种问题了。

正确的格式是:

 return $this->hasMany('App\Comment', 'foreign_key', 'local_key');

在您的指定 model 中:

 public function DesignationNames() { return $this->hasMany(\App\Models\Employees::class, 'designation_id', 'id'); }

当您在 controller 中检索它们时,您需要使用 with() 方法:

指定::with('DesignationNames')->get();

然后要访问相关员工集合中的属性,您需要:

$designation->DesignationNames->DesignationProperty

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM