D
D
Denwebart2015-08-04 19:38:15
MySQL
Denwebart, 2015-08-04 19:38:15

Selecting relationships with JOIN in Laravel 4.2?

I am using Laravel 4.2.
I'm fetching a user's article count with a JOIN like this:

User::join('pages as articles', 'articles.user_id', '=', 'users.id', 'left outer')->select([DB::raw('users.*, count(articles.id) as articlesCount')])->get()

As a result, only those users who have articles are selected.
How to display all users? Even those that do not have articles (need to display zero)?
Clarification:
There is a users table:
d635fd2513d94886b40f52885a95811a.JPG
And there is a pages table where articles and user questions are stored:
b6af05ca7fea4a6388ca103d3512ab26.JPG
What links are there:
public function publishedArticles()
  {
    return $this->hasMany('Page', 'user_id')->whereType(Page::TYPE_ARTICLE)
      ->whereIsPublished(1)
      ->where('published_at', '<', date('Y-m-d H:i:s'));
  }

public function publishedQuestions()
  {
    return $this->hasMany('Page', 'user_id')->whereType(Page::TYPE_QUESTION)
      ->whereIsPublished(1)
      ->where('published_at', '<', date('Y-m-d H:i:s'));
  }

It is necessary to make a choice of the number of articles and user questions depending on the given time interval. So that by default data is displayed for the entire period, and so that you can sort by the time when articles and questions were created: for the last month, for a specific month of a year, etc.
It would be desirable to make it a minimum of requests in a DB. In principle, how to implement it is more or less clear, but it turns out a lot of requests.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Elchev, 2015-08-06
@rsi

And why do you need to write a request at all? Use links between models, get a collection with posts from the link, it has a count method. It doesn't get easier.
Ok, now look, I'm giving you examples directly from the official tutorial

$user = App\User::find(1);
$user->posts()->where('active', 1)->get();

There is such an option, to unload all users, and then scrolling in a cycle to filter their articles by parameters and display the number.
There is a second option, the chapter is called Querying Relationship Existence
// Retrieve all posts that have at least one comment...
$posts = App\Post::has('comments')->get();

// Retrieve all posts that have three or more comments...
$posts = Post::has('comments', '>=', 3)->get();

// Retrieve all posts that have at least one comment with votes...
$posts = Post::has('comments.votes')->get();

// Retrieve all posts with at least one comment containing words like foo%
$posts = Post::whereHas('comments', function ($query) {
    $query->where('content', 'like', 'foo%');
})->get();

This will list all users whose records in the related table match your criteria. For those who did not display 0, respectively, and that's all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question