簡體   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