In my laravel application, I have an option to update user details, following is the update function in my controller
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email|unique:users,email,'.$id,
'password' => 'same:confirm-password',
'comp_id'=>'required',
'roles' => 'required'
]);
$input = $request->all();
if(!empty($input['password'])){
$input['password'] = Hash::make($input['password']);
}else{
$input = array_except($input,array('password'));
}
$user = User::find($id);
$user->update($input);
DB::table('model_has_roles')->where('model_id',$id)->delete();
$user->assignRole($request->input('roles'));
return redirect()->route('customers.index')
->with('success','Customer updated successfully');
}
But when I try to execute the above function am getting an error saying,
Call to undefined function App\Http\Controllers\array_except()
I used the exac same code in the other update functions as well, but they worked without an issue..
Solution 1:
use Illuminate\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100];
$filtered = Arr::except($array, ['price']);
Reference: https://laravel.com/docs/6.x/helpers#method-array-except
Solution 2:
Starting Laravel 6, most of the str_ & array_ helpers funcs are accessible ONLY by the new laravel/helpers composer package, if you don't have this package you'll get this sort of error.
Install helpers' package:
composer require laravel/helpers
and
composer dump-autoload
Include use Illuminate\Support\Arr; in your code and change:
$input = array_except($input,array('password'));
to
$input = Arr::except($input,array('password'));
array_except() is deprecated in versions greater than 5.8 for laravel