Laravel 未定义偏移量:1 - 由路由引起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23557798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:30:06  来源:igfitidea点击:

Laravel Undefined Offset: 1 - cause by routing

phplaravellaravel-4

提问by Jhorra

I get the error when trying to make a post call to /api/subject/search

我在尝试向 /api/subject/search

I assume it's a simple syntax error I'm missing

我认为这是我遗漏的一个简单的语法错误

I have my api routes defined below

我在下面定义了我的 api 路由

Route::group(array('prefix' => 'api'), function()
{
    Route::post('resource/search', 'ResourceController');
    Route::resource('resource', 'ResourceController');


    Route::post('subject/search', 'SubjectController');
    Route::resource('subject', 'SubjectController');

    Route::resource('user', 'UserController');

    Route::controller('/session', 'SessionController');
    Route::post('/login', array('as' => 'session', 'uses' => 'SessionController@Store'));
});

And my controller is mostly empty

我的控制器大部分是空的

class SubjectController extends \BaseController 
{
    public function search()
    {
        $subjects = [];
        if((int)Input::get('grade_id') < 13 && (int)Input::get('grade_id') > 8)
            $subjects = Subject::where('name', 'like', '%HS%')->get();
        else
            $subjects = Subject::where('name', 'not like', '%HS%')->get();

        return Response::json([
            'success' => true,
            'subjects' => $subjects->toArray()
        ]);
    }
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
            //
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
            //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
            //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
            //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
            //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
            //
    }

}

回答by cs45977

You need to specify the method.

您需要指定方法。

try

尝试

Route::post('subject/search', 'SubjectController@search');

See the named route example: Laravel Docs

查看命名路由示例: Laravel Docs

回答by sidneydobber

In your case I think searchis not resolved by the controller to load the search()method. You are also sending a POST for search functionality and I guess it's better to do a GET request since POST and PUT are for storing data.

在您的情况下,我认为控制器未解决搜索以加载search()方法。您还发送了用于搜索功能的 POST,我想最好执行 GET 请求,因为 POST 和 PUT 用于存储数据。

Conventions
When creating API's it's a good thing to stick to naming conventions and patterns.

约定
在创建 API 时,坚持命名约定和模式是一件好事。

Solution
Your route could be simpler like this: api.yourdomain.com/api/subject?search=term1,term2. Doing this with a GET query makes it going to the index()method. There you can check the GETparams and do your search stuff and return.

解决
您的路线可能是这样的简单:api.yourdomain.com/api/subject?search=term1,term2。使用 GET 查询执行此操作使其转到该index()方法。在那里您可以检查GET参数并进行搜索并返回。

Check this for the cleanest and truely RESTful way to make an API in Laravel:

检查这个在 Laravel 中制作 API 的最干净和真正的 RESTful 方式:

回答by zeeawan

I got same error when accessing object at index of an empty array in view blade php file.

在查看刀片 php 文件中访问空数组索引处的对象时,我遇到了同样的错误。