简体   繁体   中英

Laravel mysql Inner join and also select a specific field from another table

I have the following query:

Ratings::join('users', 'movieratings.rated_by', '=', 'users.usr_id')
        ->where('rated_on', $movieId)
        ->orderBy('rated_at', 'desc')
        ->select('comment', 'rating', 'rated_as', 'rated_at', 'username')
        ->paginate(20);

This will get all the feedback ratings for a specific movie. But I have another table which contains the total good and bad ratings for a specific movie movie, the only problem is that I cant get it to work to query that table as well at the same time.

If I do another query I would simply write: Movie::where('movie_id', $movieId)->select('total_good_ratings', 'total_bad_ratings')->get(); this would output eg " 22, 15 " but is it possible to only fetch two columns from a specific row then do a inner join between two tables and paginate the result?

thanks

You can do a leftJoin with the table that contains the good and bad ratings, where the join condition will be the id of the movie.

Ratings::join('users', 'movieratings.rated_by', '=', 'users.usr_id')
        ->leftJoin('movie', 'movie.id', '=', 'movieratings.rated_on')
        ->where('rated_on', $movieId)
        ->orderBy('rated_at', 'desc')
        ->select('comment', 'rating', 'rated_as', 'rated_at', 'username', 'total_good_ratings', 'total_bad_ratings')
        ->paginate(20);

I think you can try this:

Ratings::leftJoin('users', 'users.usr_id', '=', 'movieratings.rated_by')
        ->leftJoin('movie', 'movie.id', '=', 'movieratings.rated_on')
        ->where('movieratings.rated_on', $movieId)
        ->orderBy('movie.rated_at', 'desc')
        ->select('movieratings.comment', 'movieratings.rating', 'movieratings.rated_as', 'movie.rated_at', 'users.username', 'movieratings.total_good_ratings', 'movieratings.total_bad_ratings')
        ->paginate(20);

Hope this help for you !!!

In case this may be of help:

Assuming:

class Rating extends Model {
      public users() {
          $this->belongsTo(User::class, 'usr_id');
      }
      public movie() {
          $this->belongsTo(Movie::class, 'rated_on'); //Name looks odd, it should be movie_id if you are following standard conventions
      }
}

Then you can lazy/eaher load them:

$ratings = Ratings::with([ "movie" => function ($query) {
        $q->select('total_good_ratings', 'total_bad_ratings');
     }])->where('rated_on', $movieId)
       ->orderBy('rated_at', 'desc')
       ->select('comment', 'rating', 'rated_as', 'rated_at', 'username',"rated_on")
       ->paginate(20);

You can get the movie info via $ratings[X]->movie->total_good_ratings (in a loop that would be $rating->movie->total_good_ratings

A bit of critique though:

  1. total_good_ratings looks like it's a derived attribute so it should not have been stored in the first place. It's appears to be a count of the good ratings.
  2. You should use the standard conventions when naming columns and tables eg a foreign key is usually called <foreign table name in singular>_<foreign field name> example user_id or movie_id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM