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

Wednesday, January 26, 2022

[FIXED] Creating a subquery using laravel eloquent

 January 26, 2022     eloquent, laravel, laravel-5, mysql, php     No comments   

Issue

My raw query is working fine but after converting it to Laravel eloquent it's not working.

    SELECT
        key_vals.`key`,
        key_vals.`value`
    FROM
        key_vals
    WHERE TIMESTAMPDIFF(MINUTE,
            key_vals.`last_store_time`,
            now()) < (SELECT ttl from ttls) 

Solution

You can do the sub query and pass the result to the outer query:

$ttl = Ttl::select('ttl')->first();
$results = KeyValue::select('key', 'value')
                   ->where(\DB::raw('TIMESTAMPDIFF(MINUTE, key_vals.`last_store_time`, now())'), '<', $ttl->ttl);

The inner query Ttl::select('ttl') should return a single cell value though, I just tried to do it as you mentioned in your example



Answered By - MEDZ
  • 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