Почему нет объекта для вывода в вид?

Trying to get property 'title' of non-object (View: /home/c/cm48735/test/public_html/resources/views/admin/users.blade.php)
Previous exceptions

Trying to get property 'title' of non-object (0)



Модель

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


Вид

@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
  • Вопрос задан
  • 95 просмотров
Пригласить эксперта
Ответы на вопрос 2
$user->UserRole->title
Видимо UserRole не существует у $user
Ответ написан
niksongames
@niksongames Автор вопроса
$user->UserRole

Работает нормально
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы