- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
大家好,以下是我的亲戚
用户模型
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/
我正在使用 slim 框架开发一个 Restful 应用程序。我正在使用一个使用 illuminate 数据库的 session 管理器。它要求我使用 Laravel 的 illuminate 实例创
我为一个新项目创建了 Lumen 的全新安装,我正在尝试将 Redis 设置为缓存/ session 驱动程序。通过 Composer ,我能够毫无问题地安装 Predis,然后在尝试运行时: com
在我的独立(没有 Laravel)项目中,我想使用 Illuminate IoC 容器。我还想通过 illuminate/support 组件提供的 App facade 访问应用程序容器。我安装了这
我更新了 composer.json删除illuminate\html并添加: "require": { "laravelcollective/html": "5.1.*" 我从 app.ph
我知道这里有几个类似的问题,但没有一个能解决我的问题。 我正在尝试在 Ubuntu 14.04 上使用 Laravel 5 添加 HtmlServiceProvider。我不断收到以下错误: dl@d
Controller 代码: return redirect()->route('admin.patient.edit', $patientId); 测试代码: $this->visit(route(
我正在尝试开始使用 laravel php 框架。我试图调用索引文件,但收到此错误消息。 C:\xampp\php\php.exe C:\xampp\htdocs\tutorials\lara\app
我有一个使用 OAuth2 执行的 Facebook 登录,然后我使用 Laravel 4 的内置身份验证系统在重新访问时重新登录用户。对于大多数用户,我认为我所拥有的没有任何问题,但对于一位用户,他
我正在使用 smtp 协议(protocol)使用 mailtrap 发送邮件。它在本地主机中运行良好,但出现错误。 Symfony\Component\Debug\Exception\FatalTh
我对 Laravel 还很陌生,我正在尝试将用户创建的帖子添加到数据库中。但是当我这样做时,出现以下错误: Argument 1 passed to Illuminate\Database\Eloqu
我是 laravel 的初学者。我正在使用 guard 进行多重授权。我在我的管理员登录中遇到了这个问题:类型错误Illuminate\Auth\SessionGuard::login():参数 #1
题目地址:https://leetcode.com/problems/available-captures-for-rook/ 题目描述 Ona N x N grid of cells, each
我有一个网络应用程序可以在我的本地计算机上运行,但不能在我的服务器上运行。我在服务器上收到此错误。我检查了错误提到的代码,没有任何问题。 你知道是什么原因造成的吗? 非常感谢您。 这是我的错误信息
我正在我的服务器上安装一个 PHP 脚本作为测试,但我遇到了这个错误。我无法理解脚本代码有什么问题。 我在应用程序上启用 Debug模式并收到此错误消息: Call to undefined func
我尝试获取如下数据数组: $coupons = $user->coupons::where('is_activated_flg', 1)->where('is_used_flg', 0)->lists
我正在将我的 Laravel 从 5.2 升级到 5.3,并且已经阅读了 https://laravel.com/docs/5.3/upgrade 上的文档。 ,但我在运行我的应用程序时仍然收到此消息
Ive Laravel队列正在运行,但是在数据库连接上,这是配置: 'database' => [ 'driver' => 'database', 'connection' => 'm
登录后,我一直在尝试在用户模型中执行方法。据我了解,您执行了以下操作:Auth::user()->foo(); 由于某种原因,这不起作用。 代码 User.php '[email protected
登录后,我一直在尝试在用户模型中执行方法。据我了解,您执行了以下操作:Auth::user()->foo(); 由于某种原因,这不起作用。 代码 User.php '[email protected
我将 Laravel 站点部署到 cPanel 共享主机,并将我的数据库迁移到主机的数据库。当我尝试通过登录密码进入网站时,错误是: Symfony \ Component \ Debug \ Exc
我是一名优秀的程序员,十分优秀!