php - Laravel 将值传递给模型

标签 php laravel

现在我用 Eloquent 方式尝试这个查询:

'MentorId'  => $employee->intern(true)->mentor(true)->MentorId,

在我的 employeeintern 模型中,我得到了这个:

实习生

  /**
     * @return mixed
     */
    public function intern($withTrashed = false)
    {
        if($withTrashed == true)
        {
            return $this->belongsTo(internModel::class, 'InternId')->withTrashed();
        }

        return $this->belongsTo(internModel::class,'InternId');
    }

导师

 /**
     * @return mixed
     */
    public function mentor($withTrashed = false)
    {
        if($withTrashed == true)
        {
            return $this->belongsTo(mentorModel::class, 'MentorId')->withTrashed();
        }

        return $this->belongsTo(mentorModel::class,'MentorId');
    }

但是它崩溃了:

BadMethodCallException in Builder.php line 2148:
Call to undefined method Illuminate\Database\Query\Builder::mentor()

我该如何解决这个问题?

--编辑--

员工

<?php

namespace App\src\employee;

use Illuminate\Foundation\Auth\User as Authenticatable;
use App\src\department\Department as departmentModel;
use App\src\employee\Employee as employeeModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\src\intern\Intern as internModel;
use App\src\mentor\Mentor as mentorModel;
use App\src\employee\Role as roleModel;

class Employee extends Authenticatable
{
    use SoftDeletes;
    use EmployeeServiceTrait;

    /**
     * table name
     */
    protected $table = 'employee';

    /**
     * Mass assignment fields
     */
    protected $fillable = ['RoleId', 'DepartmentId', 'InternId', 'FirstName', 'LastName', 'Bio','api_token', 'email', 'LinkedIn', 'password', 'Address', 'Zip', 'City', 'ProfilePicture', 'BirthDate', 'StartDate', 'EndDate', 'Suspended','LinkedIn'];

    /**
     * Primarykey
     */
    protected $primaryKey = 'EmployeeId';

    /**
     * Deleted_at
     */
    protected $dates = ['deleted_at'];

    /**
     * @return mixed
     */
    public function role()
    {
        return $this->belongsTo(roleModel::class,'RoleId');
    }

    /**
     * @return mixed
     */
    public function intern($withTrashed = false)
    {
        if($withTrashed == true)
        {
            return $this->belongsTo(internModel::class, 'InternId')->withTrashed();
        }

        return $this->belongsTo(internModel::class,'InternId');
    }

    /**
     * @return mixed
     */
    public function department()
    {
        return $this->belongsTo(departmentModel::class,'DepartmentId');
    }

    /**
     * @return mixed
     */
    public function mentor()
    {
        return $this->belongsTo(mentorModel::class,'MentorId');
    }

    /**
     * @return mixed
     */
    public function employees()
    {
        return $this->hasManyThrough(employeeModel::class,departmentModel::class,'CompanyId','DepartmentId');
    }

    /**
     * @param $role
     * @return bool
     */
    public function hasRole($role)
    {
        if(strtolower($this->role->RoleName) == strtolower($role))
        {
            return true;
        }
        return false;
    }
}

最佳答案

您遇到的问题是任何 Eloquent 关系对象实际上都是 Relation 的实例。这意味着当您创建关系时,您实际上返回一个集合(Builder 的实例);判断你的错误:

BadMethodCallException in Builder.php line 2148: Call to undefined method Illuminate\Database\Query\Builder::mentor()

简单的解决方案,无需对代码进行任何修改,如下所示:

'MentorId'  => $employee->intern(true)->first()->mentor(true)->first()->MentorId;

但是,您可以使用如下重载:

'MentorId'  => $employee->intern->mentor->MentorId;

尽管这包括您的withTrashed。但是,您可以将您的关系调整为:

public function intern($withTrashed = false)
{
    $relation = $this->belongsTo(internModel::class, 'InternId');

    if($withTrashed == true)
    {
        return $relation->withTrashed()->first();
    }

    return $relation->first();
}

但我不建议这样做,因为稍后如果您尝试使用 WhereHas 之类的东西,您会收到错误。也就是说,另一种方法是按照以下方式做一些事情:

public function intern()
{
    return $this->belongsTo(internModel::class, 'InternId');
}

然后像这样被扔掉:

'MentorId' => $employee->intern()->withTrashed()->first()->mentor()->withTrashed()->first()->MentorId;

关于php - Laravel 将值传递给模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36398168/

相关文章:

PHP 版本 5.2.14/解析错误 : syntax error, unexpected T_FUNCTION, expecting ')'

PHP mysqli 加载缓慢。

javascript - 检查多个复选框 jQuery

php - 如何从 Maatwebsite Laravel Excel 中的单元格公式中获取计算值?

php - 如何存储日期/出勤?

php - 我的 php 端表单不接受 CSS 样式

php - 在 Laravel 关系中显示一对一关系的 VIEW

php - 对多个值进行排序 MYSQL Laravel

php - Laravel,使用 session 将输入变量从页面传递到其他页面

Laravel 5.7 - 如何使用 axios.get 从 API 检索数据?