【问题标题】:laravel 5.1 Model not foundlaravel 5.1 模型未找到
【发布时间】:2015-11-16 07:29:48
【问题描述】:

我花了大约 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');
    }
}

测试控制器(我正在执行的代码)

<?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 中做项目)

【问题讨论】:

  • 尝试运行composer dump-autoload,看看能不能解决问题。
  • 我刚试过了,可惜没解决

标签: php laravel relationship laravel-5.1


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 嘿乔尔,谢谢我完全错过的那个。尽管如此,我收到以下错误。 QueryException in Connection.php line 636: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.owner_id' in 'where clause' (SQL: select * from users` where users.owner_id = 1) ` 我也稍微改变了我的TestController 现在我在这条线上遇到了这个错误:Debugbar::info($o-&gt;owner()-&gt;get());
    猜你喜欢
    • 1970-01-01
    • 2016-01-14
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多