【发布时间】: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