0

I have a query like this:

    select * from `research_purchases` left join `company_research_articles`
 on `research_purchases`.`researchable_id` = `company_research_articles`.`id` 
and `research_purchases`.`researchable_type` = 'Modules\Analystsweb\Entities
\CompanyResearchArticle'

The research_purchases table structure is like this:research_purchases table

It is not filtering the "Modules\Analystsweb\Entities\CompanyResearchArticle" part and giving me the entire result. This is the part where I am stuck.

Originally I want to run a Laravel query. The query is like this:

$sales = DB::table('research_purchases')->select(DB::raw('count(research_purchases.id) as sales_count, research_purchases.created_at'))->leftJoin('company_research_articles', function ($join) {
                                        $join->on('research_purchases.researchable_id', '=', 'company_research_articles.id')
                                            ->where('research_purchases.researchable_type', '=', 'Modules\Analystsweb\Entities\CompanyResearchArticle');
                                    })
                                    ->where('research_purchases.created_at', '>', Carbon::now()->subMonths(11))
                                    ->groupBy(DB::raw("MONTH(research_purchases.created_at)"))
                                    ->get();

Any suggestion would be appreciated. Thank you.

4
  • if you give me query i can help you. but I have no idea about laravel Commented Apr 28, 2017 at 14:28
  • The query is at the top of the post please check. Commented Apr 28, 2017 at 14:32
  • I have already given you a reason Commented Apr 28, 2017 at 14:32
  • check the length of one of the records and make sure the length is the same as the limit (51) you likely have a non-display character causing the issue. (space, tab, end of line etc) Commented Apr 28, 2017 at 14:59

2 Answers 2

1

ohh dear you should put this in where condition

if you want left join all data on where condition of right table then you put it with join on

select * from `research_purchases` left join `company_research_articles`
 on `research_purchases`.`researchable_id` = `company_research_articles`.`id` 

where  `research_purchases`.`researchable_type` = 'Modules\Analystsweb\Entities
\CompanyResearchArticle'
Sign up to request clarification or add additional context in comments.

2 Comments

It gives zero results.
than check you where condition you have data completely like this
0

Please try whereColumn .

$sales = DB::table('research_purchases')->select(DB::raw('count(research_purchases.id) as sales_count, research_purchases.created_at'))->leftJoin('company_research_articles', function ($join) {
               $join->on('research_purchases.researchable_id', '=', 'company_research_articles.id');
        })->whereColumn([
                    ['research_purchases.researchable_type', '=', 'Modules\Analystsweb\Entities\CompanyResearchArticle'],
                    ['research_purchases.created_at', '>', Carbon::now()->subMonths(11)]
        ])->groupBy(DB::raw("MONTH(research_purchases.created_at)"))
                             ->get();

or both the where in side the closer

$sales = DB::table('research_purchases')->select(DB::raw('count(research_purchases.id) as sales_count, research_purchases.created_at'))->leftJoin('company_research_articles', function ($join) {
                                        $join->on('research_purchases.researchable_id', '=', 'company_research_articles.id')
                                        ->where(
                    'research_purchases.researchable_type', '=', 'Modules\Analystsweb\Entities\CompanyResearchArticle')
                             ->where('research_purchases.created_at', '>', Carbon::now()->subMonths(11))
                )
})->groupBy(DB::raw("MONTH(research_purchases.created_at)"))
                                    ->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.