1

Am just starting out with laravel and i wanted to create a simple rest api that accepts json data and returns a json data but didnt seem to know how to do this. I was wondering if anyone could help. below is what i want my controller to look like or do:

class MembersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //return all members as json
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //leave this as empty
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // get user values as json
        // save value to database
       // return user data as json.
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //return single user dat as json
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //update value and return as json
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //delete value and also return deleted value as json
    }
}

How can i achieve the above using laravel. Mostly 5.5 or 5.4. I dont seem to find anything that could be of help anywhere.

1
  • you can store data in array and then make use of json_encode () Commented Sep 8, 2017 at 4:22

1 Answer 1

1

This problem is not too hard; you can get the solution of such problem easily over the internet.

Anyway, you can accomplish this by using array and json_decode like below

 $data = ['staus' => 'ok',
          'id' => 10,
          'name'=>'something',
          'address'=>'something'
          ];
// encode this array to json strig you can use return or echo, print to generate the result
 return json_encode($data);

If you want to convert the model objects to json then you can simply use toJson function like below which comes with laravel framework

   return ModelName::all()->toJson();
   // for a single record retrieval
   return ModelName::find(1)->toJson();
Sign up to request clarification or add additional context in comments.

6 Comments

Doesn't work when I do a post request using postman, it returns html containing several error messages mostly saying : TokenMismatchException in VerifyCsrfToken.php line 68
token mismatch is problem of laravel, if you post form you need to send input with the name _token which is generated by laravel itself for security reason to prevent - Cross-Site Request Forgery(CSRF)
so how do I handle this, because I'm going to be calling my php rest from another server
you can make use of GET method to submit and token system to verify the request from authenticated users only
or you may disable the csrf look into laracasts.com/discuss/channels/general-discussion/…
|

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.