Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestCriteria supports for Morph relationship #773

Open
oh-bala opened this issue Feb 24, 2022 · 0 comments
Open

RequestCriteria supports for Morph relationship #773

oh-bala opened this issue Feb 24, 2022 · 0 comments

Comments

@oh-bala
Copy link

oh-bala commented Feb 24, 2022

Problem

Query for morphTo relationship will get an exception of "Please use whereHasMorph for morph relationship" error.

Idea of change

Add new condition for a bit more complex setups as array.

// Example, the data relationship has different morph types for purpose, and we can specify a condition as array for those needed parameters. 

public ProductRepository extends BaseRepository {
    protected $fieldSearchable = [
        'product_name' => 'like',
        'data.category' => [
            'relationType' => 'morph',
            'condition' => '=',
            'types' => [Turnkey::class, Pcba::class, Pcb::class, MouldTooling::class, Accessory::class],
        ],
    ];
// Changed RequestCriteria class

// patch for morph relationship query
if (is_array($condition)) {
   $types = $condition['types'];
   $relationType = $condition['relationType'];
   $condition = $condition['condition'];
}

$condition = trim(strtolower($condition));

if(!is_null($relation)) {
    $callback = function($query) use($field,$condition,$value) {
       if($condition === 'in'){
           $query->whereIn($field,$value);
      }elseif($condition === 'between'){
         $query->whereBetween($field,$value);
      }else{
         $query->where($field,$condition,$value);
     }
   };

  // patch example for morph relation
   if (!is_null($relationType) && $relationType == 'morph') {
      $query->whereHasMorph($relation, $types, $callback);
      // $query->orWhereHasMorph($relation, $types, $callback); // when it shall be OR..
   } else {
       $query->whereHas($relation, $callback);
   }
}

Commenting

The idea code is just an example for describing the idea, this issue could be used to discuss if any potential problem of this change and recommendations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant