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.