Call to a member function pluck() on null
Call to a member function pluck() on null
In this session, we’ll try our hand at solving the "Call to a member function pluck() on null" puzzle by using the computer language.
We got the error call to a member function pluck() on null, when we called pluck method on null. So before calling pluck method check if the variable is empty or not after that call pluck method else it will return an error message call to a member function pluck() on null.-
Call pluck method on $user->followings to get followings in Overtrue\LaravelFollow
$user = Auth::user(); if($user->followings){ $followings = $user->followings->pluck('id'); }else{ $followings = array(); } $users = User::whereNotIn('id', $followings) ->where('id', '<>', $user->id) ->get();
In my case I have called the $user->followings->pluck(‘id’) to the whereNotIn() query and got the error because there are no followings for the logged in user in the table. So the $user->followings method returns the null value so that is why we got the error calling a member function pluck() on null.
After that i have checked following if it is null then we pass empty array to pluck method
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Recursive function example code PHP Laravel
- Get duplicate records in laravel
- Insert data with form validation using ajax in laravel
- Class 'App\Rules\Hash' not found in Laravel
- How to return a column with different name in Laravel
- Route prefix with auth middleware in laravel
- Laravel pagination links with query string
- The use statement with non-compound name 'Auth' has no effect
- Get ids in array from users table
- Check if Relationship Method Exists in Laravel
- Call to undefined method App\Models\User::follow()
- Insert current date time in a column using Laravel
- How to display order by null last in laravel
- How to disable timestamps in laravel
- Sample .htaccess file and index.php file under public directory in laravel
- How to check data inserted or deleted in pivot after toggle method
- Add a subselect based on relationship using withAggregate method
- Retain selected value of select box in Laravel
- Store logged in user details in session and display in view in laravel
- Save or update pivot table data with additional column in Laravel
- Display success message in laravel
- Method Illuminate\Events\Dispatcher::fire does not exist
- Laravel 7 login error message not showing
- How to check email is valid or not in Laravel
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package