Building where query based on input - sql

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.

Related

How to search by Month from a date in Laravel

I'm trying like this:
$cost=DB::table('breakfast_orders')
->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date')
->where('breakfast_orders.user_id',1)
->WHERE(' MONTHNAME(date)='June'')
->sum('breakfast_costs.total_cost');
or the transformation of the following sql might help me:
SELECT * FROM `breakfast_orders` WHERE MONTHNAME(date)='June'
There are date helpers available in the query builder :
$q->whereDay('created_at', '=', date('d'));
$q->whereMonth('created_at', '=', date('m'));
$q->whereYear('created_at', '=', date('Y'));
So in your case :
$cost = DB::table('breakfast_orders')->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date')
->where('breakfast_orders.user_id',1)
->whereMonth('created_at', '=', $month)
->sum('breakfast_costs.total_cost');
$month = 'June';
$cost=DB::table('breakfast_orders')
->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date')
->where('breakfast_orders.user_id',1)
->whereRaw('MONTH(date) = '.$month)
->sum('breakfast_costs.total_cost');

Error in Laravel join

I am trying to get results from 2 table using join, where i want result between time range from those 2 tables,
here is my code
$final_trade = DB::table('finaltrade')
->join('exchanges', function($join)
{
$join->on('exchanges.id', '=', 'finaltrade.exchange_id')
->where('finaltrade.user_id', '=', Auth::user()->id)
->whereTime('finaltrade.created_at', '=', 'exchanges.start_time');
})
->get();
when i add hardcoded time value instead of 'exchanges.start_time' it gives correct result but i want to add values from column,
You have to tell Laravel that it isn't a value, but a column name:
->whereTime('finaltrade.created_at', '=', DB::raw('exchanges.start_time'));
The wrong code is ->whereTime('finaltrade.created_at', '=', 'exchanges.start_time'); change it to ->whereColumn('finaltrade.created_at', '=', 'exchanges.start_time');
And corrected code is
DB::table('finaltrade')
->join('exchanges', function($join) {
$join->on('exchanges.id', '=', 'finaltrade.exchange_id')
->where('finaltrade.user_id', '=', Auth::user()->id)
->whereColumn('finaltrade.created_at', '=', 'exchanges.start_time');
})->get();

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

Eloquent advanced where within an advanced join

I'm trying to do the following join:
Schedules::select()
->leftJoin('histories', function ($j)
{
$j->on('histories.schedule_id', '=', 'schedules.id')
->where('histories.test', '=', DB::raw('schedules.test'))
->where(function ($q)
{
$q->where('histories.order_id', '=', 'schedules.order_id')
->orWhere('histories.customer_id', '=', DB::raw('schedules.customer_id'));
});
});
When I do, Laravel reports the error:
Missing argument 2 for Illuminate\Database\Query\JoinClause::where()
How do I structure this query in Eloquent syntax so that I get the equivalent of the following for the join statement?
left join histories
on histories.schedule_id = schedules.id
and histories.test = schedules.test
and (
histories.order_id = schedules.order_id
or histories.customer_id = schedules.customer_id
)
Unfortunately JoinClauses where doesn't work like ordinary where clause, and you can't pass closure for nested wheres, so you just need to repeat part of your conditions.
Also, you don't need where and DB::raw for your additional join clauses, instead use on once again.
So basically you need this:
$j->on('histories.schedule_id', '=', 'schedules.id')
->on('histories.test', '=', 'schedules.test')
->on('histories.order_id', '=', 'schedules.order_id')
->orOn('histories.schedule_id', '=', 'schedules.id')
->on('histories.test', '=', 'schedules.test')
->on('histories.customer_id', '=', 'schedules.customer_id');

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