Laravel 5 problem with wherePivot

I work with Laravel 5, and I am having trouble ->wherePivot()working on many-to-many relationships. When I dd()look like Eloquent SQL, it looks for records in the pivot table with `pose_state`.pose_id` null.

I hope this is a simple mistake, not a mistake. Any ideas are welcome.

Database structure

create

id
name
type

state

id
name
machine_name

pose_state

pose_id
state_id
status

Models

Pose

<?php namespace App;
use DB;
use App\State;
use Illuminate\Database\Eloquent\Model;

class Pose extends Model {

  public function states()
  {
      return $this->belongsToMany('App\State')
                  ->withPivot('status_id')
                  ->withTimestamps();
  }
  public function scopeWithPendingReviews()
  {
      return $this->states()
                  ->wherePivot('status_id',10);
  }
}

State

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class State extends Model {

    public function poses()
    {
      return $this->belongsToMany('Pose')
                  ->withPivot('status_id')
                  ->withTimestamps();
    }

}

PosesController Function

public function listPosesForReview(){
    $poses = Pose::withPendingReviews()->get();
    dd($poses->toArray() );
}

SQL

select 
  `states`.*, `pose_state`.`pose_id` as `pivot_pose_id`,
  `pose_state`.`state_id` as `pivot_state_id`, 
  `pose_state`.`status_id` as `pivot_status_id`, 
  `pose_state`.`created_at` as `pivot_created_at`, 
  `pose_state`.`updated_at` as `pivot_updated_at` 
from 
  `states` inner join `pose_state` on `states`.`id` = `pose_state`.`state_id` 
where 
  `pose_state`.`pose_id` is null and `pose_state`.`status_id` = ?

EDIT

When I updated my code before deleting the area in which it worked. Thanks @Deefour for putting me on the right track! Perhaps there is something else in scope that I am missing.

public function pendingReviews()
{
  return $this->states()
              ->wherePivot('status_id','=', 10);
}

GET ANOTHER CHANGE

- , . . , , , .

public function scopeWithStatusCode($query, $tag)
{
  $query->with(['states' => function($q) use ($tag)
            {
              $q->wherePivot('status_id','=', $tag);
            }])
        ->whereHas('states',function($q) use ($tag)
            {
              $q->where('status_id', $tag);
            });
}
+4
2

, scopeWithPendingReviews() .

, , wherePivot() . ,

public function wherePivot($column, $operator = null, $value = null, $boolean = 'and')

public function wherePivot($column, $value = null, $boolean = 'and')

->wherePivot('status_id',10)

->wherePivot('status_id', '=', 10)

,

SomeModel::newQuery()

, (: 'scoped') , .

, scope, .

Pose states,

$this->states()

SQL , . , . :

public function scopeWithPendingReviews($query) {
  $query->join('pose_state', 'poses.id', '=', 'pose_state.pose.id')
        ->where('status_id', 10);
}

pendingReviews(), State, Pose.

, .

$poses = Pose::withPendingReviews();

$poses = Pose::newQuery()->withPendingReviews();

, . .

.

  • wherePivot(), .
  • withTimestamps() .
  • - " ", . withTimestamps(), , - , . , created_at updated_at.
+2

, , , , - . , status, where status_id

Try:

->wherePivot('status', 10);

, withTimestamps() . ( ), , , , / . , , timestamp, , , .

+2

All Articles