extractModelAndKeys() public static method

Extract the model instance and model keys from the given parameters.
public static extractModelAndKeys ( string | Model | Illuminate\Database\Eloquent\Collection $model, array $keys = null ) : array
$model string | Illuminate\Database\Eloquent\Model | Illuminate\Database\Eloquent\Collection
$keys array
return array
Example #1
0
 /**
  * Constrain the given roles query to those that were assigned to the given authorities.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $query
  * @param  string|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection  $model
  * @param  array  $keys
  * @return void
  */
 public function constrainWhereAssignedTo($query, $model, array $keys = null)
 {
     list($model, $keys) = Helper::extractModelAndKeys($model, $keys);
     $query->whereExists(function ($query) use($model, $keys) {
         $table = $model->getTable();
         $key = "{$table}.{$model->getKeyName()}";
         $pivot = Models::table('assigned_roles');
         $roles = Models::table('roles');
         $prefix = Models::prefix();
         $query->from($table)->join($pivot, $key, '=', $pivot . '.entity_id')->whereRaw("{$prefix}{$pivot}.role_id = {$prefix}{$roles}.id")->where("{$pivot}.entity_type", $model->getMorphClass())->whereIn($key, $keys);
     });
 }
Example #2
0
 /**
  * Retract the role from the given model(s).
  *
  * @param  string|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection  $model
  * @param  array|null  $keys
  * @return $this
  */
 public function retractFrom($model, array $keys = null)
 {
     list($model, $keys) = Helper::extractModelAndKeys($model, $keys);
     $this->newBaseQueryBuilder()->from(Models::table('assigned_roles'))->where('role_id', $this->getKey())->where('entity_type', $model->getMorphClass())->whereIn('entity_id', $keys)->delete();
     return $this;
 }