【问题标题】:Call to undefined method Tipo::orderBy() Laravel 4调用未定义的方法 Tipo::orderBy() Laravel 4
【发布时间】:2014-07-17 12:11:48
【问题描述】:

我创建了一个带有模型和控制器的新迁移(称为“tipo”)。而且我还创建了它的视图......我所做的与我之前所做的完全一样。事实上,我已经复制并粘贴了大部分代码。其他一切都完美无缺。

但是当我想访问我的“tipo/index”时,我收到了下一条消息:

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)

Call to undefined method Tipo::orderBy()

我已经阅读了一些可能的解决方案,并且我已经更新了我的控制器。我也有同样的错误...... Argggg!!!

我的控制器:TipoController.php

class TipoController extends AdminController {

    /**
     * Display a listing of the resource.
     * GET /tipo
     *
     * @return Response
     */
    public function index()
    {
        $tipos = Tipo::orderBy('created_at', 'DESC')->paginate(10);
        return View::make('tipos.index')->with('tipos', $tipos);
    }

    /**
     * Show the form for creating a new resource.
     * GET /tipo/create
     *
     * @return Response
     */
    public function create()
    {
        return View::make('tipos.create');
    }

    /**
     * Store a newly created resource in storage.
     * POST /tipo
     *
     * @return Response
     */
    public function store()
    {
        $input = Input::all();
        // var_dump($input);

        $v = Validator::make($input, Tipo::$rules);

        if ($v->passes()) {

            $tipo = new tipo;
            $tipo->nombre = Input::get('nombre');
            $tipo->descripcion = Input::get('descripcion');
            $tipo->m_desc = Input::get('m_desc');
            $tipo->slug = Str::slug(Input::get('nombre'));
            $tipo->user_id = Sentry::getUser()->id;
            $tipo->save();

            // return $tipo->user_id;

            return Redirect::route('tipos.index');
        }

        return Redirect::back()->withErrors($v);
    }

    /**
     * Display the specified resource.
     * GET /tipo/{id}
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $tipo = Tipo::where('slug', $id)->first();

        $date = $tipo->created_at;
        setlocale(LC_TIME, 'Europe/Madrid');
        $date = $date->formatlocalized('%A %d %B %Y');

        return View::make('tipos.show')->with('tipo', $tipo)->with('date', $date);
    }

    public function myShow($id, $slug)
    {
        $tipo = Tipo::where('id', $id)->first();

        $date = $tipo->created_at;
        setlocale(LC_TIME, 'Europe/Madrid');
        $date = $date->formatlocalized('%A %d %B %Y');

        return View::make('tipos.show')->with('tipo', $tipo)->with('date', $date);
    }

    /**
     * Show the form for editing the specified resource.
     * GET /tipo/{id}/edit
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $tipo = Tipo::where('id', $id)->first();

        $date = $tipo->created_at;
        setlocale(LC_TIME, 'Europe/Madrid');
        $date = $date->formatlocalized('%A %d %B %Y');

        return View::make('tipos.edit')->with('tipo', $tipo)->with('date', $date);
    }

    /**
     * Update the specified resource in storage.
     * PUT /tipo/{id}
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $input = array_except(Input::all(), '_method');

        $v = Validator::make($input, Tipo::$rules);

        if($v->passes())
        {
            Tipo::find($id)->update($input);
            return Redirect::route('tipos.index');
        }

        return Redirect::back()->withErrors($v);
    }

    /**
     * Remove the specified resource from storage.
     * DELETE /tipo/{id}
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        Tipo::find($id)->delete();

        return Redirect::route('tipos.index');
    }

}

还有我的模型:Tipo.php

class Tipo extends Eloquent {
    protected $guarded = array();

    public static $rules = array(
        'nombre' => 'required',
        );

    public function user()
    {
        return $this->belongsTo('User', 'user_id');
    }
}

【问题讨论】:

  • 请问您可以发布您的型号代码吗?
  • 'required', ); public function user() { return $this->belongsTo('User', 'user_id'); } }
  • 嗯...您的模型看起来不错,我可以看看您的控制器吗?请您通过pastebin.com 发帖以方便阅读?
  • 对不起...我不知道它是如何工作的,但这是您可以看到代码的网址:pastebin.com/0y1MyeMc
  • @JaviZu 看看这个:php.net/manual/en/class.reflectionclass.php 它可以让你这样做:$ref = new ReflectionClass('Tipo'); $ref->getFileName(); // path to the class file 和更多有用的东西。

标签: php laravel laravel-4


【解决方案1】:

如果你没有使用命名空间(你说你没有)并且你有两个同名的类 - 那是你的问题。

所以在一个地方你有

class Tipo {}

这是您的迁移。在另一个地方你有

class Tipo extends Eloquent {}

这是你的模型。

但这两个类都称为Tipo

所以稍后,当你调用 Tipo::all() - Laravel 不知道要调用哪个类,因为你有 2x Tipo

您可以通过使用命名空间或将迁移重命名为 TipoMigration 来解决此问题。

【讨论】:

  • Shift Exchange 的解决方案运行良好……但最后我发现了我的错误。我用单数创建了我的迁移(php artisan migrate:make tipo),我应该写 tipos。我已经改变了,我也解决了这个问题。没有重命名我的班级。再次感谢 The Shift Exchange 和 Deczo 的帮助!!
猜你喜欢
  • 2015-05-31
  • 2014-11-16
  • 2014-02-12
  • 2013-10-23
  • 1970-01-01
  • 2014-05-13
  • 2023-03-21
  • 2016-06-05
  • 2016-09-03
相关资源
最近更新 更多