0

I want to make a filter with different parameters. One of parameter is $article_title. But it can be empty . My problem is even if its not empty i get null in return $comments. That is because this part of code:

 $q->where('language_id', $default_language_id)->where('title','like',$article_title);

This is my function

public function getResultCommentsWithArticle($comment,$user_firstname,$article_title,$orderBy){
      $default_language = Languages::where('default',1)->first();
      $default_language_id = $default_language->id;

      $comments = ArticleComments::orderBy($orderBy,'desc')
          ->with(['user', 'article', 'article.translations' => function($q) use($default_language_id,$article_title) {
              $q->where('language_id', $default_language_id)->where('title','like',$article_title);
          }])->paginate(10);
      return $comments;
    }

EDIT:

I also tried like this:

$comments = ArticleComments::orderBy($orderBy,'desc')
            ->with(['user', 'article', 'article.translations' => function($q) use($default_language_id,$article_title) {

                $q->where([
                    ['language_id', '=', $default_language_id],
                    ['title', 'like', '%'. $article_title .'%'],
                ]);
            }])->paginate(10);

But i get all comments and not comments with title="something"

19
  • 1
    Could you explain your exact question? Right now I could implement a number generator and it would be a valid answer :) Commented Jan 17, 2017 at 15:31
  • problem is that i always get null when i use ->where('title','like',$article_title); even if i have that title in database Commented Jan 17, 2017 at 15:32
  • is it clear at all right now? :D Commented Jan 17, 2017 at 15:37
  • Can you post the result of the dumped $comments var? If you have comments in your database this query shouldn't return empty array (no matter what you pass in $default_language). Commented Jan 17, 2017 at 15:41
  • Sorry, I misread your post! I seem to remember you couldn't simple paginate (the ->paginate(10)) the result of an orderBy query, but I can't find any support on that. Can you do a ->get() and see what that does? Commented Jan 17, 2017 at 15:41

2 Answers 2

2
$comments = ArticleComments::orderBy($orderBy,'desc')
            ->with(['user', 'article'])
            ->whereHas('article.translations', function($q) 
                use($default_language_id,$article_title) {

                $q->where('language_id', $default_language_id)
                ->where('title','like',$article_title);
            })->paginate(10);
Sign up to request clarification or add additional context in comments.

4 Comments

I have modified my answer
The problem is that ->whereHas takes 2 arguments. You need to remove the => in it and replace it with a comma. That's why Paras is getting an error.
Thanks @devk I will modify my answer!
Modified, please check now if it works the way you want @None
0

Try this:

$comments =  ArticleComments::orderBy($orderBy, 'desc')
    ->with(['user', 'article' => function ($q) use($default_language_id, $article_title) {
        $q->with('translations')
            ->whereHas('translations', function ($q) use($default_language_id, $article_title) {
                $q->where('language_id', $default_language_id)->where('title','like',$article_title);
            });
    }])->paginate();

This will select all comments and it will attach to it articles that match the translation.title and default language id.

In case you want to filter the comments instead of articles you would do this:

$comments =  ArticleComments::orderBy($orderBy, 'desc')
    ->with(['user', 'article.translation'])
    ->whereHas('article', function ($q) use($default_language_id, $article_title) {
        $q->whereHas('translations', function ($q) use($default_language_id, $article_title) {
                $q->where('language_id', $default_language_id)->where('title','like',$article_title);
            });
    })->paginate();

    return $comments;

4 Comments

i tried this but again i get all comments, for all articles, and not what i enter in $article_title
Try second one. I wrote that the first one gets all comments. The 2nd one filters the comments. I'm sorry but it really really isn't clear what you want, that's why I wrote both. Edit: also can you put php tag in your post so that the code gets properly highlighted (it's far more readable that way).
I think problem with your 2nd solution is that it loads users and translations, whereas OP wants users and articles but only wants the check on translations
Maybe yeah. I just assumed he needs the translations as well (since he's including them in ->with() in the question). If he does it's a really bad idea to lazy load them (n+1 and all that). But I guess he got it to work as intended :)

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.