从单个控制器加载多个模型 [英] Load multiple models from single controller
问题描述
需要帮助laravel.
我在不同的文件夹中有2个模型
need help laravel.
I have 2 model in different folder
- app \ User
- app \ model \ Role
当我在 UsersController ->调用 app \ User 或 RolesController ->调用 app \模型\角色
there is no problem when i used at UsersController -> call app\User, or RolesController -> call app\model\Role
但是,当我在 UsersController 上同时使用两个模型时, app \ model \ Role 无法正常工作
but, when i used both models on UsersController , the app\model\Role didnt work
==================== UsersController =====================
==================== UsersController ======================
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use app\User;
use app\model\Role as Role;
use DataTables;
class UserController extends Controller
{
public function index(Request $request){
$data['title_page'] = 'User';
$data['roles'] = Role::all(); // this line show error
return view('admin/user', $data);
}
}
======================== app \ model \ Role =================== =
======================== app\model\Role ===================
namespace App\model;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $fillable = ['id','name'];
protected $tables = 'roles';
public function users(){
return $this->belongsTo('App\User','role');
}
}
======================= app \ User ===================== ===
======================== app\User =======================
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = "users";
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','role', 'status'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function roles(){
return $this->hasOne('App\model\Role','id');
}
}
Symfony \组件\调试\异常\ FatalThrowableError(E_ERROR) 找不到"app \ model \ Role"类
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Class 'app\model\Role' not found
推荐答案
在应用程序"上尝试使用大写字母"A".某些系统区分大小写.因此,use App\Model\Role
取决于您的型号的文件夹名称. IE(如果您的model
文件夹是小写字母),请在use
语句中将其匹配.
Try with capital 'A' on 'App'. Some systems are case sensitive. So use App\Model\Role
depending on your folder name for model. IE if your model
folder is lower case, match it in the use
statement.
此外,除非有冲突,否则这里不需要as关键字-仅使用use App\Model\Role
而没有as Role
可能会很好.
Also, you don't need the as keyword here unless there are conflicts - you may be fine with just use App\Model\Role
without the as Role
.
还有一项,请确保 Role 类实际上位于模型文件夹中,并且您的命名空间正确地引用了App \ Model命名空间,位于php文件的顶部.因此,在您的角色类中,请确保大小写与您的使用要求相符-
One more item, make sure the Role class is actually in the model folder and that your namespace is at the top of the php file correctly referencing the App\Model namespace. So in your Role class make sure the caps match your use call -
<?php
namespace App\Model
这篇关于从单个控制器加载多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!