mysql_num_rows in laravel? - sql

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

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();

Related

Laravel eloquent where with relationship

I am stuck on transforming this SQL query to an eloquent query in Laravel. The SQL query works (tested in Sequel) but I cannot write it in eloquent ...
SELECT faqs.question FROM faqs
JOIN categories c ON c.id = faqs.category_id
WHERE c.subsite = 'sport'
This is what I have tried so far, but it returns all the questions, (ignoring the subsite filter).
$cat = Faq::with(['category' => function($query) use ($subsite) {
$query->where('subsite', $subsite);
}])->get();
Thanks for the help
Try this
$cat = Faq::query();
if (isset($subsite) && !empty($subsite) {
$query->whereHas('category', function ($query) use ($subsite) {
$query->where('subsite', $subsite);
});
}
$query->with('category')->get();
as per you core sql query, here is your laravel query.
$data = DB::table('faqs')
->join('categories', 'categories.id', '=', 'faqs.category_id')
->select('faqs.question')
->where('categories.subsite', '=', 'sport')
->get();
Hope this will help you!

Building where query based on input

matchthese=['name'=> Input::get('name'),];
Matchthose['place'=> Input::get('place')];
$Select_db = Db::table('actvities')
->where([[matchthese],[matchthose]])
->orwhere('place', Input::get('place'))
->orwhere('color', Input::get('color'))
->orwhere('people',Input::get('people'))
->get();
But the orwhere doesn't work correctly.I want the first one or the second one etc.How should i do this to work?
You can use skip and take with first:
For example for the first one you can use first directly:
$Select_db = Db::table('actvities')
->where([['name', '=', Input::get('name')],['place', '=', Input::get('place')]])
->orwhere('place', Input::get('place'))
->orwhere('color', Input::get('color'))
->orwhere('people',Input::get('people'))
->take(1)->first();
For the second :
$Select_db = Db::table('actvities')
->where([['name', '=', Input::get('name')],['place', '=', Input::get('place')]])
->orwhere('place', Input::get('place'))
->orwhere('color', Input::get('color'))
->orwhere('people',Input::get('people'))
->skip(1)->take(1)->first();
If you need to loop through the result you can simply use foreach:
$Select_db = Db::table('actvities')
->where([['name', '=', Input::get('name')],['place', '=', Input::get('place')]])
->orwhere('place', Input::get('place'))
->orwhere('color', Input::get('color'))
->orwhere('people',Input::get('people'))
->get();
foreach($Select_db as $record) {
//Do something with $record.
}
I hope this will help you.

Multi join select query on Laravel

How to write this query in Laravel. I am new in laravel.
Example:
SELECT * FROM ((respassanger join ((`reservation` join flightres
on flightres.res_id = reservation.id)) on respassanger.res_id = reservation.id)
join passanger on respassanger.pas_id = passanger.pas_id)
Have you read the documentation? Checkout Laravel's query builder. Using your table names as mentioned, work from the following example in the docs for your needs:
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();
So something like:
DB::table('respassanger')
->join('reservation', function($join)
{
$join->on('flightres.res_id', '=', 'reservation.id')
})
->join('passanger', 'respassanger.pas_id', '=', 'passanger.pas_id')
->select( ... )
->get();
Not sure how correct the above is, but tweak it if it doesn't work.

Select sql code in Laravel

Following query returning six values
SELECT tbl_start FROM timetable inner join route ON tbl_rte_id = id WHERE rte_origin = "UL" and rte_destination = "HW" ORDER BY(tbl_start) DESC;
And my laravel code is returning only one value
$tables = Timetable::join('route', 'tbl_rte_id', '=', 'id')
->where('rte_origin', $origin, 'AND')
->where('rte_destination', $destination)
->orderBy('tbl_start', 'desc')
->get();
foreach ($tables as $table) {
$result[$table->id] = $table->tbl_start;
}
This laravel code is not similar or similar. Can anyone help me.
Change this part:
->where('rte_origin', $origin, 'AND')
// to:
->where('rte_origin', $origin)
It will know by default that it's AND operator
And if you want to provide this operator, then do this:
->where('rte_origin', '=', $origin, 'AND')
You may try something like this:
$tables = Timetable::join('route', 'tbl_rte_id', '=', 'timetable.id')
->where('rte_origin', $origin)
->where('rte_destination', $destination)
->orderBy('tbl_start', 'desc')
->get()->lists('tbl_start', 'id');
The $tables will contain an array of id => tbl_start pairs.
Add a listener in your routes.php
Event::listen('illuminate.query', function($sql){
var_dump($sql);
});
Then execute both queries and check if you have the same result

Kohana framework with ORM create custome where

I am using Kohana framework 3, and I need to create a query
I need add one where part only if variable is true, otherwise I don't need one WHERE part.
How add php if script in query?
What I need:
ORM::factory('test')
->select( array('name', 'surname') )
->where('id', '=', $user->id)
if ($active == 1) {
->where('status', '=', 1)
}
->order_by('docid', 'DESC')->find_all();
$query = ORM::factory('test')
->select( array('name', 'surname') )
->where('id', '=', $user->id);
if ($active == 1) {
query->where('status', '=', 1);
}
$query->order_by('docid', 'DESC')->find_all();