2017-10-30 1 views
0

안녕 다음은 내 관계

사용자 모델

public function loginlogout() 
    { 
     $this->HasMany("App\Models\LoginLogoutLogs"); 
    } 

하고이 내 LoginLogoutLogs 모델

입니다
public function users() 
    { 
     return $this->belongsTo('App\Models\User'); 
    } 

사용자 이름에 액세스하려고합니다.

$loginLogoutLogs = LoginLogoutLogs::all(); 
     foreach($loginLogoutLogs as $loginLogoutLog){ 
      dd($loginLogoutLog->users()->name); 
     } 

하지만 난이 오류를 얻고있다

정의되지 않은 속성 : \ 데이터베이스를 조명 \ 웅변 \ 관계 \에 BelongsTo : $ 이름

편집 추가 모델

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

이제 로그인 로그 아웃 모델

<?php 


namespace App\Models; 

use Illuminate\Notifications\Notifiable; 
use Zizaco\Entrust\Traits\EntrustUserTrait; 
use Illuminate\Database\Eloquent\Model; 
use Session; 
use Illuminate\Support\Facades\DB; 

class LoginLogoutLogs extends Model 
{ 
    use Notifiable; 
    use EntrustUserTrait; 

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


    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'userID','is_accpeted','type','addedFrom' 
    ]; 

    /** 
    * 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; 
    } 

    // change company to hasmany 

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

} 

답변

0

쉬운 수정 : 관계 모델에 반대

$loginLogoutLogs = LoginLogoutLogs::all(); 
foreach($loginLogoutLogs as $loginLogoutLog){ 
    dd($loginLogoutLog->users->name); 
} 

당신은, 관계 기관에 액세스하려는. users()를 사용하여

, 당신의 코드는 당신이 LoginLogoutLogs 클래스에 users 방법에 반대의 users 모델에 name() 메소드를 호출하려고 생각합니다.

+0

시험해 보겠습니다. –

+0

' non-object' 당신이 제안한 편집을 사용하십시오 –

+0

이 답변은 모든 사용자가'User' 관계로'LoginLogoutLogs'를 가지고 있다면'$ loginLogoutLog-> 때문에'객체가 아닌 속성을 얻으려고합니다' '$ loginLogoutLog'에 사용자 관계가 없거나 모델에 관계를 잘못 정의했기 때문에 사용자는 return null입니다. – Nerea

1

당신은 LoginLogoutLogs에 외래 키를 추가하는 사용자와의 관계를 변경해야

또한
public function user() 
{ 
    return $this->belongsTo('App\Models\User', 'userID'); 
} 

당신은 사용자가 사용자

$loginLogoutLogs = LoginLogoutLogs::all(); 
foreach($loginLogoutLogs as $loginLogoutLog){ 
    dd($loginLogoutLog->user->name); 
} 

와의 insted 호출 할 수 있도록 당신이 열망 사용을 수행하려는 경우 로드 중 :

$loginLogoutLogs = LoginLogoutLogs::with('user')->get(); 
foreach($loginLogoutLogs as $loginLogoutLog){ 
    dd($loginLogoutLog->user->name); 
} 
관련 문제