1

I am trying to get only one row which is the latest if there is two or more the same jobseeker_id.in my table,

id   jobseeker_id type_of_call    created_at            updated_at
1      0002       incoming call   2017-08-06 16:12:54   2017-08-06 16:12:54
2      0002       outgoing call   2017-09-10 12:12:54   2017-09-10 12:12:54
3      0002       incoming call   2017-10-02 16:12:54   2017-10-02 16:12:54
4      0003       incoming call   2017-08-06 16:12:54   2017-08-06 16:12:54
so in my case, i want to return only two jobseekers which is the id 3 and 4. I trying to get like
 $jobseekers = DB::table('calllogs')->select('jobseeker_id')->distinct()->get();
var_dump($jobseekers);

but it is returning as only the jobseeker_id column, not including the other columns like type_of_call and created_at and updated_at.And also i want to pull out only the latest rows also.

Any help would be highly appreciated.

Soul Coder
  • 774
  • 3
  • 16
  • 40
  • The 1st duplicate topic describes the logic in SQL, while the 2nd one demonstrates how to do the same in laravel. – Shadow Dec 10 '17 at 15:07
  • 1
    @Shadow if OP wants to get jobseekers with the latest incoming call, the link you gave him has an awful solution for that (the Laravel one). I mean QB queries with two `DB::Raw`. And no one can even give OP any good solution because the question was marked as duplicate. – Alexey Mezenin Dec 10 '17 at 15:34
  • @AlexeyMezenin can u help me out bro, i am still trying to figure out. – Soul Coder Dec 10 '17 at 15:40
  • @Than Htut Oo if you want to get job seekers with the latest incoming call, the best way to do that is to use Eloquent. Create an additional relationship in the `Jobseeker` model `public function latestIncomingCall() { return $this->hasOne(IncomingCall::class, 'jobseeker_id')->latest(); }` and then load jobseekers with the latest call with `Jobseeker::with('latestIncomingCall')->get();`. You'll get a Laravel collection of job seekers with only one incoming call loaded. This is an example, so please use real model names. – Alexey Mezenin Dec 10 '17 at 15:44
  • @AlexeyMezeninal whether you prefer raw queries or a pure eloquent solution is a question of taste. Raw queries often outperform whatever SQL eloquent cobbles together. – Shadow Dec 10 '17 at 15:47
  • @Shadow [I disagree](https://github.com/alexeymezenin/laravel-best-practices#prefer-to-use-eloquent-over-using-query-builder-and-raw-sql-queries-prefer-collections-over-arrays). Any maintainable app is built on top of ORM, not mix of Query Builder queries with `DB::raw` inserts. – Alexey Mezenin Dec 10 '17 at 15:50
  • @AlexeyMezenin well, we have to agree that we disagree. – Shadow Dec 10 '17 at 16:10
  • @AlexeyMezenin thanks for the explaining, the above solution doesn't work yet for me. because i doesn't have the incomingcall class, actually , i have jobseeker table , call log table , right now i am getting all the call logs records , so there are many calllogs for each jobseeker, so i only want to get one record for each jobseeker. jobseeker has many calllogs relationship. Please help me again if you dun mind. Thanks Alexey – Soul Coder Dec 10 '17 at 16:19
  • @ThanHtutOo as I said, it's just an example and you should change `IncomingCall` to a real model name. Also, you get all records because you're using `hasMany` and not an additional `HasOne` as I've shown in my previous comment. – Alexey Mezenin Dec 10 '17 at 16:26
  • @ThanHtutOo sure, do not even attempt to follow any of the ways described in the duplicate questions... – Shadow Dec 10 '17 at 16:47

0 Answers0