Devil System Devil System - 3 months ago 51
SQL Question

mysql_num_rows in laravel?

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

Answer

Don't use mysql_ functions.. you need ->count();

See Retrieving Aggregates here: https://laravel.com/docs/5.3/eloquent

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