【问题标题】:Trying to get the username using MODELS but getting this error: Trying to get property of non-object尝试使用 MODELS 获取用户名但出现此错误:尝试获取非对象的属性
【发布时间】:2017-06-04 02:12:05
【问题描述】:

错误: 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,因此我需要使用models but I am getting this problemuser_id 获取username,以获取非对象@ 的属性987654326@username` 与 id 关联。我不知道我在哪里做错了。

我收到的错误是 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);
}

}

【问题讨论】:

  • 请在您的问题中添加完整的错误消息
  • 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-&gt;users-&gt;username}} ...我认为它不会进入模型中的users函数来获取名称但是当我这样做时$tn-&gt;user_id它返回给我`id ' ...
  • foreach($n as $n) 即时销毁
  • 我想我找到了...我没有从TN 表中得到d_id...现在如果您查看这行代码$tn= TN::find($id); 那么它正在寻找$idTN 表中相同,我需要在TN 表中获取d_id,以便我可以取出username,但从TN 表中提取d_id 仍然存在问题...

标签: php database model laravel-5.2 laravel-migrations


【解决方案1】:

您好,我已经解决了这个问题,但忘记在这里发布,问题就在这里

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

这应该是public function user() 而不是users。因为它 belongsToUser 类,它应该是单数而不是复数,对于 hasMany 我们使用复数。谢谢你的帮助...... :)

【讨论】:

    猜你喜欢
    • 2018-01-16
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多