未定义的属性:Illuminate \ Database \ Eloquent \ Relations \ BelongsTo :: $ name laravel 5.4

时间:2017-10-30 08:29:13

标签: php laravel-5.4 has-many belongs-to

以下是我的关系

用户模型

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

但我收到此错误

  

未定义属性:Illuminate \ Database \ Eloquent \ Relations \ BelongsTo :: $ name

编辑添加模型

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

}

3 个答案:

答案 0 :(得分:7)

只需更改您的

部分
dd($loginLogoutLog->users()->name);

进入

dd($loginLogoutLog->users->name);

删除用户的括号,这很容易解决。 在这里,我们获得的是属性,而不是函数。...(尽管在模型中将其定义为函数)

答案 1 :(得分:4)

轻松修复:

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

您希望访问关系实体,而不是关系模型。

使用users(),您的代码认为您尝试在name()模型上调用users方法,而不是users上的LoginLogoutLogs方法}。class。

答案 2 :(得分:2)

您需要更改与用户在LoginLogoutLogs中添加外键的关系:

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

同时确保您呼叫用户的用户

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

如果你想进行使用急切加载:

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