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 ?

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Dester Dezzods
  • 1,417
  • 4
  • 17
  • 33

3 Answers3

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);
}
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • I've already answered your question but now you've changed the question, that's not right. – The Alpha Mar 24 '16 at 08:31
  • 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. – Dester Dezzods Mar 24 '16 at 08:46
  • 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 Alpha Mar 24 '16 at 08:49
  • 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. – Dester Dezzods Mar 24 '16 at 08:52
  • 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`. – The Alpha Mar 24 '16 at 08:54
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);
    }
}
Nick
  • 9,735
  • 7
  • 59
  • 89
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);
    }
}
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • 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... – matiaslauriti Jul 18 '21 at 07:32