how to convert mysql to eloquent laravel











up vote
0
down vote

favorite












I am trying to convert one of my MySQL query to Laravel Query Builder. I tried some of the methods but seems like I am not able to achieve the same results. The query is mentioned below.



For reference I am using Laravel 5.3 and planning to upgrade it soon.



SELECT eac.id,eac.gender,eac.start_date,eac.end_date,eac.ad_image_path,eac.ad_link,eac.requestfrom,eac.traffic,eac.registertype,eacr.region_id,eac.active,eac.impression,eac.center_image_path,eac.bottom_image_path,eac.approved_by,er.name as country_name,eac.budget,sum(budget/ (DATEDIFF(end_date,start_date)) *1000) as daily_imp ,eaa.impression_count,eac.customer_id,eaa.created_at

FROM advert_customer eac

JOIN advert_customer_regions eacr ON eac.id = eacr.advert_customer_id

JOIN regions er ON er.id = eacr.region_id

LEFT JOIN advert_abstract eaa on eac.id = eaa.advert_customer_id

WHERE eac.requestfrom ='web' AND eac.registertype = 'paid' AND eac.active = 1 AND eac.approved_by = 1 AND eac.gender ='male' AND er.name = 'india' AND IF((SELECT CASE WHEN DATE(eaa.created_at) = DATE(CURDATE()) THEN eaa.created_at ELSE NULL END), eaa.created_at , NULL)

group by eac.id,eac.gender,eac.start_date,eac.end_date,eac.ad_image_path,eac.ad_link,eac.requestfrom,eac.traffic,eac.registertype,eacr.region_id,eac.active,eac.impression,eac.center_image_path,eac.bottom_image_path,eac.approved_by,er.name,eac.budget ,eaa.impression_count,eac.customer_id, eaa.created_at


I have tried this query builder method, but seems like there is something wrong about it. I am not sure where is the mistake any guidance will be helpful.



DB::table('advert_customer as eac')->select("eac.id","eac.gender","eac.start_date","eac.end_date","eac.ad_image_path","eac.ad_link","eac.requestfrom","eac.traffic","eac.position","eac.registertype","eacr.region_id","eac.active","eac.impression","eac.center_image_path","eac.bottom_image_path","eac.approved_by","er.name as country_name","eac.budget",DB::raw("sum(budget/ (DATEDIFF(end_date,start_date)) *1000) as daily_imp"),"eaa.impression_count","eac.customer_id", "eaa.created_at")
->JOIN('advert_customer_regions as eacr','eac.id','=','eacr.advert_customer_id')
->JOIN('regions as er','er.id','=','eacr.region_id')
->LEFTJOIN('advert_abstract as eaa','eac.id','=','eaa.advert_customer_id')

->where('er.name', $event->country == null ? "India" : $event->country)
->where('eac.requestfrom','web')
->where('eac.registertype','paid')
->where('eac.active',1)
->where('eac.approved_by',1)

->orwhere("IF((SELECT CASE WHEN DATE('eaa.created_at') = DATE(CURDATE()) THEN 'eaa.created_at' ELSE NULL END), 'eaa.created_at' , NULL)")



->where(function($q){
$q->whereDate('eac.start_date','<=',Carbon::today())->where('eac.end_date','>=',Carbon::now());
})
->where(function($q){
$q->where("eac.gender", "all");
})
->groupBy("eac.id","eac.gender","eac.start_date","eac.end_date","eac.ad_image_path","eac.ad_link","eac.requestfrom","eac.traffic","eac.position","eac.registertype","eacr.region_id","eac.active","eac.impression","eac.center_image_path","eac.bottom_image_path","eac.approved_by","eac.budget","eaa.impression_count","eac.customer_id","eaa.created_at")
->inRandomOrder()
->take(5)
->get();


Thank you (In advance)










share|improve this question
























  • Please explain what you mean with "seems like there is something wrong about it." For example, give us an example of what you expect it to return and what it currently returns.
    – Delena Malan
    Nov 13 at 13:13















up vote
0
down vote

favorite












