Not getting all the table data while executing the laravel query - laravel

Link for table
link for expected result, after group by 'receiver_user_id' and recent time
I have used the laravel query:-
$sub = BaseMessagesHistory::select('messages_history.*')->orderBy('created_at','DESC');
$chats = DB::table(DB::raw("({$sub->toSql()}) as sub"))
->select('receiver_user_id',DB::raw('max(created_at) as recent_time'))
->where('sender_user_id',$userId)
->orwhere('receiver_user_id',$userId)
->groupBy('receiver_user_id')
->havingRaw('max(created_at)')
->latest()->get();
Result:-
I am getting only "recent_time" and "receiver_user_id"
Expectation:- I need whole data from table not only "recent_time" and "receiver_user_id"
So can you please help me out

It will return you only those columns which you mentioned in the select().
In your query, you mentioned only receriver_user_id and recent_time.
->select('receiver_user_id',DB::raw('max(created_at) as recent_time'))
You need to add all those columns in select() which you need.
Or try this ->select('sub.*',DB::raw('max(created_at) as recent_time'))
Hope this helps. Ask in case of doubt.

Related

Laravel Eloquent - How do I select columns of the updated row?

I'm trying to update a single row,
using this query,
$update_row = PayRecord::where('date_from',$date_from)
->where->('date_to',$date_to)
->update(['status',1]);
what I want to do is to get the id of the updated row, without creating another query to get the id,
$get_updated_row_id = PayRecord::where('date_from',$date_from)
->where->('date_to',$date_to)'
->get('id');
Thank You,
Remove extra -> from where clause.
This may useful to you.
$update_row = PayRecord::where('date_from',$date_from)
->where('date_to',$date_to)
->first();
$update_row->update(['status',1]);
$update_row->id; // to get the id

How to handle new incoming entries while paging through posts?

Let's assume I have a pagination like this
return App\Post::paginate(1);
After loading this someone creates a new entry in the database for posts. How can i ensure that the "second page" of my pagination does not return the first one again?
Will I always need to send a timestamp with every request to make an additional query before pagination is used?
like
App\Post::where('created_at', '<', $request->input('max_created_at'))->paginate(1);
You can use orderBy. You could do something like that:
// Get the last post (most recent)
return App\Post::orderBy('created_at', 'DESC')->paginate(1);
Or:
// Same as above
return App\Post::orderBy('created_at', 'DESC')->first();
orderBy means all you result are sorted in your query. Hope it will help.
You don't need to pass any timestamp value to verify pagination.
In the controller, in view file you need to pass result of this query.
$result = App\Post::orderBy('created_at', 'desc')->paginate(1);
return view('pagination',compact('result'));
In view file you need below line for pagination work automatically as per Laravel syntax.
{{ $result->links() }}
Visit https://laravel.com/docs/5.2/pagination/ how laravel pagination work.

Select one column with where clause Eloquent

Im using Eloquent. But I'm having trouble understanding Eloquent syntax. I have been searching, and trying this cheat sheet: http://cheats.jesse-obrien.ca, but no luck.
How do i perform this SQL query?
SELECT user_id FROM notes WHERE note_id = 1
Thanks!
If you want a single record then use
Note::where('note_id','1')->first(['user_id']);
and for more than one record use
Note::where('note_id','1')->get(['user_id']);
If 'note_id' is the primary key on your model, you can simply use:
Note::find(1)->user_id
Otherwise, you can use any number of syntaxes:
Note::where('note_id', 1)->first()->user_id;
Note::select('user_id')->where('note_id', 1)->first();
Note::whereNoteId(1)->first();
// or get() will give you multiple results if there are multiple
Also note, in any of these examples, you can also just assign the entire object to a variable and just grab the user_id attribute when needed later.
$note = Note::find(1);
// $user_id = $note->user_id;

Where Is My Raw Query?

Here is my controller code;
$temp_table_data = $temp_table
->setTempTable($generated_temp_table)
->newQuery()
->with(['payment' => function ($query) use ($column_values) {
$query->select($column_values);
}])->get();
My toSql query is right below it;
$sql = str_replace(['%', '?'], ['%%', "'%s'"], $temp_table->toSql());
$fullSql = vsprintf($sql, $temp_table->getBindings());
print_r($fullSql);
My code prints out;
select * from `selected_postcodes_1434968225_1`
Where are the details of the payments information that I am "with"ing? If I want to print out the raw query now, to show another developer, to get some help, what am I supposed to do here?
The eager loaded relationships are fetched in a separate query. You can use DB::getQueryLog() to get all run queries. Note that you have to enable it first using with DB::enableQueryLog().
Another alternative is the Laravel Debugbar package that shows you all queries and much more.

Short table name for SQL Query with Codeigniter database library

I'm using CodeIgniter, I want to using short table name for my SQL Query like that:
select pr.name from product pr where pr.id=12;
by using db class, I supposed to do:
$this->db->select('pr.name')->from('product pr')->where('pr.id', 12)->get();
This works perfect on CI 2.1.3. Don't forget to use result().
Example works for me:
function test(){
$this->load->database();
$sql = $this->db->select('pr.order_id')->from('items_table pr')->where('pr.order_id', 2)->get();
foreach($sql->result() as $item){
echo $item->order_id;
}
}
You can do it in this form:
$this->db->select('pr.name');
$this->db->from('product as pr');
$this->db->where('pr.id', 4);
return $this->db->get()->row_array();
with row_array() you will get one row, with result_array() you will get result of array.
There are so many solutions.. I prefer HMVC, but for demo purpose to explain "how it works", solutions inside controller (terrible,sucks!!!!)
Answer for using shorts table as alias, pls read rtfm or use simple query and generating results
For join methods,you can use too. Happy coding

Resources