gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:54:47 27 4
gpt4 key购买 nike

大家好,以下是我的亲戚

用户模型

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

但是我收到了这个错误

Undefined property: 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');
}

}

最佳答案

只需改变你的部分

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

进入

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

移除用户的支架,这很容易修复。这里我们获得了一个属性,而不是一个函数....(尽管在模型中它被定义为函数)

关于php - 未定义的属性:Illuminate\Database\Eloquent\Relations\BelongsTo::$name laravel 5.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47010807/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com