2

This is more of a concept question, so I apologize if it isn't specific enough.

I am coming from a Jquery/AngularJS background; usually I am doing front-end stuff and only work with a back-end sparingly.

I am trying to learn Laravel 5 to expand my skills, but am having trouble conceptually fitting together what I know from Angular with what Laravel is telling me.

I want CRUD functionality to a database using Angular, but I want Laravel to help me get that database from MySQL to JSON so it can be passed.

What I have done is made the following in Laravel:

~Model:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Pun extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */

     public $timestamps = false;
}

~Controller:

namespace App\Http\Controllers;

use App\Pun;
use App\Http\Controllers\Controller;

class PunController extends Controller
{
    /**
     * Show a list of all available puns.
     *
     * @return Response
     */
    public function index()
    {
        $puns = Pun::all();

        return Response::json($puns->toArray());
    }
}
}

~Route:

Route::get('showpunsfromdatabase', function () {
    return view('???');
});

The question marks above is where I am having trouble. If I understand it correctly, the controller is supposed to query the database as defined in the model, and output the results as JSON. How do I then direct Angular to this controller so I can get this JSON using the $http service? Does it have to go in a view that then pulls it in? I don't really want it in a "view", I just want the data (JSON) available to Angular. Am I thinking about this correctly?

My angular controller looks like this:

$scope.punGenerate = function(){
    $http.get("???").then(function (response) {$scope.thePunJSON = response.data;});
}

1 Answer 1

1

IF you do this:

Route::get('showpunsfromdatabase', function () {
    return view('???');
});

You are not calling the controller, you are returning the view directly. To call the controller your route should look like this:

Route::get('showpunsfromdatabase' ,  [
    'as' => 'showpunsfromdatabase.index',
    'uses' => 'PunController@index'
]);

Now, from the controller you should load the view including the info returned by the model:

<?php

namespace App\Http\Controllers;

use App\Pun;
use App\Http\Controllers\Controller;

class PunController extends Controller
{
    /**
     * Show a list of all available puns.
     *
     * @return Response
     */
    public function index()
    {
        $puns = Pun::all();

        return view('???')
           ->with(['puns' => json_encode($puns)]);
    }
}

It's just an example, because I don't know what are you looking for exactly.

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

5 Comments

Should the view in the controller that I am returning match the named route? showpunsfromdatabase.index?
Also sorry, where is @index coming from in the route?
No, is just an alias. For example if you create a FORM, you can do: {!! Form::open(['route' => 'showpunsfromdatabase.index']) !!}
@index is the name of the action to be call in the controller. So it's like: controller@action
So the view from the controller can just echo out the json for Angular, is this correct? Can you add what that view would look like in your answer?

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.