N
N
Nikolai Steshenko2020-05-25 19:37:09
Laravel
Nikolai Steshenko, 2020-05-25 19:37:09

Why is there no object to display in the view?


Trying to get property 'title'

of non-object (View: /home/c/cm48735/test/public_html/resources/views/admin/users.blade.php) (0)



Model

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use \Storage;
use \App\Role;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
  'name', 'email',  'biografi', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    
  public function UserRole()
  {
    return $this->belongsTo(Role::class, 'role_id');
  }
  
  public function edit($fields)
  {
    $this->fill($fields);
    $this->save();
  }
  
  public function generatePassword($password)
  {
    if ($password != null) {
      $this->password = bcrypt($password);
      $this->save();
    }
  }
  
  
  public function uploadAvatar($image)
  {
    if ($image == null) {
       return; }

    $this->removeAvatar();

    $filename = str_random(10) . '.' . $image->extension();
    $image->storeAs('uploads/user/avatar', $filename);
    $this->avatar = $filename;
    $this->save();
  }
  
  public function removeAvatar()
  {
    if ($this->avatar != null) {
      Storage::delete('uploads/user/avatar' . $this->avatar);
    }
  }
  
  public function getAvatar()
  {
    if ($this->avatar == null) {
      return '/images/no-avatar.jpg';
    }

    return '/public/uploads/user/avatar/' . $this->avatar;
  }
  
  
  
}


View

@extends('admin.layouts.app')

@section('content')

<div class="container">
<h2 style="float:left">Пользователи</h2>
<div class="text-right">
  <a  href="{{ Route('user.create') }}" class="btn btn-success">Регистрация</a></div>
<hr>

  
<div class="content">

    <table class="table">
      <thead class="thead-dark">
        <tr>
          <th scope="col">Аватар</th>
          <th scope="col">Имяst</th>
          <th scope="col">Статус</th>
          <th scope="col">Handle</th>
        </tr>
      </thead>
      <tbody>
      @foreach($users as $user) 
        <tr>
          <td scope="row" style="width:32px; height:32px;"> <img style="width:32px; height:32p;" src="{{ $user->getAvatar() }}" alt="{{ $user->name }}" /></td>
          <td >{{$user->name}}</td>
          <td> {{$user->UserRole->title }}</td>
          
          
        </tr>
        
        @endforeach
      </tbody>
    </table>
</div>


<footer>
  
    {{ $users->links() }}
  
</footer>
  
  
</div>

@endsection


laravel version 7

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Bukharev, 2020-05-25
@evgenybuckharev

$user->UserRole->title
Apparently UserRole does not exist for $user

N
Nikolai Steshenko, 2020-05-25
@niksongames

$user->UserRole
Works fine

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question