-3

I have two tables Parameters and Share_Details.I have my raw sql query here, I want to write this in pure eloquent way.Please help.

$shareDetails=DB::select
              ("SELECT s.id,
                       share_type,
                       para_int_1,
                       para_int_2,
                       price,
                       para_name 
             FROM share_details as s,parameters 
                  where para_type='share'
                    and para_id=share_type
                    and startDate=(select max(startdate) 
                                   from share_details 
                                   where share_type=s.share_type)
              group by share_type,
                       s.id,
                       para_int_1,
                       para_int_2,
                       price,
                       para_name");
Veer
  • 13
  • 3
  • What do you mean by `pure eloquent way`? – flyingfox Sep 10 '22 at 08:56
  • 1
    We're not a code writing service, you need to show some effort. I suggest you read the [Eloquent](https://laravel.com/docs/9.x/eloquent) and [Eloquent Relationships](https://laravel.com/docs/9.x/eloquent-relationships) documentation. There is also the [Laravel from Scratch](https://laracasts.com/series/laravel-8-from-scratch) series which has [a video on relationships](https://laracasts.com/episodes/2018). – Peppermintology Sep 10 '22 at 09:28

1 Answers1

0

Try like this:

DB::table('share_details')
->crossJoin('parameters'
->select('share_details.id', 'share_type', 'para_int_1', 'para_int_2', 'price', 'para_name')
->where('para_type','=','share')
->where('para_id','=',DB::raw('share_type'))
->where('startDate','=',function($query) {
    $query->from('share_details')
        ->select(DB::raw("'max'(startdate))
        ->where('share_type','=',DB::raw('s.share_type'));
})
->groupBy('share_type','share_details.id','para_int_1','para_int_2','price','para_name')
->get();
Domen
  • 224
  • 2
  • 15