【问题标题】:laravel one-to-many relationship is nulllaravel 一对多关系为空
【发布时间】:2020-07-21 04:34:49
【问题描述】:

我有 3 个模型:用户、公司和分支。

在刀片文件中,我希望能够显示用户所在公司所属的分支。

在我看来我应该有以下关系:

User -> Company :用户属于一个公司,一个公司有很多用户,所以这是一对多的关系。

公司 -> 分公司:一个公司属于一个分公司,一个分公司可以有多个公司。所以又是一对多的关系。

我在用户表中有外键:company_id,它引用了公司表上的 id。 company 表中的另一个 FK:branch_id,它引用了 branch 表上的 id。

在我的刀片文件中,希望像这样显示分支名称:{{ $user->company->branch->name }} 其中只有 name 是一个属性。公司和分公司是关系。

我的查询如下所示:

 $users = User::with(['company','company.branche' => function($q){
            $q->select('name');
        }])->inRandomOrder()->paginate(24);
<?php

namespace App;

use App\Events\GeneralEvent;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Nicolaslopezj\Searchable\SearchableTrait;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable, SoftDeletes, HasRoles, SearchableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'gender', 'first_name','last_name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */

...



    public function company()
    {
        return $this->belongsTo(Company::class,'company_id');
    }   

}
<?php

namespace App;

use App\Events\GeneralEvent;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Nicolaslopezj\Searchable\SearchableTrait;

class Company extends Model
{
    use SoftDeletes, SearchableTrait;


    ...

    public function branche()
    {
        return $this->belongsTo(Branche::class);
    }   
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Nicolaslopezj\Searchable\SearchableTrait;

class Branche extends Model
{
    protected $fillable = [
        'name'
    ];

    protected $searchable = [
        'columns' => [
            'name' => 10,
        ],
    ];

    public function companies()
    {
        return $this->hasMany(Company::class);
    }
}

但是,当我转储 $user->company 时,我得到了空值。所以在那之后添加分支现在是没有意义的。当我转储用户时,关系显示但为空。我不知道我哪里出错了。有人可以帮忙吗?

【问题讨论】:

  • 你可以展示你的模型吗?
  • 你试过在你的用户模型中没有'company_id'吗?

标签: laravel eloquent foreign-keys relational-database


【解决方案1】:

所以问题是我的一个外键指向错误的表。呵呵。

【讨论】:

    【解决方案2】:

    您必须包含分支机构的 id,以便 eloquent 可以链接分支机构和公司 你可以这样做

     $users = User::with(['company','company.branche' => function($q){
                $q->select('id','name');
            }])->inRandomOrder()->paginate(24);
    

    或者像那样

     $users = User::with('company.branche:id,name')->inRandomOrder()->paginate(24);
    

    【讨论】:

    • 您好 Masmoud,很遗憾这没有帮助。
    • 你试过第二个了吗,如果是,你使用的是哪个版本的 laravel,你能告诉我公司、分支和用户的属性吗
    • 我在这个项目中使用 laravel 7 发布了模型。
    • 必须在user的可填写变量中添加company_id,能否查看数据库中user的company_id是否填写
    • 已排序。在同事的帮助下。我会尝试准确指出哪里出了问题,所以我可以在这里接受答案。大概会在周末。非常感谢你们。
    猜你喜欢
    • 1970-01-01
    • 2018-06-15
    • 2018-09-27
    • 2017-12-24
    • 2018-04-20
    • 1970-01-01
    相关资源
    最近更新 更多