I am trying to convert one of my MySQL query to Laravel Query Builder. I tried some of the methods but seems like I am not able to achieve the same results. The query is mentioned below.



For reference I am using Laravel 5.3 and planning to upgrade it soon.



SELECT eac.id,eac.gender,eac.start_date,eac.end_date,eac.ad_image_path,eac.ad_link,eac.requestfrom,eac.traffic,eac.registertype,eacr.region_id,eac.active,eac.impression,eac.center_image_path,eac.bottom_image_path,eac.approved_by,er.name as country_name,eac.budget,sum(budget/ (DATEDIFF(end_date,start_date)) *1000) as daily_imp ,eaa.impression_count,eac.customer_id,eaa.created_at

FROM advert_customer eac

JOIN advert_customer_regions eacr ON eac.id = eacr.advert_customer_id

JOIN regions er ON er.id = eacr.region_id

LEFT JOIN advert_abstract eaa on eac.id = eaa.advert_customer_id

WHERE eac.requestfrom ='web' AND eac.registertype = 'paid' AND eac.active = 1 AND eac.approved_by = 1 AND eac.gender ='male' AND er.name = 'india' AND IF((SELECT CASE WHEN DATE(eaa.created_at) = DATE(CURDATE()) THEN eaa.created_at ELSE NULL END), eaa.created_at , NULL)

group by eac.id,eac.gender,eac.start_date,eac.end_date,eac.ad_image_path,eac.ad_link,eac.requestfrom,eac.traffic,eac.registertype,eacr.region_id,eac.active,eac.impression,eac.center_image_path,eac.bottom_image_path,eac.approved_by,er.name,eac.budget ,eaa.impression_count,eac.customer_id, eaa.created_at


I have tried this query builder method, but seems like there is something wrong about it. I am not sure where is the mistake any guidance will be helpful.



DB::table('advert_customer as eac')->select("eac.id","eac.gender","eac.start_date","eac.end_date","eac.ad_image_path","eac.ad_link","eac.requestfrom","eac.traffic","eac.position","eac.registertype","eacr.region_id","eac.active","eac.impression","eac.center_image_path","eac.bottom_image_path","eac.approved_by","er.name as country_name","eac.budget",DB::raw("sum(budget/ (DATEDIFF(end_date,start_date)) *1000) as daily_imp"),"eaa.impression_count","eac.customer_id", "eaa.created_at")
->JOIN('advert_customer_regions as eacr','eac.id','=','eacr.advert_customer_id')
->JOIN('regions as er','er.id','=','eacr.region_id')
->LEFTJOIN('advert_abstract as eaa','eac.id','=','eaa.advert_customer_id')

->where('er.name', $event->country == null ? "India" : $event->country)
->where('eac.requestfrom','web')
->where('eac.registertype','paid')
->where('eac.active',1)
->where('eac.approved_by',1)

->orwhere("IF((SELECT CASE WHEN DATE('eaa.created_at') = DATE(CURDATE()) THEN 'eaa.created_at' ELSE NULL END), 'eaa.created_at' , NULL)")



->where(function($q){
$q->whereDate('eac.start_date','<=',Carbon::today())->where('eac.end_date','>=',Carbon::now());
})
->where(function($q){
$q->where("eac.gender", "all");
})
->groupBy("eac.id","eac.gender","eac.start_date","eac.end_date","eac.ad_image_path","eac.ad_link","eac.requestfrom","eac.traffic","eac.position","eac.registertype","eacr.region_id","eac.active","eac.impression","eac.center_image_path","eac.bottom_image_path","eac.approved_by","eac.budget","eaa.impression_count","eac.customer_id","eaa.created_at")
->inRandomOrder()
->take(5)
->get();


Thank you (In advance)










share|improve this question
























  • Please explain what you mean with "seems like there is something wrong about it." For example, give us an example of what you expect it to return and what it currently returns.
    – Delena Malan
    Nov 13 at 13:13













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to convert one of my MySQL query to Laravel Query Builder. I tried some of the methods but seems like I am not able to achieve the same results. The query is mentioned below.



For reference I am using Laravel 5.3 and planning to upgrade it soon.



