3

I have these tables:

  1. places
    • id
    • name
    • address
  2. users
    • id
    • name
    • email
  3. reviews
    • id
    • place_id
    • user_id
    • title
    • review
    • as_anoniem

The user_id is filled even if as_anoniem is 1.

Now I want to have all the reviews for all the places with the user except for the ones with as_anoniem = 1.

Something like this:

Place::with(['review' => function ($query) {
    return $query->with('user')->where('as_anoniem', 1);
}]);

This is not fully correct as it returns only reviews with as_anoniem = 1.

How can I achieve this using ?

2
  • What! you have just changed the question right after I answerd, what is your question at all? Commented Mar 24, 2016 at 8:30
  • @TheAlpha, yes sorry to tell u that first. The first question wasn't clear enough. Commented Mar 24, 2016 at 8:31

3 Answers 3

3

You may try this:

$users = \App::User::with('reviews' => function($query) {
    $query->where('as_anoniem', '!=', 1);
})->get();

This will require you to create a one-to-many relationship in App\User model, for example:

// App\User.php
public function reviews()
{
    // namespace: App\Review
    return $this->hasMany(Review::class);
}

Assumed that, the namespace for both User & Review is App, they are in the same directory.

Update After the original question was changed by OP:

$places = \App::Place::with('reviews' => function($query) {
    $query->with('user')->where('reviews.as_anoniem', '!=', 1);
})
->get();

Place Model:

public function reviews()
{
    // namespace: App\Review
    return $this->hasMany(Review::class);
}

Review Model:

public function user()
{
    // namespace: App\User
    return $this->belongsTo(User::class);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I've already answered your question but now you've changed the question, that's not right.
This doesn't work because it will give ONLY the reviews with as_anoniem=1. I want all the reviews but only the user of the reviews which don't have as_anoniem= 1.
This suppose to work because as_anoniem field is in the reviews table and I've used the right code as: $query->with('user')->where('as_anoniem', '!=', 1); so it should work, make sure you didn't make any mistakes.
the where applies to reviews so it won't show not show the anoniem reviews as all. Do you understand what i mean? sql will be something like where as_anoniem != 1 but I want all the reviews.
Yes, I've done that exactly, where('reviews.as_anoniem', '!=', 1) means that gimme all the reviews with users where the review.as_anoniem is not 1.
0

It is possible to use values of model in conditions:

class Content extends Model
{

    // ...

    /**
     * Get linked content
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function linked()
    {
        return $this->hasMany(self::class, 'source_content_id', 'source_content_id')
            ->where('source_content_type_id', '=', $this->source_content_type_id)
            ->where('id', '<>', $this->id);
    }
}

3 Comments

Such conditions don't work with eager loading
@AnatolySokolov is there any way we can use it with eager loading
@HamzaAfzal didn't met any of cases i should figure out yet (fortunately)
0

You can check out this link https://stackoverflow.com/a/18600698/16237933.

class Game extends Eloquent {
    // many more stuff here

    // relation without any constraints ...works fine 
    public function videos() {
        return $this->hasMany('Video');
    }

    // results in a "problem", se examples below
    public function available_videos() {
        return $this->videos()->where('available','=', 1);
    }
}

1 Comment

This is in no way a good answer, as you are copy pasting code from one question to another one... in this case, just mark the original question as a duplicate. Still, it is not a duplicate... Just use author code instead of copying code out of context...

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.