Trying to get the username using MODELS but getting this error: Trying to get property of non-object
Trying to get the username using MODELS but getting this error: Trying to get property of non-object
错误: ErrorException in 814a6fb85b2cceb262c3a8191c08e42742940fc7.php line 223: Trying to get property of non-object (View: /var/www/html/m/TS/resources/views/d/show-details.blade.php)
实际上我正在尝试获取将结果存储在数据库中的用户名,即 TN has got user_id as a foreign key in the table
所以我需要使用 [=19= 从 user_id
中获取 username
]尝试获取与 ID 关联的 属性 非对象 when I try to get the
username`。我不知道我哪里做错了。
我遇到的错误在这里 value="{{$tn->users->username}}"
显示在缓存文件中。
我也在下面给出了我的代码以供查看。
提前致谢
控制器
public function details($id) {
$d= $this->detail->showDetails($id);
$tn= TN::find($id);
// calling functions from Model
$n = $d->tN;
$o = $d->tO;
return view('details.show-details', compact('d','o', 'n', 'tn'));
}
查看
foreach($n as $n)
<input style="font-size:10px;font-weight: bold; color:black; background:#59d864;" value="{{$tn->users->username}}" readonly>
型号
用户模型
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\TN;
use App\TO;
use App\UserType;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function tN() {
return $this->hasMany(TN::class);
}
public function tO() {
return $this->hasMany(TO::class);
}
public function userType() {
return $this->belongsTo(UserType::class);
}
}
TO 型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\D;
use App\TT;
use App\User;
class TO extends Model
{
protected $fillable = ['o', 'date'];
public function d() {
return $this->belongsTo(D::class);
}
public function tOType() {
return $this->belongsTo(TOType::class);
}
public function users() {
return $this->belongsTo(User::class);
}
}
TN 型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Debtor;
use App\TracingType;
use App\User;
class TN extends Model
{
protected $fillable = ['r', 'date'];
public function d() {
return $this->belongsTo(D::class);
}
public function tType() {
return $this->belongsTo(TType::class);
}
public function users() {
return $this->belongsTo(User::class);
}
}
你好,我已经解决了这个问题,但是忘记了 post 问题就在这里
public function users() {
return $this->belongsTo(User::class);
}
这应该是 public function user()
而不是 users
。因为它 belongsTo
到 User
class,它应该是单数而不是复数,对于 hasMany
我们使用复数。谢谢你的帮助... :)
错误: ErrorException in 814a6fb85b2cceb262c3a8191c08e42742940fc7.php line 223: Trying to get property of non-object (View: /var/www/html/m/TS/resources/views/d/show-details.blade.php)
实际上我正在尝试获取将结果存储在数据库中的用户名,即 TN has got user_id as a foreign key in the table
所以我需要使用 [=19= 从 user_id
中获取 username
]尝试获取与 ID 关联的 属性 非对象 when I try to get the
username`。我不知道我哪里做错了。
我遇到的错误在这里 value="{{$tn->users->username}}"
显示在缓存文件中。
我也在下面给出了我的代码以供查看。
提前致谢
控制器
public function details($id) {
$d= $this->detail->showDetails($id);
$tn= TN::find($id);
// calling functions from Model
$n = $d->tN;
$o = $d->tO;
return view('details.show-details', compact('d','o', 'n', 'tn'));
}
查看
foreach($n as $n)
<input style="font-size:10px;font-weight: bold; color:black; background:#59d864;" value="{{$tn->users->username}}" readonly>
型号
用户模型
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\TN;
use App\TO;
use App\UserType;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function tN() {
return $this->hasMany(TN::class);
}
public function tO() {
return $this->hasMany(TO::class);
}
public function userType() {
return $this->belongsTo(UserType::class);
}
}
TO 型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\D;
use App\TT;
use App\User;
class TO extends Model
{
protected $fillable = ['o', 'date'];
public function d() {
return $this->belongsTo(D::class);
}
public function tOType() {
return $this->belongsTo(TOType::class);
}
public function users() {
return $this->belongsTo(User::class);
}
}
TN 型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Debtor;
use App\TracingType;
use App\User;
class TN extends Model
{
protected $fillable = ['r', 'date'];
public function d() {
return $this->belongsTo(D::class);
}
public function tType() {
return $this->belongsTo(TType::class);
}
public function users() {
return $this->belongsTo(User::class);
}
}
你好,我已经解决了这个问题,但是忘记了 post 问题就在这里
public function users() {
return $this->belongsTo(User::class);
}
这应该是 public function user()
而不是 users
。因为它 belongsTo
到 User
class,它应该是单数而不是复数,对于 hasMany
我们使用复数。谢谢你的帮助... :)