【发布时间】:2018-08-02 03:34:17
【问题描述】:
我正在为我的 laravel 项目中的用户处理访问和权限我想显示模块的功能,以便我可以将它们添加到某个角色这里是我的数据库:
public function up()
{
Schema::create('role_modules', function (Blueprint $table)
{
$table->increments('id');
$table->integer('rang');
$table->string('title');
$table->string('route');
$table->text('description');
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('role_modules');
}
public function up()
{
Schema::create('role_functions', function (Blueprint $table)
{
$table->increments('id');
$table->integer('module_id')->unsigned();
$table->string('title');
$table->string('function');
$table->softDeletes();
$table->timestamps();
$table->foreign('module_id')->references('id')->on('role_modules')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('role_functions');
}
这是我的模型代码:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Rolemodules extends Model
{
protected $table = 'role_modules';
public function func()
{
return $this->hasMany('App\Rolefunctions','module_id');
}
}
当我想在我的视图中显示每个模块的功能时:
@foreach($rolemodules as $rolemodule)
{{$rolemodule->func['title']}}
@endforeach
我收到此错误:未定义索引:标题
【问题讨论】:
-
你需要预先加载 eloquent 模型对象。
-
这是什么意思?
标签: laravel