laravel belongsTo on User Model无法正常工作

时间:2017-11-01 11:10:42

标签: php laravel-5.4 belongs-to

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

以下是我的模特

    IF if exists(select * from INFORMATION_SCHEMA.VIEWS where TABLE_NAME='ACTIVE_GROUPS')
BEGIN
 ALTER  view ACTIVE_GROUPS AS
    SELECT id, name, status
    FROM test1.group
    WHERE status != 2 group by

END
ELSE
    CREATE  view ACTIVE_GROUPS AS
    SELECT id, name, status
    FROM test1.group
    WHERE status != 2 group by id
BEGIN
END

以下是我的公司模特

<?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");
    }
}

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

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

}

3 个答案:

答案 0 :(得分:1)

首先,你正在做什么

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

但它应该是

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

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

请同时检查User::find($user->id)是否给予您反对。如果没有,那么你就无法获得非对象的公司

答案 1 :(得分:1)

首先,如果用户属于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')

答案 2 :(得分:0)

实际上,如果您的company_id字段位于用户模型上,那么您的关系应为

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

除非用户可以拥有多家公司?