SELECT eac.id,eac.gender,eac.start_date,eac.end_date,eac.ad_image_path,eac.ad_link,eac.requestfrom,eac.traffic,eac.registertype,eacr.region_id,eac.active,eac.impression,eac.center_image_path,eac.bottom_image_path,eac.approved_by,er.name as country_name,eac.budget,sum(budget/ (DATEDIFF(end_date,start_date)) *1000) as daily_imp ,eaa.impression_count,eac.customer_id,eaa.created_at

FROM advert_customer eac

JOIN advert_customer_regions eacr ON eac.id = eacr.advert_customer_id

JOIN regions er ON er.id = eacr.region_id

LEFT JOIN advert_abstract eaa on eac.id = eaa.advert_customer_id

WHERE eac.requestfrom ='web' AND eac.registertype = 'paid' AND eac.active = 1 AND eac.approved_by = 1 AND eac.gender ='male' AND er.name = 'india' AND IF((SELECT CASE WHEN DATE(eaa.created_at) = DATE(CURDATE()) THEN eaa.created_at ELSE NULL END), eaa.created_at , NULL)

group by eac.id,eac.gender,eac.start_date,eac.end_date,eac.ad_image_path,eac.ad_link,eac.requestfrom,eac.traffic,eac.registertype,eacr.region_id,eac.active,eac.impression,eac.center_image_path,eac.bottom_image_path,eac.approved_by,er.name,eac.budget ,eaa.impression_count,eac.customer_id, eaa.created_at


I have tried this query builder method, but seems like there is something wrong about it. I am not sure where is the mistake any guidance will be helpful.



DB::table('advert_customer as eac')->select("eac.id","eac.gender","eac.start_date","eac.end_date","eac.ad_image_path","eac.ad_link","eac.requestfrom","eac.traffic","eac.position","eac.registertype","eacr.region_id","eac.active","eac.impression","eac.center_image_path","eac.bottom_image_path","eac.approved_by","er.name as country_name","eac.budget",DB::raw("sum(budget/ (DATEDIFF(end_date,start_date)) *1000) as daily_imp"),"eaa.impression_count","eac.customer_id", "eaa.created_at")
->JOIN('advert_customer_regions as eacr','eac.id','=','eacr.advert_customer_id')
->JOIN('regions as er','er.id','=','eacr.region_id')
->LEFTJOIN('advert_abstract as eaa','eac.id','=','eaa.advert_customer_id')

->where('er.name', $event->country == null ? "India" : $event->country)
->where('eac.requestfrom','web')
->where('eac.registertype','paid')
->where('eac.active',1)
->where('eac.approved_by',1)

->orwhere("IF((SELECT CASE WHEN DATE('eaa.created_at') = DATE(CURDATE()) THEN 'eaa.created_at' ELSE NULL END), 'eaa.created_at' , NULL)")



->where(function($q){
$q->whereDate('eac.start_date','<=',Carbon::today())->where('eac.end_date','>=',Carbon::now());
})
->where(function($q){
$q->where("eac.gender", "all");
})
->groupBy("eac.id","eac.gender","eac.start_date","eac.end_date","eac.ad_image_path","eac.ad_link","eac.requestfrom","eac.traffic","eac.position","eac.registertype","eacr.region_id","eac.active","eac.impression","eac.center_image_path","eac.bottom_image_path","eac.approved_by","eac.budget","eaa.impression_count","eac.customer_id","eaa.created_at")
->inRandomOrder()
->take(5)
->get();


Thank you (In advance)










share|improve this question















I am trying to convert one of my MySQL query to Laravel Query Builder. I tried some of the methods but seems like I am not able to achieve the same results. The query is mentioned below.



For reference I am using Laravel 5.3 and planning to upgrade it soon.



SELECT eac.id,eac.gender,eac.start_date,eac.end_date,eac.ad_image_path,eac.ad_link,eac.requestfrom,eac.traffic,eac.registertype,eacr.region_id,eac.active,eac.impression,eac.center_image_path,eac.bottom_image_path,eac.approved_by,er.name as country_name,eac.budget,sum(budget/ (DATEDIFF(end_date,start_date)) *1000) as daily_imp ,eaa.impression_count,eac.customer_id,eaa.created_at

