skip to Main Content

I am strugling with this problem for hours.
The problem appear from Laravel when I am trying make a POST API call. I have to mention that GET call is working fine.

The error is this:
IlluminateContractsContainerBindingResolutionException: Unresolvable dependency resolving [Parameter #0 [ <required> $resource ]] in class IlluminateHttpResourcesJsonJsonResource in file C:laragonwwwsmart-life-stylevendorlaravelframeworksrcIlluminateContainerContainer.php on line 1032

This is my laravel code:

api.php

<?php
use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;

Route::post('login', 'UserController@login')->name('login');
Route::post('register', 'UserController@register')->name('register');
Route::group(['middleware' => 'auth:api'], function(){
    Route::post('details', 'UserController@details');
    Route::apiResource('schedule', 'ScheduleController');
});

ScheduleController

<?php

namespace AppHttpControllers;

use AppSchedules;
use AppHttpResourcesSchedule;
use IlluminateHttpRequest;

class ScheduleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index(){
        return Schedule::collection(Schedules::all());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return IlluminateHttpResponse
     */
    public function create(){
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    public function store(Schedule $request)
    {
        $schedule = new Schedules();

        if (isset($request['title'])) {
            $schedule->title = $request['title'];
        };

        if (isset($request['start'])) {
            $schedule->start = $request['start'];
        };

        if (isset($request['end'])) {
            $schedule->end = $request['end'];
        };

        if ($schedule->save()) {
            return response()->json(new Schedule($schedule), 200);
        } else {
            return response()->json('Error can't save schedule', 500);
        };
    }

    /**
     * Display the specified resource.
     *
     * @param  AppSchedule  $schedule
     * @return IlluminateHttpResponse
     */
    public function show(Schedule $id)
    {
        return response()->json(new Schedule(Schedules::find($id)), 200);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  AppSchedule  $schedule
     * @return IlluminateHttpResponse
     */
    public function edit(Schedule $schedule)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  AppSchedule  $schedule
     * @return IlluminateHttpResponse
     */
    public function update(Request $request, Schedule $schedule)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  AppSchedule  $schedule
     * @return IlluminateHttpResponse
     */
    public function destroy(Schedule $id)
    {
        if (Schedules::find($id)->delete()) {
            return response()->json('Success on delete carpet', 200);
        } else {
            return response()->json('Error can't delete carpet', 500);
        }
    }

    public function creazaProgramare(Schedule $request)
    {
        $schedule = new Schedules();

        if (isset($request['title'])) {
            $schedule->title = $request['title'];
        };

        if (isset($request['start'])) {
            $schedule->start = $request['start'];
        };

        if (isset($request['end'])) {
            $schedule->end = $request['end'];
        };

        if ($schedule->save()) {
            return response()->json(new Schedule($schedule), 200);
        } else {
            return response()->json('Error can't save schedule', 500);
        };
    }

}

ScheduleResource

<?php

namespace AppHttpResources;

use IlluminateHttpResourcesJsonJsonResource;

class Schedule extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  IlluminateHttpRequest $request
     * @return array
     */
    public function toArray($request)
    {
        return [
                'title' => $this->title,
                'start' => $this->start,
                'end' => $this->end
        ];
    }
}

Schedule Model

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Schedules extends Model
{
    //
}

I tried to run "composer install" and nothing happened.

Laravel version: 7.30.4
PHP: 7.2.19 (cli)

Should be there any difference if in postman at body I use "form-data" or "x-www-form-urlencoded"?

3

Answers


  1. Chosen as BEST ANSWER

    I found the problem due to another post.

    The problem was that in ScheduleController I was calling $request with different type than it should.

    Change: public function store(Schedule $request)

    Into: public function store(Request $request)

    Thank you


  2. You can try with this command

    php artisan optimize:clear

    Login or Signup to reply.
  3. u must change this:
    return response()->json(new Schedule($schedule), 200);
    on:
    return response()->json(new Schedule([‘resource’ => $schedule]), 200);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search