Laravel API ErrorException:未定义的变量:

时间:2019-04-13 10:44:09

标签: json laravel postman

我正在尝试从该Laravel CRUD API开始。到目前为止,显示和删除作品有效(我正在使用邮递员和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 个答案:

答案 0 :(得分:1)

这里有错字

    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;

答案 1 :(得分:0)

您的代码中有一个错字:

$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;