phplaravellaravel-5.8

ReflectionException Class App\User does not exist


hello i have this error : ReflectionException Class App\User does not exist Previous exceptions syntax error, unexpected '{', expecting ')' (0)

but dont understand where is syntax error ,

User.php

<?php

namespace App;

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

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'nom', 'prenom', 'adresse', 'ville', 'codepostale', 'datedenaissance','email', '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',
    ];

    protected static function boot()
    {
        parent::boot();
        static::created(function ($user {
            $user->profile()->create([
                'title' => 'Profil de' . $user->username
            ]);
        });
    }

    public function getRouteKeyName()
    {
        return 'username';
    }

    public function profile()
    {
        return $this->hasOne('App\Profile');
    }

    public function posts()
    {
        return $this->hasMany('App\Post')->orderBy('created_at', 'DESC');
    }
}

ProfileController.php

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;

class ProfileController extends Controller
{
    public function show(User $user)
    {
        return view('profile.show', compact('user'));
    }

    public function edit(User $user)
    {
        $this->authorize('update', $user->profile);
        return view('profile.edit', compact('user'));
    }

    public function update(User $user)
    {
        $this->authorize('update', $user->profile);
        $data = request()->validate([
            'title' => 'required',
            'description' => 'required',
            'image' => 'sometimes|image|max:3000'
        ]);

        if (request('image')) {
        $imagePath = request('image')->store('avatars', 'public');

        $image = Image::make(public_path("/storage/{$imagePath}"))->fit(800, 800);
        $image->save();

        auth()->user()->profile->update(array_merge($data,
            ['image' => $imagePath]
        ));
        } else {
            auth()->user()->profile->update($data);
        }

        auth()->user()->profile->update($data);

        return redirect()->route('profile.show', ['user' => $user]);
    }
}

show.blade.php

<@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-4">
           <img src="{{ $user->profile->getImage() }}" class="rounded-circle">
        </div>

        <div class="col-8">
            <div class="d-flex align-items-baseline">

                <div class="h4 mr-3 pt-2">{{ $user->username }}</div>
                <button class="btn btn-primary">S'abonner</button>
            </div>
            <div class="d-flex">
                <div class="mr-3">{{ $user->posts->count() }} article(s) en vente
            </div>

            @can('update', $user->profile)
            <a href=" {{ route('profile.edit', ['username' => $user->username]) }}">Modifier Profile</a>
            @endcan

            <div class="mt-3">
                <div class="font-weight-bold">
                    {{ $user->profile->title }}
                </div>
                <div class="font-weight-bold">
                    {{ $user->profile->description }}
                </div>
            </div>

        </div>
    </div>

    <div class="row mt-5">
        @foreach ($user->posts as $post)

        <div class="col-4">
            <a href="{{ route('posts.show', ['post' => $post->id]) }}"> <img src="{{ asset('storage') . '/' . $post->image }}" class="w-100"></a>
        </div>
        @endforeach
    </div>



</div>
@endsection

Profile.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    protected $fillable = ['title']; 

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function getImage()
    {
        $imagePath = $this->image ?? 'avatars/default.png';

        return "/storage/" . $imagePath;
    }
}

i try to create profile with upload image, someone can help me with this error?


Solution

  • Your IDE should give you an error in your overridden boot method, so change this:

    static::created(function ($user {
        $user->profile()->create([
            'title' => 'Profil de' . $user->username
        ]);
    });
    

    to this:

    static::created(function ($user) {
       $user->profile()->create([
               'title' => 'Profil de' . $user->username
       ]);
    });
    

    Note the missing ) in your $user param.