mysql_num_rows in laravel?
Asked Answered
D

1

7

im trying to use mysql_num_rows in laravel but laravel says it not the same way like in 'raw php'

example:

$users = DB::table('users')
         ->where('username', '=', $username)
         ->where('password', '=', $password)
         ->get();

what i want to do:

$count = mysql_num_rows($users);

   if($count > 0 ){

      $user->login = $request->login;
      $user->email = $request->email;
      $user->password = $request->password;

      Auth::login($user);
      return redirect("/");
      }else{
         return "datos incorrectos";
      }

what laravel says:

Call to undefined function App\Http\Controllers\Auth\mysql_num_rows()

PD: its not philosophy of code just make commets about that question, i dont want answers like "u gonna crypt that thing?", "why not use [insert my faborite ORM]" is just a simple question THANKS

Duffy answered 6/9, 2016 at 20:16 Comment(2)
If this is the same project that you were working on a few days ago, you should add that, because you're interfacing with a legacy project with its own password hashing scheme, you can't simply use Laravel's built-in cryptography to hash passwords and handle authentication.Lockwood
is true Auth::login($user); dont works yet but i feel close to resolve how to do that legacy thing and learn how things works in the proccess and later learn how to upgrade that but step by stepDuffy
C
11

Instead of using mysql_* functions, you should use count() instead. It can be chained to Eloquent, query builder, or collections.

$users_count = DB::table('users')
     ->where('username', '=', $username)
     ->where('password', '=', $password)
     ->count();
Capitalization answered 6/9, 2016 at 20:23 Comment(7)
u mean like that: $users = DB::table('users') ->where('username', '=', $username) ->where('password', '=', $password) ->get()->count(); ERROR:Call to a member function count() on arrayDuffy
Note, @DevilSystem, that in cmnardi's answer, he's not chaining get().Lockwood
Pay attetion to your Laravel version too.. I think it don't change, but I'm not sureCapitalization
@Capitalization Fortunately, count() has been around since at least Laravel 4.2Lockwood
->count() only works on collections, you have not returned a collection but an instance of the query builder. Updated your answer to reflect that.Corcyra
@Ohgodwhy, I rolled your edit back; the query builder does accept count() and other aggregate methods (Source)Lockwood
@ChrisForrence Good call!Corcyra

© 2022 - 2024 — McMap. All rights reserved.