FROM advert_customer eac

JOIN advert_customer_regions eacr ON eac.id = eacr.advert_customer_id

JOIN regions er ON er.id = eacr.region_id

LEFT JOIN advert_abstract eaa on eac.id = eaa.advert_customer_id

WHERE eac.requestfrom ='web' AND eac.registertype = 'paid' AND eac.active = 1 AND eac.approved_by = 1 AND eac.gender ='male' AND er.name = 'india' AND IF((SELECT CASE WHEN DATE(eaa.created_at) = DATE(CURDATE()) THEN eaa.created_at ELSE NULL END), eaa.created_at , NULL)

group by eac.id,eac.gender,eac.start_date,eac.end_date,eac.ad_image_path,eac.ad_link,eac.requestfrom,eac.traffic,eac.registertype,eacr.region_id,eac.active,eac.impression,eac.center_image_path,eac.bottom_image_path,eac.approved_by,er.name,eac.budget ,eaa.impression_count,eac.customer_id, eaa.created_at


I have tried this query builder method, but seems like there is something wrong about it. I am not sure where is the mistake any guidance will be helpful.



DB::table('advert_customer as eac')->select("eac.id","eac.gender","eac.start_date","eac.end_date","eac.ad_image_path","eac.ad_link","eac.requestfrom","eac.traffic","eac.position","eac.registertype","eacr.region_id","eac.active","eac.impression","eac.center_image_path","eac.bottom_image_path","eac.approved_by","er.name as country_name","eac.budget",DB::raw("sum(budget/ (DATEDIFF(end_date,start_date)) *1000) as daily_imp"),"eaa.impression_count","eac.customer_id", "eaa.created_at")
->JOIN('advert_customer_regions as eacr','eac.id','=','eacr.advert_customer_id')
->JOIN('regions as er','er.id','=','eacr.region_id')
->LEFTJOIN('advert_abstract as eaa','eac.id','=','eaa.advert_customer_id')

->where('er.name', $event->country == null ? "India" : $event->country)
->where('eac.requestfrom','web')
->where('eac.registertype','paid')
->where('eac.active',1)
->where('eac.approved_by',1)

->orwhere("IF((SELECT CASE WHEN DATE('eaa.created_at') = DATE(CURDATE()) THEN 'eaa.created_at' ELSE NULL END), 'eaa.created_at' , NULL)")



->where(function($q){
$q->whereDate('eac.start_date','<=',Carbon::today())->where('eac.end_date','>=',Carbon::now());
})
->where(function($q){
$q->where("eac.gender", "all");
})
->groupBy("eac.id","eac.gender","eac.start_date","eac.end_date","eac.ad_image_path","eac.ad_link","eac.requestfrom","eac.traffic","eac.position","eac.registertype","eacr.region_id","eac.active","eac.impression","eac.center_image_path","eac.bottom_image_path","eac.approved_by","eac.budget","eaa.impression_count","eac.customer_id","eaa.created_at")
->inRandomOrder()
->take(5)
->get();


Thank you (In advance)







mysql laravel laravel-5 orm eloquent






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 9:48

























asked Nov 12 at 6:04









user3201500

682726




682726












  • Please explain what you mean with "seems like there is something wrong about it." For example, give us an example of what you expect it to return and what it currently returns.
    – Delena Malan
    Nov 13 at 13:13


















  • Please explain what you mean with "seems like there is something wrong about it." For example, give us an example of what you expect it to return and what it currently returns.
    – Delena Malan
    Nov 13 at 13:13
















Please explain what you mean with "seems like there is something wrong about it." For example, give us an example of what you expect it to return and what it currently returns.
– Delena Malan
Nov 13 at 13:13




Please explain what you mean with "seems like there is something wrong about it." For example, give us an example of what you expect it to return and what it currently returns.
– Delena Malan
Nov 13 at 13:13

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256637%2fhow-to-convert-mysql-to-eloquent-laravel%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256637%2fhow-to-convert-mysql-to-eloquent-laravel%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine