I came to this, that's right I can't use in this case collection haha :-)
I tried your solution but receives an error:
Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method App\Http\Resources\Sale::resource() in file C:\xampp\htdocs\Customer-Service_v_2.0\app\Http\Resources\DeviceInspection.php on line 27
I found a solution for my problem and I did it that way in my api controller:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class DeviceInspection extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'sale_id' => $this->sale_id,
'inspection_date' => $this->inspection_date,
'service_man' => $this->service_man,
'status' => $this->status,
'warranty_service' => $this->warranty_service,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'user' => $this->user,
'sale'=> $this->sale
];
}
public function with($request) {
return [
'version' => '1.0.0'
];
}
}
DeviceInspection Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class DeviceInspection extends Model
{
use SoftDeletes;
/**
* The atributes that should be muted to dates.
*
* @var array
*/
protected $fillable = [
'sale_id',
'inspection_date',
'service_man',
'status'
];
protected $dates = ['deleted_at'];
public $sortable = ['created_at'];
public function sale()
{
return $this->belongsTo(Sale::class);
}
public function user() {
return $this->belongsTo(User::class);
}
}
And reply from API like this:
{
"data": {
"id": 1,
"sale_id": 1,
"inspection_date": "2020-02-21",
"service_man": null,
"status": "expectant",
"warranty_service": 1,
"created_at": "2019-03-08T13:21:43.000000Z",
"updated_at": "2019-03-08T13:21:43.000000Z",
"user": {
"id": 1,
"name": "Adrian",
"email": "[email protected]",
"sex": "m",
"department": "integrator",
"avatar": null,
"created_at": null,
"updated_at": "2019-03-14 11:37:25"
},
"sale": {
"id": 1,
"user_id": 1,
"sale_number": "Sale 045/02/2019/AG",
"customer_institution": "Institution of Customer",
"institution_address": "Institution Address",
"product_name": "Genano 310",
"manufacturer": "---",
"serial_number": "0000-0000",
"amount_of_sales": "1",
"sale_date": "2019-02-21",
"term_realization": "2019-02-21",
"guarantee_period": "24",
"directory_link": "C:/",
"status": "expectant",
"file_sales": null,
"sale_type": "national",
"created_at": "2019-03-08 14:21:43",
"updated_at": "2019-10-09 23:56:55",
"deleted_at": "2019-10-09 23:56:55"
}
},
"version": "1.0.0"
}
This is work! :-) But way your solution don't work and what is the difference between these two ways?
You won't believe it but the data from eloquent relationship was not displayed in the API response because I was using softDeletes, do you know why this happens? If he comments on "use softDeletes;" everything works correctly