Kohana 3.2 ORM - a sample of the unique values - orm

Here is my query:
$comment = ORM::factory('comment')
->where('status', '=', 1)
->find_all();
field = user_id
How do I chose to only one of each comments in user_id?
The following:
$comment = ORM::factory('comment')
->distinct('user_id')
->where('status', '=', 1)
->find_all();
Displays everything in the database!

You code should be more like this..
$comment = ORM::factory('comment')
->where('status', '=', 1)
->group_by('user_id')
->find_all();
Check the documentation for more info.

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

Laravel 5 join table

Is any possible to join table users only on column where my user id != user id from db.
$games = Games::where('status', '=', 2)->where('owner_user_id', Auth::user()->id)->orWhere('join_user_id', Auth::user()->id)->orderBy('games.updated_at', 'desc')->paginate(10);
Question is:
How join user table, where owner_user_id and join_user_id != 30.
So I only want join user table for 1304 and 49. Its only example.
Games::where('status', '=', 2)
->join('users', function($join){
$join->on('users.id', '=', 'games.owner_user_id')
->where('user.id', '!=', Auth::user()->id);
})
->orderBy('games.updated_at', 'desc')
->paginate(10);
Something like this?
You need to use a use function to pass data to join:
$id = 1; // whatever id of other user
Games::where('status', '=', 2)
->join('users', function($join)use($id){ // Here you need to pass user Id
$join->on('users.id', '=', 'games.owner_user_id')
->where('user.id', '!=', $id);
})
->orderBy('games.updated_at', 'desc')
->paginate(10);

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