0

When entering the url, domain.com/floorplans, I would like the FloorplanController.php to return all floorplans to an angular controller and also return the view.

Here is FloorplanController@index:

public function index()
    {
        $floorplans = Floorplan::all();
        return view('floorplans.index', compact('floorplans'));
    }

Here is my Angular controller:

angular
.module('myApp', [])

.controller('FloorplanController', function($scope, $http) {

    $http.get('/floorplan').success(function(floorplans) {
        $scope.floorplans = floorplans;
    });

});

The above doesn't work as $scope.floorplans = floorplans; is always undefined.

If I create two separate routes in my routes.php file, I can get it to work, but that seems very sloppy.

Route::get('/', function()
{

    return view('floorplans.index');

});

Route::get('test', function()
{

    return Floorplan::all();

});

What is the proper syntax to return a view with an object so that the object is picked up by my Angular controller?

2 Answers 2

0

You can try this code, it work for me now:

public function index()
{
    $data['floorplans'] = Floorplan::all();
    return view('floorplans.index')-> with($data);
}

Then in your view, use: $floorplans (php) or {{$floorplans}} (html)

Get value from column-name: $floorplans['column-name']

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

7 Comments

I just tried this... no luck. There has to be a well known way to pass the view with the object so that the Angular controller can pick off the object. Why am I not seeing it?
The code above to pass data to php or html, if you want your angular get this data, take a look here: stackoverflow.com/questions/29246298/…
<?php $value = "Hello"; $bob = "Bob"; <script type="text/javascript"> myData = { blah: $value, alice: $bob }; </script> ?>
Your Angular: .controller('FloorplanController', function($scope, $http) { console.log(myData); });
Thank you for your effort, rome. I decided not to use Angular in this instance.
|
0

I was facing same issue but I resolved it in one of my projects. This reply mightbe too late but just want to share my experience. Here you can do as stated below.

Here is your Route :

Route::get('/', floorplanController@index);
Route::get('/floorplan', floorplanController@floorplan);

Here is your controller :

public function index(){
 return view('index');
}

public function floorplan(){
return response()->json(Floorplan:get());
}

You can keep your angular controller as it is. But its better to use separate service page and your main application module defined in separate page, which will be helpful when your application grows.

Comments

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.