0

I have model looks like this folowing Product model code:

public class Product extends Model
{
    public function types()
    {
        return $this->belongsToMany(Type::class)
            ->withPivot('published');
    }
}

and here the Types model:

public class Type extends Model
{
    public function products()
    {
        return $this->belongsToMany(Product::class)
            ->withPivot('published');
    }
}

in nova ProjectResource I have this following field :

BelongsToMany::make('Types')
    ->fields(function() {
        return [
            Boolean::make('Published')
        ];
     })->actions(function() { return new Action/UpdateProductTypeActions }),

in nova TypeResource I have this following field :

BelongsToMany::make('Projects')
        ->fields(function() {
            return [
                Boolean::make('Published')
            ];
         }),

and I wanna make this published attribute from pivot table updateable using Nova Actions

and I already create UpdateProductTypeActions like this following code:

public function handle(ActionFields $fields, Collection $models)
    {
        foreach ($models as $model) {

        }
    }

my questions how to get product_id from those actions?

naticap
  • 379
  • 1
  • 5
  • 19

1 Answers1

0

This will give you all products that have many-to-many relations with types which have many-to-many relations with $model that here is your project:

 foreach($model->types as $type)
   {
     $products=$type->products->pluck(id);
   }
Ramisa
  • 21
  • 4
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Jeremy Caney Sep 07 '21 at 00:28
  • But what if you try publish ony one or several products (not all of them)? How you can get them? – Hayate Apr 17 '22 at 15:52