Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name laravel 5.4

noobie-php picture noobie-php · Oct 30, 2017 · Viewed 20.6k times · Source

Hi following are my relations

User Model

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

and this is my LoginLogoutLogs Model

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

I am trying to access name from Users like this

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

but i am getting this error

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name

EDIT Adding Models

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

And now LoginLogouts Model

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

}

Answer

Toni Tegar Sahidi picture Toni Tegar Sahidi · Jan 19, 2019

simply change your part of

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

into

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

remove the bracket on users, its the easy fix. here we obtain a property, not a function.... (although in the model its defined as function)