0

I was following a tutorial on how to do so; I created a separate table for the user's profile model;

public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('profile_image')->nullable();//profile image
            $table->string('address')->unique();
            $table->string('phoneno')->unique();
            $table->string('sex');
            $table->string('martial_status');
            $table->timestamps();
        });
    }

then i copied this file UploadTrait.php in the Traits folder but the code didn't highlight in my vs code editor

namespace App\Traits;

use Illuminate\Support\Str;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

trait UploadTrait
{
    public function uploadOne(UploadedFile $uploadedFile, $folder = null, $disk = 'public', $filename = null)
    {
        $name = !is_null($filename) ? $filename : Str::random(25);

        $file = $uploadedFile->storeAs($folder, $name.'.'.$uploadedFile->getClientOriginalExtension(), $disk);

        return $file;
    }
}

Here is my Profilescontroller

 public function updateProfile(Request $request)
    {
        // Form validation
        $request->validate([
            'profile_image'    => 'required|image|mimes:jpeg,png,jpg,gif|max:2040',
            'address'          => 'required',
            'phoneno'          => 'required',
            'sex'              => 'required',
            'martial_status'   => 'required'
        ]);

        // Get current user
        $user = User::findOrFail(auth()->user()->id);
        // Set user name
        $user->accno = $request->input('accno');

        //check if a profile image has been uploaded
        if ($request->has('profile_image')) {
            // Get image file
            $image = $request->file('profile_image');
            // Make a image name based on user name and current timestamp
            $name = Str::slug($request->input('accno')).'_'.time();
            // Define folder path
            $folder = '/uploads/images/';
            // Make a file path where image will be stored [ folder path + file name + file extension]
            $filePath = $folder.$name. '.' . $image->getClientOriginalExtension();
            // Upload image
            $this->uploadOne($image, $folder, 'public' , $name);
            // Set user profile image path in database to filePath
            $user->profile()->profile_image = $filePath;
        }
        // Persist user record to database
        $user->save();

        // Return user back and show a flash message
        return redirect()->back()->with(['status' => 'Profile updated successfully.']);
    }

I get this error when I finally load the page

BadMethodCallException Method App\Http\Controllers\ProfilesController::uploadOne does not exist.

even though i refer to uploadOne() in UploadTrait.php, I need help to understand where i went wrong.

3
  • 1
    use UploadTrait; ? Commented Dec 16, 2019 at 23:06
  • 1
    Did you include use App\Traits\UploadTrait; in your controller? Commented Dec 17, 2019 at 0:29
  • yes it's working now but i'm getting a 419 page expired error Commented Dec 17, 2019 at 5:48

1 Answer 1

1

In your ProfileController Class make sure that you are using the UploadTrait

<?php

namespace App\Http\Controllers;

use App\Traits\UploadTrait; /// Add the Path to your UploadTrait.
use Illuminate\Http\Request;

class ProfileController extends Controller {

    use UploadTrait;  /// After the class is declared add this statement to use the trait

    ...

     public function updateProfile(Request $request)
    {
        ...
    }

} 
Sign up to request clarification or add additional context in comments.

2 Comments

yes the error stop after i added the path to the UploadTrait but it's showing me a 419 page expired error
That's actually a separate issue, the form is missing the csrf token, check the docs as it depends on if you are using blade templates, VueJs or another framework for your UI Laravel Docs - CSRF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.