![](/img/trans.png)
[英]Laravel 8 belongsTo relationship not returning data on User model
[英]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.