vendredi 23 décembre 2016

Laravel returns null on request

I am working on a Laravel API and the last few hours, the following tries to drive me crazy.

I have a model called Group and a route as:

Route::get('/groups/numberofgroups', 'GroupsController@getNumberOfGroups'); `

My controller is:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Group;

class GroupsController extends Controller
{

    public function getNumberOfGroups(Request $request)
    {
      $groupid = $request['groupid'];

      $numberOfGroups = Group::where('GroupId', $groupid)->max('GroupLevel');

      return response()->json(compact('numberOfGroups','groupid'));
    }

}

When I try on Postman a get method on:

http://localhost/laravel/public/api/groups/numberofgroups?groupid=50

I have the following response:

{"numberOfGroups":null,"groupid":null}

But if I run the query directly on the sqllite I have the answer I need

SELECT MAX(GroupLevel) FROM Groups WHERE GroupId=50

For a reason I cannot understand, the $request cannot parse the groupid variable on the url.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire