Quick way to Request Json from Angular and return from Laravel

Posted: 2015-07-06 16:03:14

I simply want to make sure to return json from my app more easily even on errors. So in Angular I set my app.js as such

    'use strict';

    angular.module('app', []);

            $http.defaults.headers.common.Accept = 'application/json';

Then in my Laravel routes I can

    public function index(Request $request)
        $blogs = Blog::orderBy('created_at', 'desc')->paginate(5);
        $customPagination = new CustomPagination($blogs);

        if($request->header('Accept') == 'application/json')
            return $blogs;

        return view('blogs.index', compact('blogs', 'customPagination'));

Then I can share the same route with Angular that I do with my Blade templates as needed.