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

Tuesday, February 1, 2022

[FIXED] How to select min and max in laravel

 February 01, 2022     eloquent, laravel, laravel-5     No comments   

Issue

I want to convert this code in laravel.

SELECT MAX(date_start) AS DateStart,MIN(date_end) AS DateEnd FROM DBTest

And I try this code

$data = DB::table('DBTest')
    ->select(max('date_start'), min('date_end')))
    ->get();

Return Error: max(): When only one parameter is given, it must be an array

I am using laravel 5.2, and SQLyog as database

I am confuse in syntax please help me


Solution

You can't use functions in select statement, but you can use raw SQL :

$data = DB::table('DBTest')
   ->select(\DB::raw('MIN(date_start) AS DateStart, MAX(date_end) AS DateEnd'));
   ->get();


Answered By - Vincent Decaux
  • 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