I have model Post:
protected $guarded = [];
public function reviews() {
return $this->hasMany(Review::class);
}
My reviews table have a column type with values: 1(good),2(comment),3(negative).
I need get count reviews for every type. I need globally get these counts. I know that I can do in model something like this:
protected $withCount = ['reviews'];
But this get me all reviews. But I need get count only for every type.
You could use the withCount method and do sub queries inside:
$counts = Post::withCount([
'reviews',
'reviews as good_reviews' => function ($query) {
$query->where('type', 1);
}],
'reviews as bad_reviews' => function ($query) {
$query->where('type', 3);
}],
}])->get();
You can access the count like this:
echo $counts[0]->good_reviews;
For more info: Docs
you can use GroupBy method for it.
Something like this:
DB::table('reviews')
->select('type', DB::raw('count(*) as total'))
->groupBy('type')
->get();
Related
I have created many-to-many relation using belongsToMany function:
class Doctor extends Model
{
...
public function categories()
{
return $this->belongsToMany('App\Category', 'doctors_to_categories', 'doctor_id', 'category_id');
}
...
}
Now I want to create query with many-to-many condition. In SQL in would be:
SELECT *
FROM `doctors`
JOIN `doctors_to_categories`
ON `doctors_to_categories`.`doctor_id` = `doctors`.`id`
WHERE `doctors_to_categories`.`category_id` = 1
I have tried to achieve this like:
$doctors = Doctor::with(['categories' => function($query) {
$query->where('category_id', '=', 1);
}])->get();
Or
$doctors = Doctor::with(['categories' => function($query) {
$query->where('categories.id', '=', 1);
}])->get();
But it is not working. Any ideas how it should be? Thanks for any help.
The with() function does not actually introduce a join in your query, it just loads the relation of all models as a second query. So the with() function couldn't possibly change the original result set.
What you are looking for is whereHas(). This will add a WHERE EXISTS clause to the existing query.
$doctors = Doctor::with('categories')->whereHas('categories', function ($query) {
$query->where('categories.id', 1);
})->get();
Using ->with() doesn't actually limit the results of the Doctor::...->get() query; it simply tells Laravel what to return in the relationships attribute. If you actually want to enforce returning only Doctors that have a category 1 relationship, you need to use whereHas():
$doctors = Doctor::whereHas('categories', function($query) {
$query->where('categories.id', '=', 1);
// `id` or `categories.id` should work, but `categories.id` is less ambigious
})->get();
You can add whereHas condition for this. Try code below:
$doctors = Doctor::with('categories')->whereHas('categories', function($query) {
$query->where('id', 1);
})->get();
Is it possible to merge an eloquent collection with a query builder?
In this example, I've got 3 tables. An images table, a tag table, and an images_tag table. I also have eloquent relationships in both the Images and Tag models.
Tag.php:
class Tag extends Model {
protected $table = 'tags';
public $timestamps = false;
protected $fillable = [
'tagname',
];
public function Images() {
return $this->belongsToMany('CommendMe\Models\Images');
}
}
Images.php:
public function tags() {
return $this->belongsToMany('CommendMe\Models\Tag');
}
What I'd like to do is something like this:
$tag = Tag::where('tagname', $query)->first();
$imagesWithTag = $tag->images;
$imagesWithoutTag = Images::where('title', 'LIKE', "%{$query}%");
$images = $imagesWithTag + $imagesWithoutTag
->whereIn('mature', [0, $mature])
->where('created_at', '>=', Carbon::now()->subHours($withinHours))
->orderBy($orderByString, 'desc')
->Paginate(50);
Essentially just taking the query (which returns an array from the images table), and the collection (which also returns an array from the images table), combine and sort the results.
Is this possible?
EDIT 2:
When trying Paras's new answer, the following error occurs:
FatalThrowableError in SearchController.php line 98: Call to a member
function toArray() on integer
Try this:
$tag = Tag::where('tagname', $query)->first();
$imageIds = $tag->images()->select('images.id')->get()->pluck('id')->toArray(); // assuming your images table name is "images" and primary key name is "id"
$images = Images::where(function($q) use($imageIds, $query) {
$q->whereIn('id', $imageIds)
->orWhere('title', 'LIKE', "%{$query}%");
})->whereIn('mature', [0, $mature])
->where('created_at', '>=', Carbon::now()->subHours($withinHours))
->orderBy($orderByString, 'desc')->paginate(50);
I have 2 models: TheSeries and TheEpisodes.
TheSeries has many TheEpisodes and TheEpisodes has one TheSeries.
I am trying to list all TheSeries and display latestEpisode in each, by using TheEpisodes.addDate.
The code I have right now is this:
$TheSeries = TheSeries::with('TheEpisodes');
What should I do to display only latest 1 episode for each TV serial?
EDIT
->take(1) and ->limit(1) do not work for TheEpisodes
EDIT (Latest Semi-Working Code)
$results = TheSeries::take(5)->with(['TheEpisodes' => function($q) {
$q->orderBy('addDate', 'desc');
}])->get()
This works, it returns the episodes in correct order but I am unable to limit the results to 1. This following codes don't work:
// Example 1
$results = TheSeries::take(5)->with(['TheEpisodes' => function($q) {
$q->orderBy('addDate', 'desc')->take(1);
}])->get()
// Example 2
$results = TheSeries::take(5)->with(['TheEpisodes' => function($q) {
$q->orderBy('addDate', 'desc')->limit(1);
}])->get()
// Example 3
$results = TheSeries::take(5)->with(['TheEpisodes' => function($q) {
$q->orderBy('addDate', 'desc')->first();
}])->get()
Those are the column names of the tables:
TheSeries - id, originalTitle, aliasTitle, description, imageURL, startingDate, endingDate, activeBool
TheEpisodes: id, seriesID, authorID, addDate, episodeVersion
Define a TheLatestEpisode hasOne relation on your TheSeries model:
class TheSeries extends Model
{
public function TheLatestEpisode()
{
return $this->hasOne(TheEpisode::class, 'seriesID')->orderBy('id', 'desc');
}
}
Then you can easily do:
$series = TheSeries::with('TheLatestEpisode')->get();
You can try it as:
$TheSeries = TheSeries::with(['TheEpisodes' => function($q) {
$q->orderBy('created_at', 'desc')->take(1);
}])
->get();
Or try with limit as:
$TheSeries = TheSeries::with(['TheEpisodes' => function($q) {
$q->orderBy('created_at', 'desc')->limit(1);
}])
->get();
$TheSeries = TheSeries::with(['TheEpisodes' => function($q) {
$q->orderBy('created_at', 'desc')->first();
}])
->get();
can't work?
Why don't you use the DB statement ?
DB::table('TheEpisodes')
->leftjoin('TheSeries','TheEpisodes.SeriesId','=','TheSeries.id')
->select('TheEpisodes.*','TheSeries.id as sId','TheSeries.name as sName',...)
->orderBy('TheEpisodes. addDate','desc')
->take(1)
->get();
You can try something like this in your TheSeries model: (it is easier)
public function getLatestEpisodeAttribute(){
$episode = TheEpisodes::where('series_id',$this->attributes['id'])
->latest()
->first();
if(!$episode){
return "no episodes for this Series";
}
return $episode;
}
On your controller just do the query normally without including anything related to TheSeries and you can access it values in your blade file like this:
//lets suppose there is a title attibute in the episodes
{{$TheSeries->latest_episode->title}}
//or a duration attribute
{{$TheSeries->latest_episode->duration}}
The best solution I found, was to create a one-to-one relationship with orderBy('addDate'), it works!!!
Reference: https://softonsofa.com/tweaking-eloquent-relations-how-to-get-latest-related-model/
$TheSeries = TheSeries::with('TheEpisodes')->first();
Or
$TheSeries = TheSeries::with('TheEpisodes')->firstOrFail();
I have a Posts table it has three fields id, title, description.
My Post Model
class Post extends Model
{
use SoftDeletes;
protected $fillable = ['title', 'description'];
public function tags()
{
return $this->belongsToMany(Tag::class, 'post_tag');
}
}
My Tag Model
class Tag extends Model
{
use SoftDeletes;
protected $fillable = ['name'];
public function posts()
{
return $this->belongsToMany(Post::class, 'post_tag');
}
}
Now I want to get posts & paginate where I have a tag filter e.g I have two tags animals & news which has id 1 & 2. Now I want to get all posts which has tag 1 & 2 & paginate. Here is what I tried
Post:: with('tags')->whereHas('tags', function($q) {
$q->whereIn('id', [1, 2]);
})->paginate();
But here as I am whereIn it returns posts has tags 1 or 2 or both. But I want post who has both tag id 1 & 2.
I am using Laravel 5.2.
You'll have to loop through your list of ids to add that condition, then. For instance:
$query = Post::with('tags');
foreach ($ids as $id) {
$query->whereHas('tags', function($q) use ($id) {
$q->where('id', $id);
});
}
$query->paginate();
I have been looking for the same thing and inspired by this stackoverflow MySQL answer, I have ended up with this
Code:
Post:: with('tags')->whereHas('tags', function($q) {
$idList = [1,2];
$q->whereIn('id', $idList)
->havingRaw('COUNT(id) = ?', [count($idList)])
})->paginate();
Because I think I might use it in a few places I have made it into a trait which you can view here. Which if you included the trait in your Post class you could use like the following.
Code:
Post::with('tags')->whereHasRelationIds('tags', [1,2])->paginate();
You can also use whereHas()'s third and fourth parameter in combination with whereIn():
$keys = [1, 2];
$list->whereHas(
'genres',
fn ($query) => $query->whereIn('id', $keys),
'=',
count($keys)
);
I don't think there's a built in method for this, but I would suggest putting the foreach loop inside the whereHas method just for neatness sake.
$query = Post::with('tags')->wherehas('tags', function ($q) use ($ids) {
foreach ($ids as $id) {
$q->where('id', $id);
}
})->paginate(10);
I have a simple code and what I want to do is to access a field from another table and put it in my where clause. This is my code:
ReportController.php
$reservations = Reservation::with('charge', 'room', 'client')
-> whereBetween('reservation_from', [$from, $to])
-> where('room.type', \Request::input('type')) //what should this be
-> orderBy('created_at')
-> get();
Room.php
class Room extends Model
{
use SoftDeletes;
protected $table = 'rooms';
protected $fillable = ['id', 'roomNumber', 'type', 'price', 'description'];
public function reservations() {
return $this->hasMany('App\Reservation', 'id', 'room_number');
}
}
Reservation.php
class Reservation extends Model
{
use SoftDeletes;
protected $table = 'reservations';
protected $fillable = ['roomNumber', 'clientId', 'reservation_from', 'reservation_to'];
public function room() {
return $this->belongsTo('App\Room');
}
}
Schema:
As you can see in the ReportController.php, there is a comment saying "what should this be", that's the part that I want to fix. What I wanted to do is access the type field in the rooms table in my eloquent query.
The query that I want to do is like this:
select * from `reservations` where `reservation_from` between '2015-10-29' and '2015-10-29' and `rooms.type` = "test"
Is there a way to do this? Thank you.
What you are looking for is the whereHas method.
$reservations = Reservation::with('charge', 'room', 'client')
->whereBetween('reservation_from', [$from, $to])
->whereHas('room', function($query) {
$query->where('type', '=', \Request::input('type'));
})
->orderBy('created_at')
->get();
Link to docs: http://laravel.com/docs/5.1/eloquent-relationships#querying-relations
Edit:
Editing this to clarify some things in the comments.
To create convenient, reusable query constraints to make your code cleaner, you can use query constraints: http://laravel.com/docs/5.1/eloquent#query-scopes
Also, because queries can be chained, you can do something like this:
// Create query with common constraints
$query = Reservation::with('charge', 'room', 'client')
->whereBetween('reservation_from', [$from, $to]);
// Ternary operator to decide whether or not to add whereHas constraint
$query = (\Request::input('type') == "all") ? $query : $query->whereHas('room', function($query) {
$query->where('type', '=', \Request::input('type'));
});
// Finally, fetch the results sorted by 'latest', which is a convenient way of doing "orderBy('created')"
$reservations = $query->latest()->get();
I believe you are looking to do this. Updated per your question update. The with method takes a string or an array.
$reservations = Reservation::with(['charge', 'client', 'room' =>
function($query){
$query->where('type', \Request::input('type'));
}])
->whereBetween('reservation_from', [$from, $to])
->orderBy('created_at')
->get();