BadMethodCallException方法eloquent不存在

时间:2015-01-27 15:34:09

标签: php laravel eloquent

LARAVEL中的当前项目收到此错误:

BadMethodCallException Method eloquent does not exist.    

我的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');

}

和我的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);
        }
    }
}

return Response::eloquent(Actividades::all());

中出现错误

我尝试重建整个项目,使用区分大小写,但方法“laravel”似乎并不像apear。

需要帮助!

1 个答案:

答案 0 :(得分:1)

Response::eloquent()根本不存在。这就是错误所说的,这就是问题所在。 OP的含义(见注释)是Response::json(),它将传递的变量转换为JSON:

return Response::json(Actividades::all());

作为一点奖励:确实有可能有类似Response::eloquent()的东西。 Laravel的Response Macros允许您添加自定义快捷方式以生成响应。这是你注册的方式:

Response::macro('eloquent', function($value)
{
    return Response::json($value);
});

用法:(完全和你一样)

return Response::eloquent(Actividades::all());

显然,将它传递给Response::json()没有多大意义。通常你会改变价值或完全做其他事情。