繁体   English   中英

用户模型上的 laravel 属于不工作

[英]laravel belongsTo on User Model not working

以下是我在 laravel 中的关系,我无法使用用户对象访问公司,但是当我尝试访问它时却为,请参阅下面的代码以获取图片

以下是我的模型

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use Session;
use Illuminate\Support\Facades\DB;

class User extends Authenticatable
{
    use Notifiable;
    use EntrustUserTrait;

    protected $table = 'tbl_users';
    protected $primaryKey = 'id';
    protected $guarded = ['id'];
    const API = 'api';
    const WEB = 'web';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'last_login', 'Address', 'Age', 'DateOfBirth', 'created_by', 'deleted_by'
    ];

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

    protected $casts = [
        'is_admin' => 'boolean',
    ];

    public function isAdmin()
    {
        return $this->is_admin;
    }

    static function GetUserNamebyID($id)
    {
        $name = User::select("name")->where(["id" => $id])->pluck('name');
        if (isset($name[0])) {
            return $name[0];
        } else {
            return '';
        }
    }



    public function loginlogout()
    {
        $this->hasMany("App\Models\LoginLogoutLogs", 'userID');
    }

    public function company()
    {
        $this->hasMany("App\Models\Company", "company_id");
    }
}

以下是我的公司模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use DB;
use App\Models\CarePlanData;
use Session;

class Company extends Model
{
    protected $table = 'companies';
    protected $primaryKey = 'id';
    protected $guarded = ['id'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'phone_no', 'address', 'password', 'description', 'city', 'company_logo', 'country', 'email'
    ];

    static public function fetchAllActiveCompanies()
    {

        return DB::table("companies")->where(['is_active' => 1])->pluck('name', 'id');
    }

// change company to hasmany

    public function users()
    {
        return $this->hasmany('App\Models\User');
    }

}

这就是我试图访问公司的方式,但我得到了null

public function fetchCompany(){
 $User = User::find($user->id);
        dd($User->company());
}

您的实际问题是:您有属于用户和公司之间的关系,但是当您尝试通过用户对象访问公司时,您会得到 null

在您的 User.php 模型中放置以下功能,但如果您已经拥有,则保留它:

public function company()
{
    $this->belongsTo("App\Models\Company", "company_id");
}

然后

替换这个函数:

public function fetchCompany(){
 $User = User::find($user->id);
        dd($User->company());
}  

到是一个:

public function fetchCompany(){
    $User = User::find($user->id);
    if($User){ 
        dd($User->company()->get());
    }
} 

或者到这个:

 public function fetchCompany(){
    $User = User::find($user->id);
    if($User){ 
        dd($User->company);
    }
} 

首先,如果用户属于 1 个公司,那么它应该是:

public function company()
{
    $this->belongsTo("App\Models\Company", "company_id");
}

那么fetchCompany()应该是

public function fetchCompany(){
   $User = User::with('company')->find($user->id);
   dd($User->company);
}

您需要使用with来加载关系。 您将定义User模型中关系的函数的名称传递给with像这样with('function_name')

实际上,如果您的company_id字段在用户模型上,那么您的关系应该是

public function company()
    {
        $this->belongsTo("App\Models\Company", "company_id");
    }

除非一个用户可以有多少家公司?

暂无
暂无

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

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