2

I have a RESTful controller, that I'm trying to get my ajax to get to, and I keep getting an internal sever error 500, controller method not found.

In the routes.php I have:

Route::controller('friends', 'FriendController');

In the controller I have:

class FriendController extends Controller{
    public function postAdd(){
        $user_id = Auth::user()->id;
        $friend_id = Input::get('id');
        $friend = new Friend;
        $friend->user_id = $user_id;
        $friend->friend_id = $friend_id;
        if($friend->save()){
            return Response::json(array('status' => 'OK'));
        }else{
            return Response::json(array('status' => 'FAIL'));
        }
    }
}

and the ajax is:

$.ajax({
                url : "{{URL::action('FriendController@postAdd')}}",
                type : "POST",
                data : { id : $(this).attr('val')}
            })
            .done(function(json){
                console.log(json);
            });

resulting in http://localhost/velser/friends/Add for the URL.

Any idea where I'm going wrong? And if you see any other relevant problems with the code, let me know please! I'm still getting used to laravel 4, so I still have many issues I believe.

EDIT: Includin the Friend model now too:

class Friend extends Eloquent{
    protected $table = 'friends';
}
Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
samuraiseoul
  • 2,888
  • 9
  • 45
  • 65
  • looks like you have to add manually `Route::post('friends/Add', 'FriendController@postAdd');`, because by default, laravel creates only route to friends@store via POST request for RESTful controller. Execute `php artisan routes` to see all your routes created. Check also related topic http://stackoverflow.com/questions/18402298/laravel-4-defining-restful-controllers – Alexandr Perfilov Sep 29 '13 at 03:06

2 Answers2

1

There is no mistakes in your code, tested with latest laravel update. But you need to update your Laravel Framework. So run,

composer update

Or,

php composer.phar update

And make sure you don't have a controller with the same class name of model like Friend. Then make sure you have created_at and updated_at fields in your table. If you don't want to use this fields, just add this line to your model,

public $timestamps = false;
Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
0

The issue was hard to find, but once found an easy fix. Had I wrapped the $friend->save() in a try catch block earlier I would have found it very fast. I didn't have the updated_at and created_at columns in the table. But the laravel error page didn't tell me this and just failed. In future times, I will wrap things in a try catch block to find the answer.

samuraiseoul
  • 2,888
  • 9
  • 45
  • 65