lundi 26 mars 2018

Laravel Resource is empty

I'm passing data to these functions via Axios/Vue. The Eloquent interactions work perfectly. When I store (i.e. create a new call) the resource returns as expected. When I update a record, it updates in the database, however, I get a blank response. In other words the return new CallResource($call) returns nothing. I can't work out where I've gone wrong.

public function store(Request $request)
{
    $call = $this->validate($request, [
        'title' => 'required',
        'job_id' => 'required',
        'location' => 'required',
        'starts' => 'required|date|before:ends',
        'ends' => 'required|date|after:starts',
        'rate' => 'required'
    ]);

    $call = Call::create($call);

    return new CallResource($call);
}

public function update(Request $request, $id)
{
    $data = $this->validate($request, [
        'title' => 'required',
        'job_id' => 'required',
        'location' => 'required',
        'starts' => 'required|date|before:ends',
        'ends' => 'required|date|after:starts',
        'rate' => 'required'
    ]);

     $call = Call::find($id);
     $call->update($data);

    return new CallResource($call);
}

The call resource is really simple

class CallResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
        'id' => $this->id,
        'title' => $this->title,
        'location' => $this->location,
        'starts' => $this->starts,
        'ends' => $this->ends,
        'rate' => $this->rate
        ];
    }
}

Laravel 5.6 fyi.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire