[英]Load multiple models from single controller
need help laravel.需要帮助 laravel。
I have 2 model in different folder我在不同的文件夹中有 2 个 model
there is no problem when i used at UsersController -> call app\User , or RolesController -> call app\model\Role当我在UsersController -> call app\User或RolesController -> call app\model\Role使用时没有问题
but, when i used both models on UsersController , the app\model\Role didnt work但是,当我在UsersController上使用这两个模型时, app\model\Role没有工作
==================== 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 =================== ========================应用\模型\角色===================
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 ======================= ========================应用\用户=======================
<?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 \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Class 'app\model\Role' not found Symfony \ 组件 \ 调试 \ 异常 \ FatalThrowableError (E_ERROR) Class 'app\model\Role' 未找到
Try with capital 'A' on 'App'.尝试在“应用程序”上使用大写字母“A”。 Some systems are case sensitive.有些系统区分大小写。 So use App\Model\Role
depending on your folder name for model.因此,根据 model 的文件夹名称use App\Model\Role
。 IE if your model
folder is lower case, match it in the use
statement. IE如果你的model
文件夹是小写的,在use
语句中匹配。
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
.此外,除非存在冲突,否则您不需要 as 关键字 - 您可以只use App\Model\Role
而不使用as Role
。
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.还有一项,确保角色class 实际上位于 model 文件夹中,并且您的命名空间位于 php 文件的顶部,正确引用了 App\Model 命名空间。 So in your Role class make sure the caps match your use call -因此,在您的角色class 中,请确保上限与您的使用电话相匹配 -
<?php
namespace App\Model
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.