Which line is the exception pointing to? That's the most important information.
Nov 9, 2020
14
Level 5
Trying to access array offset on value of type bool | Upload file/image
Hello,
So I'm following this tutorial on uploading files and still trying to figure most out.
I think my function has all the necessary steps to work, but unexpectedly I'm being surprised by the following error :
ErrorException
Trying to access array offset on value of type bool
For a, to me unknown, reason the error is thrown because my function is searching for a boolean?
Is this because I wanted to validate all form input at once?
= the way the function is structured?
Or is it an error in my script?
Below the update function in my controller :
public function update(Request $request){
//Fetch URL inputs and convert to valid url
//Facebook
$facebook = $request->input('facebook');
if(!preg_match("~^(?:f|ht)tps?://~i", $facebook)) {
$facebook= "https://" . $facebook;
}
$request->merge(['facebook' => $facebook]);
//Twitter
$twitter = $request->input('twitter');
if(!preg_match("~^(?:f|ht)tps?://~i", $twitter)) {
$twitter= "https://" . $twitter;
}
$request->merge(['twitter' => $twitter]);
//Instagram
$instagram = $request->input('instagram');
if(!preg_match("~^(?:f|ht)tps?://~i", $instagram)) {
$instagram= "https://" . $instagram;
}
$request->merge(['instagram' => $instagram]);
//Linkedin
$linkedin = $request->input('linkedin');
if(!preg_match("~^(?:f|ht)tps?://~i", $linkedin)) {
$linkedin= "https://" . $linkedin;
}
$request->merge(['linkedin' => $linkedin]);
//Skype
$skype = $request->input('skype');
if(!preg_match("~^(?:f|ht)tps?://~i", $skype)) {
$skype= "https://" . $skype;
}
$request->merge(['skype' => $skype]);
//Youtube
$youtube = $request->input('youtube');
if(!preg_match("~^(?:f|ht)tps?://~i", $youtube)) {
$youtube= "https://" . $youtube;
}
$request->merge(['youtube' => $youtube]);
//Handle avatar
//Check if request has image
if($request->hasFile('avatar')){
//Check if upload is valid (avoid data corruption)
$request->file('avatar')->isValid(){(
//Validate input
request()->validate([
'firstname' => ['required', 'string', 'min:2', 'max:50'],
'lastname' => ['required', 'string', 'min:2', 'max:80'],
'bio' => ['required', 'max:255'],
'function' => ['required'],
'mobile' => ['required', 'string', 'alpha_num', 'min:9', 'max:20'],
'avatar' => ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'], //max 2MB
'facebook' => ['required', 'active_url'],
'twitter' => ['required', 'active_url'],
'instagram' => ['required', 'active_url'],
'linkedin' => ['required', 'active_url'],
'skype' => ['required', 'active_url'],
'youtube' => ['required', 'active_url'],
])
)};
//Fetch file extension
$extension = $request->avatar->extension();
$request->avatar->storeAs('/images.avatars', request('avatar') . "." . $extension);
//Generate URL to store in DB
$url = Storage::url(request('avatar') . "." . $extension);
}
//Verify user
$user = Auth::user();
//update fields
$user->firstname = request('firstname');
$user->lastname = request('lastname');
$user->avatar = request('avatar');
$user->bio = request('bio');
$user->function = request('function');
$user->mobile = request('mobile');
$user->facebook = request('facebook');
$user->twitter = request('twitter');
$user->instagram = request('instagram');
$user->linkedin = request('linkedin');
$user->skype = request('skype');
$user->youtube = request('youtube');
$user->save();
return redirect(route('useraccount.show'));
}
}
Level 56
Change this line
$request->file('avatar')->isValid(){(
to
if ($request->file('avatar')->isValid()) {(
Also you can remove the parenthesis within the block.
Lastly I would move the validation sooner in the script.
Is the avatar file always required to be sent by the user?
1 like
Please or to participate in this conversation.