Laravel API ErrorException: неопределенная переменная: - PullRequest
0 голосов
/ 13 апреля 2019

Я пытаюсь начать с этим API Laravel для CRUD. Пока что работает программа show and delete (я использую сценарий postman & json), но каким-то образом, когда я пытаюсь сделать запрос на публикацию, я получаю эту ошибку:


ErrorException: Undefined variable: requests in file C:\xampp\htdocs\psdsystems\app\Http\Controllers\CustomerController.php on line 35
Stack trace:
  1. ErrorException->() C:\xampp\htdocs\psdsystems\app\Http\Controllers\CustomerController.php:35
  2. Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() C:\xampp\htdocs\psdsystems\app\Http\Controllers\CustomerController.php:35
  3. App\Http\Controllers\CustomerController->store() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:54
  4. call_user_func_array() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:54
  5. Illuminate\Routing\Controller->callAction() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:45
  6. Illuminate\Routing\ControllerDispatcher->dispatch() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Route.php:219
  7. Illuminate\Routing\Route->runController() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Route.php:176
  8. Illuminate\Routing\Route->run() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Router.php:680
  9. Illuminate\Routing\Router->Illuminate\Routing\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:30
 10. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php:41
 11. Illuminate\Routing\Middleware\SubstituteBindings->handle() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:163
 12. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 13. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php:58
 14. Illuminate\Routing\Middleware\ThrottleRequests->handle() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:163
 15. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 16. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:104
 17. Illuminate\Pipeline\Pipeline->then() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Router.php:682
 18. Illuminate\Routing\Router->runRouteWithinStack() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Router.php:657
 19. Illuminate\Routing\Router->runRoute() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Router.php:623
 20. Illuminate\Routing\Router->dispatchToRoute() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Router.php:612
 21. Illuminate\Routing\Router->dispatch() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:176
 22. Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:30
 23. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() C:\xampp\htdocs\psdsystems\vendor\fideloper\proxy\src\TrustProxies.php:57
 24. Fideloper\Proxy\TrustProxies->handle() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:163
 25. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 26. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php:21
 27. Illuminate\Foundation\Http\Middleware\TransformsRequest->handle() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:163
 28. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 29. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php:21
 30. Illuminate\Foundation\Http\Middleware\TransformsRequest->handle() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:163
 31. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 32. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php:27
 33. Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:163
 34. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 35. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php:62
 36. Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:163
 37. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 38. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:104
 39. Illuminate\Pipeline\Pipeline->then() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:151
 40. Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter() C:\xampp\htdocs\psdsystems\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:116
 41. Illuminate\Foundation\Http\Kernel->handle() C:\xampp\htdocs\psdsystems\public\index.php:55

Я уже изменил свой первичный ключ внутри своей модели, так как я сделал мой первичный ключ, который обычно просто "id", на "CustomerID"

API:

<?php

use Illuminate\Http\Request;

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

//List Customers Details
Route::get('customers','CustomerController@index');

//List Single Customer Detail
Route::get('customer/{CustomerID}','CustomerController@show');

//Create new Customer Details
Route::post('customer','CustomerController@store');

//Update Customer Details
Route::put('customer','CustomerController@store');

//Delete Customer Details
Route::delete('customer/{CustomerID}','CustomerController@destroy');

Контроллер:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Customer;
use App\Http\Resources\Customer as CustomerResource;

class CustomerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // Get customer details
        $customers = Customer::paginate(10);

        //Return collection of Customers as a resource
        return CustomerResource::collection($customers);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $customer = $requests->isMethod('put') ? Customer::findOrFail($request->CustomerID) : new Customer;

        $customer->CustomerID = $request->input('CustomerID');
        $customer->Customer = $request->input('Customer');
        $customer->Address = $request->input('Address');

        if($customer->save()) {
            return new CustomerResource($customer);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $CustomerID
     * @return \Illuminate\Http\Response
     */
    public function show($CustomerID)
    {
        //Get single customer detail
        $customer = Customer::findOrFail($CustomerID);

        //Return single customer as a resource
        return new CustomerResource($customer);



    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($CustomerID)
    {
         //Get single customer detail
         $customer = Customer::findOrFail($CustomerID);

         if($customer->delete()) {
            return new CustomerResource($customer);
         }
    }
}

Модель:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    protected $primaryKey = 'CustomerID';

}

Я ожидаю, что почтовый запрос действительно будет работать

Ответы [ 2 ]

1 голос
/ 13 апреля 2019

Здесь есть опечатка

    public function store(Request $request)
    {
        $customer = $requests->isMethod('put') ? Customer::findOrFail($request->CustomerID) : new Customer;

дополнительные s в переменной запроса должны быть

    public function store(Request $request)
    {
        $customer = $request->isMethod('put') ? Customer::findOrFail($request->CustomerID) : new Customer;
0 голосов
/ 13 апреля 2019

в вашем коде есть опечатка:

$customer = $requests->isMethod('put') ? Customer::findOrFail($request->CustomerID) : new Customer; // remove the `s`, it's $request

Это должно быть:

$customer = $request->isMethod('put') ? Customer::findOrFail($request->CustomerID) : new Customer;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...