laravel 5.1未找到模型

时间:2015-08-22 11:53:49

标签: php laravel relationship laravel-5.1

我花了4个小时来解决这个问题,但我无法解决这个问题。 我正在使用laravel 5.1

我希望查看组织的所有者或其他方式,查看用户所属的组织。

我从网络服务器收到以下错误:

FatalErrorException in Model.php line 893:
Class 'app\models\Organisation' not found
in Model.php line 893
at FatalErrorException->__construct() in HandleExceptions.php line 133
at HandleExceptions->fatalExceptionFromError() in HandleExceptions.php line 118
at HandleExceptions->handleShutdown() in HandleExceptions.php line 0
at Model->hasMany() in User.php line 19
at User->owner_of() in TestController.php line 24
at TestController->index() in Controller.php line 256
at call_user_func_array:{/home/shifts/public_html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:256}() in Controller.php line 256
at Controller->callAction() in ControllerDispatcher.php line 164
at ControllerDispatcher->call() in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}() in Pipeline.php line 139

我的文件夹结构如下:

- App/
-- Models/
--- Organisation.php
--- User.php

用户模型

<?php

namespace app\models;

use Illuminate\Database\Eloquent\Model;

class User extends Model {

    protected $table = 'users';
    public $timestamps = true;

    public function owner_of()
    {
        return $this->hasMany('Organisation', 'owner_id');
    }    
}

组织模式

<?php

namespace app\models;

use Illuminate\Database\Eloquent\Model;

class Organisation extends Model {

    protected $table = 'organisations';
    public $timestamps = true;

    public function owner()
    {
        return $this->belongsTo('App\Models\User','owner_id','id');
    }
}

测试控制器(代码I&#39;正在执行)

<?php 

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Organisation;
use View;
use Debugbar;

class TestController extends Controller {

  /**
   * Display a listing of the resource.
   *
   * @return Response
   */
  public function index()
  {
    $site = url('test');
    echo "testpage <a href=\"$site\">Reload</a>";
    $u = User::find(1);
    var_dump($u);
    echo "<br /><br />";
    var_dump($u->owner_of());
    echo "<br /><br />";
    $o = Organisation::find(1);
    echo "<br /><br />";
    var_dump($o);
    echo "<br /><br />";

    var_dump($o->owner());
  }
}

用户计划

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UsersAccess extends Migration
{
    public function up()
    {
        Schema::create('users_roles', function(Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('ex_id');
            $table->string('obj',3);
            $table->integer('granted');
            $table->timestamps();
        });
    }
}

组织计划

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateOrganisationsTable extends Migration {

    public function up()
    {
        Schema::create('organisations', function(Blueprint $table) {
            $table->increments('id');
            $table->integer('owner_id');
            $table->string('name');
            $table->text('description');
            $table->timestamps();
            $table->softDeletes();
        });
    }
}

有人能指出我做错了什么吗?我盯着这个问题视而不见。 (这是我第一次尝试在laravel 5.x中创建一个项目)

2 个答案:

答案 0 :(得分:3)

即使它们共享命名空间,您仍然需要提供关系定义的完全限定路径:

public function owner_of()
{
    return $this->hasMany('App\Models\Organisation', 'owner_id');
}

答案 1 :(得分:0)

我可以在您的表格架构中看到您的表格名称为users_roles,但在您的模型中,您使用了protected $table = 'users';

将您的表名更改为users或更改模型中的表名protected $table = 'user_roles';