0

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)

Actually I am trying to get the username who has stored the result in the database i.e. 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 problemTrying to get property of non-objectwhen I try to get theusername` associated with the id. 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.

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

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

}
asad sajjad
  • 85
  • 2
  • 12
  • Add ___the complete error message to your question___ please – RiggsFolly Jan 19 '17 at 11:34
  • `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)` – asad sajjad Jan 19 '17 at 11:36
  • The error is in this line `{{$tn->users->username}}` ... I think it's not going into `users` function in the model to fetch the name but when I do `$tn->user_id` it returns me the `id' ... – asad sajjad Jan 19 '17 at 11:44
  • `foreach($n as $n)` ___Instant destruction___ – RiggsFolly Jan 19 '17 at 12:22
  • I think I have found it ... I am not getting `d_id` from `TN` table ... right now if you look at this line of code `$tn= TN::find($id);` then it is finding the same `$id` in `TN` table where I need to get `d_id` in `TN` table so that I can fetch out the `username` but still getting problem with fetching the `d_id` from `TN` table ... – asad sajjad Jan 19 '17 at 12:45

1 Answers1

0

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. Because it belongsTo to User class, it should be singular not plural and for hasMany we use plurals. Thank you for your help though ... :)

asad sajjad
  • 85
  • 2
  • 12