【问题标题】:laravel 5.4 eloquent model loading , porting from 4.1 projectlaravel 5.4 eloquent 模型加载,从 4.1 项目移植
【发布时间】:2017-04-02 15:50:04
【问题描述】:

我目前正在将一个使用 laravel 4.1 的旧站点移植到 laravel 5.4。已经实现了很多小改动(错误日志有帮助),但我坚持使用以下模型:

namespace App;


class navire extends Eloquent
{
protected $table = 'navire';

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

public function getId()
{
    return $this->id;
}

}

我正在尝试从控制器调用它:

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Input;

use App\navire;

class UserController extends Controller
{
   public function somefunc()
   {
      // .... 

      $navire = \navire::where('user_id',$user->id)->get()->take(1);


    }

}

尝试了很多使用“use ...”或在开头使用 \ 调用它的东西,到目前为止没有任何效果

错误示例:

local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'App\Http\Controllers\navire' not found' in /home/user/www/app/Http/Controllers/UserController.php:30

local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'App\Http\Controllers\navire' not found' in /home/user/www/app/Http/Controllers/UserController.php:30

local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'App\navire' not found' in /home/user/www/app/Http/Controllers/UserController.php:32

 local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'navire' not found' in /home/user/www/app/Http/Controllers/UserController.php:32

我已经调用了 dumpautoload。

【问题讨论】:

  • 调用时从类中删除 \
  • @LeaTano 不使用 App\navire : 'Class 'App\Http\Controllers\navire' not found , with : 'Class 'App\Eloquent' not found'
  • class navire extends \Eloquent 解决了这个问题。谢谢 :)

标签: php laravel-4 laravel-5.4


【解决方案1】:

要么使用$navire = \App\navire::where('user_id',$user->id)->get()->take(1); 或者因为您已经导入了模型,请使用 $navire = navire::where('user_id',$user->id)->get()->take(1);,即前面没有斜线。

另外,您可以只使用first() 而不是->get()->take(1)。赞这个$navire = \App\navire::where('user_id',$user->id)->first();

【讨论】:

  • 直接使用 navire::where 会导致消息 'Class 'App\navire' not found' 错误。
  • @NicoAD 你是否包含使用 App\navire?
  • 现在我收到消息“未找到类 'Authenticatable'”。每次我解决一个问题时,我都会得到一个新问题。我记得我在 laravel 4 中最喜欢的是干净优雅的自动加载解决方案。现在5.4一团糟..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 2013-06-10
  • 1970-01-01
  • 2018-02-21
  • 1970-01-01
  • 2015-11-15
  • 2011-04-15
相关资源
最近更新 更多