Reputation: 59
Current project in LARAVEL get this error:
BadMethodCallException Method eloquent does not exist.
my routes.php:
Route::any('act', array('as' => 'ApiActividadesController', 'uses' => 'ApiActividadesController@get_index'));
my model.php (Actividades.php)
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class Actividades extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'actividades';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
}
and my controller.php (ApiActividadesController.php)
class ApiActividadesController extends BaseController {
public $restful = true;
public function get_index($id = null)
{
if (is_null($id ))
{
return Response::eloquent(Actividades::all());
}
else
{
$actividades = Actividades::find($id);
if(is_null($actividades)){
return Response::json('Actividades not found', 404);
} else {
return Response::eloquent($actividades);
}
}
}
The error throws in the return Response::eloquent(Actividades::all());
I've try to rebuild entire project, use case sensitive but method "laravel" doesn't seems to apear.
need help!
Upvotes: 1
Views: 2626
Reputation: 153140
Response::eloquent()
simply does not exist. That's what the error says and that's the problem here. What the OP meant (see comments) was Response::json()
which converts the passed variable to JSON:
return Response::json(Actividades::all());
As a little bonus: It is indeed possible to have something like Response::eloquent()
. Laravel's Response Macros allow you to add a custom shortcut to generate responses. This is how you register them:
Response::macro('eloquent', function($value)
{
return Response::json($value);
});
And the usage: (exactly like you had it)
return Response::eloquent(Actividades::all());
Obviously it makes not much sense to just pass it on to Response::json()
. Normally you would change the value or do something else entirely.
Upvotes: 1