0

im making an api in laravel but when i send from a post request it display nothing it work only when i send the values in the url what im i doing wrong here is my code !

$user = new userInscription;

            $user->nom = Request::get('name');
            $user->pseudo = Request::get('pseudo');
            $user->userId = Request::get('userId');
            $user->hasFiat = Request::get('hasFiat');
            $user->optin = Request::get('optin');
            $user->mail = Request::get('mail');

            $pseudo = Input::get('pseudo');
            $userId = Input::get('userId');
            $hasFiat = Input::get('hasFiat');

    if($pseudo == '' || $hasFiat == '' )
        {
            return Response::json( array(
            'status'  => 'ko',
            'message'  => 'missing mandatory parameters')
            );
        }

    else if($userId == '')
        {
            if( $user->save() )
            {
                $id = DB::table('user')
                ->where('pseudo','LIKE',$pseudo)
                ->pluck('userId');

                return Response::json(array(
                    'status'  => 'ok',
                    'message'  => 'success',
                    'userId' => $id
                ));
            }
            else
            {
                return Response::json(array(
                    'message'  => 'error while saving this user !!',   
                ));
            }
        }
2
  • Did you add a post route for this method ? Commented Aug 22, 2014 at 9:19
  • no here is my routes Route::group(array('prefix' => ''), function() { Route::resource('user', 'userInscriptionController'); }); Commented Aug 22, 2014 at 9:21

3 Answers 3

1

Laravel REST-ful (Resourceful) controlllers has pre-configured routes (can be re-configured):

According to : http://laravel.com/docs/controllers#resource-controllers

+-----------+---------------------------+---------+------------------+
|   Verb    |           Path            | Action  |    Route Name    |
+-----------+---------------------------+---------+------------------+
| GET       | /resource                 | index   | resource.index   |
| GET       | /resource/create          | create  | resource.create  |
| POST      | /resource                 | store   | resource.store   |
| GET       | /resource/{resource}      | show    | resource.show    |
| GET       | /resource/{resource}/edit | edit    | resource.edit    |
| PUT/PATCH | /resource/{resource}      | update  | resource.update  |
| DELETE    | /resource/{resource}      | destroy | resource.destroy |
+-----------+---------------------------+---------+------------------+

Referencing the table each of the Verb must correspond to the action method in the controller.

For example if your Resourceful Route is registered as:

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

Then to POST to user resource, you need to have userInscriptionController@store action (i.e. method called store() in your userInscriptionController.

To avoid manually creating each of these actions, you can use Laravel's artisan controller:make

php artisan controller:make userInscriptionController

which will generate all these actions for you, then you just need to fill in your logic to complete the resource.

Sign up to request clarification or add additional context in comments.

Comments

0

From your comment, you are using

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

which will generate following routes

Verb        |   Path                    | Action        | Route Name
------------------------------------------------------------------------
GET         | /resource                 | index         | resource.index
GET         | /resource/create          | create        | resource.create
POST        | /resource                 | store         | resource.store
GET         | /resource/{resource}      | show          | resource.show
GET         | /resource/{resource}/edit | edit          | resource.edit
PUT/PATCH   | /resource/{resource}      | update        | resource.update
DELETE      | /resource/{resource}      | destroy       | resource.destroy

And as you can see, the only action allowing post is store. So you should use this one or add post route for an other method like this :

Route::post('your_url', array('as' => 'your_route_name', 'uses' => 'YourController@yourMethod')); 

I hope it's clear now

Comments

0

if request is GET then:

if (Request::isMethod('get'))
{
$user = new userInscription;

            $user->nom = Request::get('name');
            $user->pseudo = Request::get('pseudo');
            $user->userId = Request::get('userId');
            $user->hasFiat = Request::get('hasFiat');
            $user->optin = Request::get('optin');
            $user->mail = Request::get('mail');

            $pseudo = Input::get('pseudo');
            $userId = Input::get('userId');
            $hasFiat = Input::get('hasFiat');

    if($pseudo == '' || $hasFiat == '' )
        {
            return Response::json( array(
            'status'  => 'ko',
            'message'  => 'missing mandatory parameters')
            );
        }

    else if($userId == '')
        {
            if( $user->save() )
            {
                $id = DB::table('user')
                ->where('pseudo','LIKE',$pseudo)
                ->pluck('userId');

                return Response::json(array(
                    'status'  => 'ok',
                    'message'  => 'success',
                    'userId' => $id
                ));
            }
            else
            {
                return Response::json(array(
                    'message'  => 'error while saving this user !!',   
                ));
            }
        }

}

=================== if request is POST then:

if (Request::isMethod('post'))
{
$user = new userInscription;

            $user->nom = Request::post('name');
            $user->pseudo = Request::post('pseudo');
            $user->userId = Request::post('userId');
            $user->hasFiat = Request::post('hasFiat');
            $user->optin = Request::post('optin');
            $user->mail = Request::post('mail');

            $pseudo = Input::post('pseudo');
            $userId = Input::post('userId');
            $hasFiat = Input::post('hasFiat');

    if($pseudo == '' || $hasFiat == '' )
        {
            return Response::json( array(
            'status'  => 'ko',
            'message'  => 'missing mandatory parameters')
            );
        }

    else if($userId == '')
        {
            if( $user->save() )
            {
                $id = DB::table('user')
                ->where('pseudo','LIKE',$pseudo)
                ->pluck('userId');

                return Response::json(array(
                    'status'  => 'ok',
                    'message'  => 'success',
                    'userId' => $id
                ));
            }
            else
            {
                return Response::json(array(
                    'message'  => 'error while saving this user !!',   
                ));
            }
        }

}

1 Comment

it's the same nothing is appearing when i use post !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.