mardi 2 avril 2019

Error "Call to undefined method" when attempting hasFile() function

I'm taking my first steps in Laravel 5.8. Starting from the Laracast tutorials, I'm using the standard Laravel website template and user authentication out of the box, so nothing fancy here. After a user logs on, he can access a dashboard and change some of his own user information which is stored in the Users table in MySQL. Everything works fine except for a file upload. When checking whether the user has added a file to be uploaded, I get the error "Call to undefined method konsens24\User::hasFile()", i.e. it seems that Laravel is expecting the method to be defined in the user.php file which is not contained in there (and I wouldn't know how to put it in there?).

I've looked at various problem descriptions online, tried out some of them but unfortunately to no avail.

User.php file:

<?php

namespace konsens24;

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


class User extends Authenticatable
{
    use Notifiable;


    protected $fillable = [
    'first_name', 'last_name','name', 'email', 'password', 'street', 'postal', 'city', 'state', 'country', 'phone', 'mediator', 'mediator_cert'
    ];


protected $hidden = [
    'password', 'remember_token',
];


protected $casts = [
    'email_verified_at' => 'datetime',
];

public function projects()
{
  return $this->hasMany(Project::class, 'owner_id');
}
}

ProfileController.php file:

<?php

namespace konsens24\Http\Controllers;

use Illuminate\Http\Request;
use konsens24\User;


class ProfileController extends Controller
{
  public function __construct()
  {
    $this->middleware('auth');

  }


  public function show(User $user)
  {

      $user = auth()->user();

      return view('profile.edit', compact('user'));
  }


  public function update(User $user)
  {

    abort_if($user->id !== auth()->id(), 403);

    //Handle File Upload
      if($user->hasFile('mediator_cert')){
       //Get filename with the extension
       $filenameWithExt = $user->file('mediator_cert')->getClientOriginalName();

        //Get just filename
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);

        //Get just ext
        $extension = $user->file('mediator_cert')->guessClientExtension();
        //FileName to store
        $fileNameToStore = $filename.'_'.time().'.'.$extension;

        //Upload Image
         $path = $user->file('mediator_cert')->storeAs('public/certs/', $fileNameToStore);
      }

    $user->update($this->validateUser());

    return view('profile.edit', compact('user'));

  }


  protected function validateUser()
  {
    return request()->validate([
      'street' => ['required', 'min:3', 'max:30'],
      'postal' => ['required', 'min:3', 'max:7'],
      'city' => ['required', 'min:3', 'max:30'],
      'state' => ['max:30'],
      'country' => ['required', 'min:1', 'max:30'],
      'phone' => ['required', 'min:9', 'max:30'],
      'mediator' => ['boolean'],
      'mediator_cert' => ['nullable']
    ]);
  }
}

edit.blade.php file:

@extends('layouts.app')

@section('content')
  <h1 class="title">Profile Settings</h1>

  <p>Please complete your profile by adding the following information:</p>

  <form method="post" action="/profile/" enctype="multipart/form-data">
    @method('PATCH')
    @csrf

    ...

    <div class="field">
      <label class="label" for="mediator_cert">Certification (upload as PDF, Word Document DOCX, or JPG)</label>
      <div class="control">
        <input type="file" name="mediator_cert">
      </div>
    </div>

    <br>

    <div class="field">
        <div class="control">
        <button type="submit" class="button">Update Profile</button>
      </div>
    </div>

    @include('errors')

  </form>


@endsection

The expected result should be that the hasFile() method can be executed. Thanks a lot for helping me out on this one!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2TRNClJ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire