Search code examples
phpangularlaravelpostdto

Laravel post request pass class with property with type of another class


I have controller in Laravel:

public function save(Request $request, ClientOrderDTO $clientOrderDTO){
        
}

Definition of above DTO looks like:

use App\DTO\ClientDTO;

class ClientOrderDTO
{
    public $id;
    public $userId;
    public ClientDTO $client;
}

And ClientDTO looks like:

class ClientDTO
{
    public $id;
}

And when I want to send post request from Angular:

save(orderDTO: OrderDTO) {
    return this.httpClient.post<RespDTO>(`${environment.apiUrl}/client-order/save`, orderDTO);
  }

I get error:

"message": "syntax error, unexpected 'ClientDTO' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)",
    "exception": "ParseError",
    "file": "C:\\xampp\\htdocs\\crm-api\\app\\DTO\\ClientOrderDTO.php",

I can't have property in class with type as another class. What is quiet popular for example in C#.


Solution

  • I have to migrate PHP from 7.3 to 7.4 version. From version 7.4 PHP allow declare types of properties