Relationship between pivot tables - return resource do not found using pivot or belongsToMany

24 views Asked by At

good afternoon!

I would like some help from them regarding the scenario below:

I have 3 tables, 1 is the Moviment which is the main one, the 2 is the cart_moviments which is the secondary one which when I enter the movement data, it saves some data in cart_moviments and I have the 3 table called vehicles, which I have all the vehicle information, all of them have a foreignId, just to relate them.

So on my return from the resource, I have Moviment->cartMoviment, and I want to access it from cartMoviment ->vehicles, which I can't, because it doesn't relate.

My code Store:

    public function store(StoreMovimentRequest $request, StoreCartMovimentRequest $requestCart, StoreDocumentRequest $requestDocument)
    {

        $this->authorize('create', Moviment::class);
        $moviment = Moviment::create($request->validated());
        $moviment->cartMoviment()->create($request->validated());

        foreach(array($request['key_number']) as $notasFiscais) {
            foreach($notasFiscais as $notas) {
                $moviment->document()->create($notas);
            }
        }
        return new MovimentResource($moviment);
    }

My CartMoviment MOdel:

class CartMoviment extends Model
{
    use HasApiTokens, HasFactory, Notifiable, UserTenant;
    protected $fillable = ['type', 'vehicle_cart_id'];

    // protected $table = 'cart_moviments';

    public function Moviment() {
        return $this->belongsTo(Moviment::class);
    }

    public function Vehicle() {
        return $this->belongsTo(Vehicle::class, 'vehicle_cart_id');
    }

My Moviment Model:

class Moviment extends Model
{
    use HasApiTokens, HasFactory, Notifiable, UserTenant;

    protected $fillable = [
        'document_type_id', 'people_id', 'company_id', 'vehicle_id', 'department_id'
    ];

    public function Vehicle() {
        return $this->belongsTo(Vehicle::class);
    }

    public function cartMoviment() {
        return $this->belongsToMany(Moviment::class, cartMoviment::class); 
    }

`

My resource Array:

  public function toArray($request)
    {
        return [
            'id'                            => $this->id,
            // 'count'                         => $this->count(),
            'type'                          => $this->type,
            'department_id'                 => $this->department_id,
            'person_id'                     => $this->people_id,
            'person_name'                   => $this->person->name,
            'vehicle_id'                    => $this->vehicle_id,
            'vehicle_board'                 => $this->vehicle->board,
            'vehicle_status'                => $this->vehicle->status,
            'vehicle_manufacturer'          => $this->vehicle->vehicleModel->name,
            'vehicle_type'                  => $this->vehicle->vehicleType->type,
            'cart_moviment'                 => $this->cartMoviment,

My return resource

"cart_moviment": [
        
      ],
      "documents": [
        
      ],

`

I need relationship , thanks you very match.

0

There are 0 answers