Laravel Route Binding Model Undefined offset:9

时间:2018-01-03 06:26:03

标签: laravel laravel-5.2

我正在使用laravel 5.2。我有一个名为Book的模型,然后尝试将路径绑定模型从我的控制器移动到RouteServiceProvider.php

这是我的路线绑定模型,然后将其移至RouteServiceProvider.php

$book = Book::findOrFail($id);

之后(在我的RouteServiceProvider.php中)

$router->model('book', 'App\Book');

在我将它移动到RouteServiceProvider之前它完全有效,但现在它给我一个错误eveytime我尝试更新,(创建和存储工作正常)。

无法更新数据库的输入,但是当我尝试使用dd($this->method());在请求的规则顶部进行调试时,它会说“PATCH”

错误是未定义偏移:9 ... framework \ src \ Illuminate \ Validation \ Validator.php第1461行:$extra[$segments[$i]] = $segments[$i + 1];

我认为错误来自我的更新控制器return view('dashboards.book', compact('book'));或来自补丁/更新方法的条件请求的mybe

这是完整的代码

控制器@更新

public function update(Book $book, BookRequest $request)
{
    //$book = Book::findOrFail($id);  move to the RouteServiceProvider

    $input = $request->all();

    $book->update($request->all());

    //return view('dashboards.book');
    return view('dashboards.book', compact('book'));

}

BookRequest.php

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class BookRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {

        //$book = $this->route('book');

        //$this->route()->parameter('books');

        //dd($this->method());



        // Check Create or Update
        if ($this->method() == 'POST') {
            $label_rules = 'required|string|size:6|unique:books,label,';
            $isbn_rules = 'sometimes|string|size:10|unique:books,isbn,';
        }

        else {
            $label_rules = 'required|string|size:6|unique:books,label,' .$this->route()->parameter('book'); 
            $isbn_rules = 'sometimes|string|size:10|unique:books,isbn,' .$this->route()->parameter('book');
        }




        return [
            'judul'             => 'required|string|max:100',
            'label'             => $label_rules,
            'isbn'              => $isbn_rules,
            'tanggal_terbit'    => 'required|date',
            'status'            => 'required|in:tersedia,dipinjam,hilang,rusak',
            'id_penulis'        => 'required',
            'id_penerbit'       => 'required',
            'id_kategori'       => 'required',
        ];


    }
}

RouteServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to the controller routes in your routes file.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
        //

        parent::boot($router);

        // route model binding
        $router->model('book', 'App\Book');
    }

    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function map(Router $router)
    {
        $router->group(['namespace' => $this->namespace], function ($router) {
            require app_path('Http/routes.php');
        });
    }
}

错误

in Validator.php line 1461
at HandleExceptions->handleError('8', 'Undefined offset: 9', 'E:\YUDY\WEB PROJECT\CLIENTS\perpus lab\testroutemodelbinding\vendor\laravel\framework\src\Illuminate\Validation\Validator.php', '1461', array('segments' => array('label:"099187"', 'isbn:"9918892810"', 'tanggal_terbit:"2018-02-10"', 'status:"tersedia"', 'id_penerbit:1', 'id_kategori:1', 'id_penulis:1', 'created_at:"2018-01-02 00:01:26"', 'updated_at:"2018-01-02 00:01:26"}'), 'extra' => array('label:"099187"' => 'isbn:"9918892810"', 'tanggal_terbit:"2018-02-10"' => 'status:"tersedia"', 'id_penerbit:1' => 'id_kategori:1', 'id_penulis:1' => 'created_at:"2018-01-02 00:01:26"'), 'count' => '9', 'i' => '8')) in Validator.php line 1461
at Validator->getExtraConditions(array('label:"099187"', 'isbn:"9918892810"', 'tanggal_terbit:"2018-02-10"', 'status:"tersedia"', 'id_penerbit:1', 'id_kategori:1', 'id_penulis:1', 'created_at:"2018-01-02 00:01:26"', 'updated_at:"2018-01-02 00:01:26"}')) in Validator.php line 1379
at Validator->getUniqueExtra(array('books', 'label', '{"id":15', 'judul:"Fast And Furious"', 'label:"099187"', 'isbn:"9918892810"', 'tanggal_terbit:"2018-02-10"', 'status:"tersedia"', 'id_penerbit:1', 'id_kategori:1', 'id_penulis:1', 'created_at:"2018-01-02 00:01:26"', 'updated_at:"2018-01-02 00:01:26"}')) in Validator.php line 1338
at Validator->validateUnique('label', '099187', array('books', 'label', '{"id":15', 'judul:"Fast And Furious"', 'label:"099187"', 'isbn:"9918892810"', 'tanggal_terbit:"2018-02-10"', 'status:"tersedia"', 'id_penerbit:1', 'id_kategori:1', 'id_penulis:1', 'created_at:"2018-01-02 00:01:26"', 'updated_at:"2018-01-02 00:01:26"}'), object(Validator)) in Validator.php line 485
at Validator->validate('label', 'Unique') in Validator.php line 425
at Validator->passes() in ValidatesWhenResolvedTrait.php line 24
at FormRequest->validate() in FoundationServiceProvider.php line 41
at FoundationServiceProvider->Illuminate\Foundation\Providers\{closure}(object(BookRequest), object(Application)) in Container.php line 1031
at Container->fireCallbackArray(object(BookRequest), array(object(Closure))) in Container.php line 994
at Container->fireResolvingCallbacks('App\Http\Requests\BookRequest', object(BookRequest)) in Container.php line 648
at Container->make('App\Http\Requests\BookRequest', array()) in Application.php line 697
at Application->make('App\Http\Requests\BookRequest') in RouteDependencyResolverTrait.php line 85
at ControllerDispatcher->transformDependency(object(ReflectionParameter), array('book' => object(Book)), array('book' => object(Book))) in RouteDependencyResolverTrait.php line 58
at ControllerDispatcher->resolveMethodDependencies(array('book' => object(Book)), object(ReflectionMethod)) in RouteDependencyResolverTrait.php line 41
at ControllerDispatcher->resolveClassMethodDependencies(array('book' => object(Book)), object(DashboardController), 'update') in ControllerDispatcher.php line 143
at ControllerDispatcher->call(object(DashboardController), object(Route), 'update') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(DashboardController), object(Route), object(Request), 'update') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\DashboardController', 'update') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53
at require_once('E:\YUDY\WEB PROJECT\CLIENTS\perpus lab\testroutemodelbinding\public\index.php') in server.php line 21

有关错误的任何建议吗?

1 个答案:

答案 0 :(得分:0)

您的验证规则先前假定路径段是标识符。现在模型绑定处于活动状态,它会返回一个模型实例。

$label_rules = 'required|string|size:6|unique:books,label,' . 
                    $this->route()->parameter('book'); 

这可以在堆栈跟踪中看到。

in Validator.php line 1461
at HandleExceptions->handleError('8', 'Undefined offset: 9', 'E:\YUDY\WEB PROJECT\CLIENTS\perpuslab\testroutemodelbinding\vendor\laravel\framework\src\Illuminate\Validation\Validator.php', '1461', array('segments' => array('label:"099187"', 'isbn:"9918892810"', 'tanggal_terbit:"2018-02-10"', 'status:"tersedia"', 'id_penerbit:1', 'id_kategori:1', 'id_penulis:1', 'created_at:"2018-01-02 00:01:26"', 'updated_at:"2018-01-02 00:01:26"}'), 'extra' => array('label:"099187"' => 'isbn:"9918892810"', 'tanggal_terbit:"2018-02-10"' => 'status:"tersedia"', 'id_penerbit:1' => 'id_kategori:1', 'id_penulis:1' => 'created_at:"2018-01-02 00:01:26"'), 'count' => '9', 'i' => '8')) in Validator.php line 1461
at Validator->getExtraConditions(array('label:"099187"', 'isbn:"9918892810"', 'tanggal_terbit:"2018-02-10"', 'status:"tersedia"', 'id_penerbit:1', 'id_kategori:1', 'id_penulis:1', 'created_at:"2018-01-02 00:01:26"', 'updated_at:"2018-01-02 00:01:26"}')) in Validator.php line 1379
at Validator->getUniqueExtra(array('books', 'label', '{"id":15', 'judul:"Fast And Furious"', 'label:"099187"', 'isbn:"9918892810"', 'tanggal_terbit:"2018-02-10"', 'status:"tersedia"', 'id_penerbit:1', 'id_kategori:1', 'id_penulis:1', 'created_at:"2018-01-02 00:01:26"', 'updated_at:"2018-01-02 00:01:26"}')) in Validator.php line 1338
at Validator->validateUnique('label', '099187', array('books', 'label', '{"id":15', 'judul:"Fast And Furious"', 'label:"099187"', 'isbn:"9918892810"', 'tanggal_terbit:"2018-02-10"', 'status:"tersedia"', 'id_penerbit:1', 'id_kategori:1', 'id_penulis:1', 'created_at:"2018-01-02 00:01:26"', 'updated_at:"2018-01-02 00:01:26"}'), object(Validator)) in Validator.php line 485
at Validator->validate('label', 'Unique') in Validator.php line 425

唯一验证规则支持多个参数,您尝试传入的ID实际上是{"id":15'(第一个逗号之前的数组的初始部分),然后是主键列'judul:"Fast And Furious"' ,然后尝试将其余条目解析为额外条件。它们应该成对出现(column,value,column,value,...),但在你的情况下,数组中只剩下7个条目,数字不均匀。因此失败了。