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
- Trying to get property 'title' of non-object
- Pagination in laravel
- How to set column as primary key in Laravel model
- How to start websocket server in laravel
- Laravel clone model
- Send post data from controller to view
- Ajax POST request in laravel
- Convert input array to comma-separated string in laravel controller
- How to randomly get the user id from users table in laravel
- Laravel order by date not working
- Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
- How to get IP address in laravel
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Create records using relationship in laravel
- How to get query string value in laravel
- Best Practices for Error Handling in Production Server Code (example code)
- How to pass external link in laravel blade to anchor tag
- How to send email in laravel
- Remove several global scope from query
- Laravel migration add foreign key to existing table
- Add a subselect based on relationship using withAggregate method
- How to create projects method with belongstomany relationship in user model
- How to get count of all records created at yesterday
- Save or update pivot table data with additional column in Laravel
- If no route matched route::fallback in laravel