简体   繁体   English

尝试使用MODELS获取用户名,但出现此错误:尝试获取非对象的属性

[英]Trying to get the username using MODELS but getting this error: Trying to get property of non-object

ERROR: 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) 错误: 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)

Actually I am trying to get the username who has stored the result in the database ie TN has got user_id as a foreign key in the table so I need to get the username from that user_id using models but I am getting this problem Trying to get property of non-object when I try to get the username` associated with the id. 实际上,我正在尝试获取将结果存储在数据库中的用户名,即TN has got user_id as a foreign key in the table因此我需要使用models but I am getting this problem从该user_id获取usernamemodels but I am getting this problem尝试获取属性when I try to get the与ID关联when I try to get the用户名when I try to get the的非对象。 I dont know where I am doing it wrong. 我不知道我在哪里做错了。

THE ERROR I AM GETTING IS HERE value="{{$tn->users->username}}" WHICH SHOWS IN THE CACHED FILE. 我在这里得到的错误是value="{{$tn->users->username}}"显示在缓存的文件中。

I have given my code below too to look. 我在下面也给出了我的代码。

Thank you in advance 先感谢您

Controller 控制者

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

}

View 视图

foreach($n as $n)
<input style="font-size:10px;font-weight: bold; color:black; background:#59d864;" value="{{$tn->users->username}}" readonly>

Models 楷模

User Model 用户模型

<?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 Model 模拟

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

}

Hello I have sorted this problem but forgot to post it in here the problem was here 您好,我已经对这个问题进行了排序,但是忘记将其发布在这里,问题在这里

public function users() {
    return $this->belongsTo(User::class);
}

This should be public function user() rather than users . 这应该是public function user()而不是users Because it belongsTo to User class, it should be singular not plural and for hasMany we use plurals. 因为它belongsTo User类,所以它应该是单数形式而不是复数形式,并且对于hasMany我们使用复数形式。 Thank you for your help though ... :) 谢谢您的帮助,但是... :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM