This is code here:
protected function credentials(Request $request)
{
$admin=admin::where('email',$request->email)->first();
if(count($admin))
{
if($admin->status==0){
return ['email'=>'inactive','password'=>'You are not an active person, Please contact to admin'];
}
else{
return ['email'=>$request->email,'password'=>$request->password,'status'=>1];
}
}
return $request->only($this->username(), 'password');
}
When i run the code this error become:
"count(): Parameter must be an array or an object that implements Countable"
It happens because of in PHP 7.2 NULL in count() return Warning. You can try to change
count($admin)
to
count((is_countable($admin)?$admin:[]))
Note that here, When you use thecount()
method, there should be countable element, like an array or object that implementArrayAccess
.
Admin::where('email',$request->email)->first();
But thefirst()
method give you single element, not a collection or array. Theget()
method returns you countable a collection with found elements
Instead of using count you can directly check variable itself is it defined or null
if($admin){
// do something here
}
or you can useis_null()
method
if(!is_null($admin)){
// do something here
}
You should check if it is null instead of count, because you ask for one result withfirst()
just this
if($admin)
will do it.
if you use return a collection using->get()
then you can check$admin->count()
.
$admin
variable is neither array nor object that implements countable. When you usefirst()
the result will be a model object if record is found else it will be null. For this condition you can use:
if (!empty($admin)) {
//
}
Just replaceif (count($admin))
withif (!empty($admin))
.
And when you useget()
method to get multiple records you can check by:
if ($admins->count() > 0) {
//
}
Well,
$admin=Admin::where('email',$request->email)->first();
//It will always return an **object**.
And make sure you included Admin model in your controller like as.
Use App\Admin;
at the same time check that you will have to mention which field of table needs to be fillable like in your model such as
protected $fillable = [
'first_name',
'last_name'
];
whatever data you will going to save in your database.
and then check object is null or not
I mean is.
if($admin && $admin!==null){
//do whatver you want to do.
}
You can cast the variable to an array:
count((array)$variable);
If it's null then it will become an empty array.
$admin = null;
var_dump(count($admin));
output: Warning: count(): Parameter must be an array or an object that implements Countable in ??� on line 12 // as of PHP 7.2
if condition should be like:
if(isset($admin) && count($admin))
Useisset($admin->id)
instead ofcount($admin)
Try this :
protected function credentials(Request $request)
{
$admin=admin::where('email',$request->email)->first();
if(isset($admin->id)))
{
if($admin->status==0){
return ['email'=>'inactive','password'=>'You are not an active person, Please contact to admin'];
}
else{
return ['email'=>$request->email,'password'=>$request->password,'status'=>1];
}
}
return $request->only($this->username(), 'password');
}
In my case count was 1 even when I got [] from api
so I had to put it in try catch
try{
$propertyId = arr[0].propertyId;
}catch(\Exception $e) {
return response()->json(['success' => 'false']);
}
add this your controler this code:
$user = User::where('email',$request->email)->first();
if ($user){
return redirect()->back()->with('errors','We cant find a user with that e-mail address.');
}else{
$user->password = bcrypt($request->new_password);
$user->update();
return redirect()->back()->with('success','Success');
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.