laravel 5.4 eloquent 模型加载,从 4.1 项目移植

laravel 5.4 eloquent model loading , porting from 4.1 project

我目前正在将使用 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。

要么使用 $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();