Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5数据透视表项目<-&燃气轮机;用户_Php_Mysql_Laravel_Pivot - Fatal编程技术网

Php Laravel 5数据透视表项目<-&燃气轮机;用户

Php Laravel 5数据透视表项目<-&燃气轮机;用户,php,mysql,laravel,pivot,Php,Mysql,Laravel,Pivot,我发现pivot表非常复杂,我不知道下一步该做什么,也不知道我做错了什么,我已经找到了一些教程,但在我的需要方面没有帮助 我有一个项目和用户,具有多对多关系。 一个项目有多个用户,一个用户有多个项目 我现在所做的是让项目与用户没有关系 这就是我到目前为止所做的: 项目表 class CreateProjectsTable extends Migration { public function up() { Schema::create('projects', function(Blue

我发现pivot表非常复杂,我不知道下一步该做什么,也不知道我做错了什么,我已经找到了一些教程,但在我的需要方面没有帮助

我有一个
项目
用户
,具有
多对多
关系。 一个
项目有多个用户
,一个
用户有多个项目

我现在所做的是让项目与用户没有关系

这就是我到目前为止所做的:

项目表

class CreateProjectsTable extends Migration {

public function up()
{
    Schema::create('projects', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->date('completion_date');
        $table->integer('completed')->default(0);
        $table->integer('active')->default(0);
        $table->timestamps();
    });
}
class CreateUsersTable extends Migration {

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('company_id');
        $table->integer('project_id');
        $table->integer('usertype_id')->default(0);
        $table->string('username');
        $table->string('password');
    });
}
用户表

class CreateProjectsTable extends Migration {

public function up()
{
    Schema::create('projects', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->date('completion_date');
        $table->integer('completed')->default(0);
        $table->integer('active')->default(0);
        $table->timestamps();
    });
}
class CreateUsersTable extends Migration {

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('company_id');
        $table->integer('project_id');
        $table->integer('usertype_id')->default(0);
        $table->string('username');
        $table->string('password');
    });
}
项目用户表(透视图)

用户模型

public function projects() {
    return $this->belongsToMany('App\Project', 'project_users', 'user_id', 'project_id');
}
public function users() {
    return $this->belongsToMany('App\User', 'project_users', 'project_id', 'user_id');
}
项目模型

public function projects() {
    return $this->belongsToMany('App\Project', 'project_users', 'user_id', 'project_id');
}
public function users() {
    return $this->belongsToMany('App\User', 'project_users', 'project_id', 'user_id');
}
项目控制员

public function index(Project $project)
{

   $projects = $project->with('users')->get();

    dd($projects);

    $currenttime = Carbon::now();

    //return view('project.index', array('projects' => $projects, 'currenttime' => $currenttime));

    return view('user.index', compact('projects'));
}

用户
模型中的关系不正确。你必须交换钥匙

public function projects() {
    return $this->belongsToMany('App\Project', 'project_users', 'user_id', 'project_id');
}
编辑最新评论:

不要考虑数据透视表,只要您的关系设置正确(我相信它们是正确的),Laravel就会为您处理所有这些

现在,
$projects->users
没有任何意义,因为
项目
没有
用户
<代码>项目只是
项目
的集合。该集合中的每个
项目
将具有
用户
关系。您必须遍历集合才能查看每个
项目的用户

foreach($projects as $project) {
    foreach($project->users as $user) {
        echo $user;
    }
}

只是好奇:为什么“project_users”表有自己的增量ID?在我的脑海中,我发现pivot表应该是这样的:(project_users_ID=1,project_ID=5,user_ID=8)(例如)如果我错了,请纠正我。我意识到这是可能的:ID 1,project_ID:2,user_ID:3,ID:2,project_ID:2,user_ID:3?两次相同的值(同一个用户与同一个项目相关两次)这实际上取决于您试图用透视表完成什么。如果您不希望重复(同一用户拥有同一个项目两次),那么最好使用组合主键
project\u id
user\u id
,以防止重复发生在数据库级别,而不需要
id
,因为它变得不必要。我已经发现,但它仍然会让我得到空虚的关系。至于我所知道的,它应该返回(当数据库被填充时)项目中的dd($projects)关系,对吗?它应该,而不是
dd($projects)尝试
dd($projects->toArray())关系属性在集合中的嵌套可能太深,转储时无法显示。0=>数组:8[▼     “id”=>“1”“name”=>“project”“users”=>[]仍然返回一个空的users值,而此项目实际上有一个用户(具有此project\u id的用户),这很奇怪。我将在本地进行设置,看看会发生什么。它现在似乎可以工作了,可能是某个地方的输入错误,非常感谢您的帮助。