PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 19, 2022

[FIXED] Laravel - DB:raw

 January 19, 2022     eloquent, laravel, laravel-5     No comments   

Issue

I need to set this query in Laravel and I believe that DB:Raw is the best alterntive, how can I set this query in Eloquent or as DB:raw?

SELECT      PROP_TYPE,
            concat(round(avg( prop_exclusive = 'Exclusiva' ) * 100, 0),'%') as Exclusiva,
            concat(round(avg( prop_exclusive = 'No Exclusiva' ) * 100, 0),'%') as No_Exclusiva
FROM        PROPERTIES
WHERE       PROP_MARKET_CENTER = 'KW' AND
            PROP_EXCLUSIVE IS NOT NULL
GROUP BY    PROP_TYPE
ORDER BY    PROP_TYPE ASC

Regards!


Solution

I presume yo have a Property.php model similar to this.

class Property extends Model
{
    $table = 'PROPERTIES';
}

From here just use DB::raw() for the selects and the rest is standard usage of the Eloquent QueryBuilder.

$properties = Property::select(
        'PROP_TYPE',
        DB::raw("concat(round(avg( prop_exclusive = 'Exclusiva' ) * 100, 0),'%') as Exclusiva"),
        DB::raw("concat(round(avg( prop_exclusive = 'No Exclusiva' ) * 100, 0),'%') as No_Exclusiva")
    )
    ->where('PROP_MARKET_CENTER', 'KW')
    ->whereNotNull('PROP_EXCLUSIVE')
    ->groupBy('PROP_TYPE')
    ->orderBy('PROP_TYPE', 'asc');


Answered By - mrhn
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing