Laravel Route Binding Model Undefined offset:9

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel Route Binding Model Undefined offset:9相关的知识,希望对你有一定的参考价值。

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

这是我的路由绑定模型,然后我将它移动到RouteServiceProvider.php

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

之后(在我的RouteServiceProvider.php中)

$router->model('book', 'AppBook');

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

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

错误是Undefined offset:9 ... framework src Illuminate Validation Validator.php line 1461:$extra[$segments[$i]] = $segments[$i + 1];

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

这是完整的代码

Controller@update

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

use AppHttpRequestsRequest;

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

use IlluminateRoutingRouter;
use IlluminateFoundationSupportProvidersRouteServiceProvider 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 = 'AppHttpControllers';

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

        parent::boot($router);

        // route model binding
        $router->model('book', 'AppBook');
    }

    /**
     * Define the routes for the application.
     *
     * @param  IlluminateRoutingRouter  $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:YUDYWEB PROJECTCLIENTSperpus lab	estroutemodelbindingvendorlaravelframeworksrcIlluminateValidationValidator.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->IlluminateFoundationProviders{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('AppHttpRequestsBookRequest', object(BookRequest)) in Container.php line 648
at Container->make('AppHttpRequestsBookRequest', array()) in Application.php line 697
at Application->make('AppHttpRequestsBookRequest') 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->IlluminateRouting{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->IlluminateRouting{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), 'AppHttpControllersDashboardController', '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->IlluminateRouting{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->IlluminateRouting{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->IlluminateFoundationHttp{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->IlluminateRouting{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:YUDYWEB PROJECTCLIENTSperpus lab	estroutemodelbindingpublicindex.php') in server.php line 21

对错误的任何建议?

答案

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

$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:YUDYWEB PROJECTCLIENTSperpuslab	estroutemodelbindingvendorlaravelframeworksrcIlluminateValidationValidator.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个条目,数字不均匀。因此失败了。

以上是关于Laravel Route Binding Model Undefined offset:9的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Form-Model Binding 多选默认值

Laravel - Route::resource 与 Route::controller

Laravel - 使用 Redirect::route() 添加斜杠

Laravel 4 - Route::resource 与 Route::controller。使用哪个?

Laravel 5 Route Group 和 Basic(/) GET Route 组内

Laravel route ---- resoure