4

I am new to laravel and I want to make an ajax call to a function written in controller. I have done the following but not working.

In View :

$.ajax({
    type: "POST",
    url: 'OrderData', // Not sure what to add as URL here
    data: { id: 7 }
}).done(function( msg ) {
    alert( msg );
});

My Controller which is located inside app/controllers/DashBoardController.php and inside DashBoardController.php I have

class DashBoardController extends BaseController {
    public function DashView(){
        return View::make('dashboard');
    }

    public function OrderData(){ // This is the function which I want to call from ajax
        return "I am in";
    }
}

My Question is how can I make an ajax call from view on page load to a function inside my DashBoardController.php ?? Thanks.

Yunus Aslam
  • 2,447
  • 4
  • 25
  • 39
  • For anyone else that comes across this needing help, I have added an expanded tutorial as a [post on my website](http://thingsaker.com/blog/ajax-laravel-controller-method) - hope its useful. – DavidT Oct 10 '15 at 23:28

1 Answers1

14

In your routes.php file add

Route::post('/orderdata', 'DashBoardController@OrderData');

Then use your ajax call to send data to /orderdata the data will be passed through to your OrderData method in the DashBoardController

So your ajax call would become

$.ajax({
    type: "POST",
    url: '/orderdata', // This is what I have updated
    data: { id: 7 }
}).done(function( msg ) {
    alert( msg );
});

If you want to access the data you will need to add that into your method like so

class DashBoardController extends BaseController {
    public function DashView(){
        return View::make('dashboard');
    }

    public function OrderData($postData){ // This is the function which I want to call from ajax
        //do something awesome with that post data 
        return "I am in";
    }
}

And update your route to

Route::post('/orderdata/{postdata}', 'DashBoardController@OrderData')
DavidT
  • 2,341
  • 22
  • 30
  • I did this and I am getting `404 Not Found` .. I tried this before posting this question. – Yunus Aslam Sep 24 '14 at 11:08
  • Your answer did help me to find out my issue. In the ajax call you have added `url : '/orderdata'` .. Remove the slash. I did a very silly mistake though. In Routes.php I used get instead of post. – Yunus Aslam Sep 24 '14 at 11:10
  • Ah ok, well glad it helped you out. I used `/` assuming your going from root. Update accordingly for your URL. – DavidT Sep 24 '14 at 11:11
  • Also Can you tell me how to create a simple modal and fetch the data from database.. Any link will be useful.. thanks – Yunus Aslam Sep 24 '14 at 11:11
  • Sure, I cant really describe it in the comments but if you havent already check out [Laracasts](https://laracasts.com) – DavidT Sep 24 '14 at 11:12
  • I will definetely go through Laracast but I think it will take some time though. Can you just give me a small example of modal and how to fetch data from db. – Yunus Aslam Sep 24 '14 at 11:16
  • Ok, couple of things to remember about laravel models. If your DB table is called `projects` your model will be `Project.php`, then use the [Eloquent ORM](http://laravel.com/docs/4.2/eloquent) to do database calls like `Project::get()` which will get all of your projects. Thats really all I can do in comments, read the Eloquent documentation its pretty clear and very powerful. Have a go, if you get stuck ask a question on SO, I watch the laravel tags if i see it I will give you a helping hand. – DavidT Sep 24 '14 at 11:21