【问题标题】:many to many laravel get values多对多 laravel 获取值
【发布时间】:2018-01-27 20:24:04
【问题描述】:

在用户类模型中,我有这个

    public function projects()
{
    return $this->belongsToMany('App\Project');
}

在项目类模型中我有这个:

    public function users(){
    return $this->belongsToMany('App\User');
}

在我的 projectController 我有这个:

    public function index()
{
    $TeamLeader = array();
    $posts = Project::orderby('id', 'desc')->with('users')->paginate(5); 
    return view('Projects.index', compact('posts'));
}

在我看来,每个人都有这个:

                    @foreach ($posts as $post)
                    <tr>
                        <td><input type="checkbox" class="checkthis" /></td>
                        <td href="{{ route('Projects.show', $post->project_id ) }}">{{ $post->project_id }}</td>
                        <td>{{ $post->name }}</td>
                        <td>{{ $post->description }}</td>
                        <td>{{ $post->created_at }}</td>
                        <td>{{ $user->name}}</td>
                        <td><p data-placement="top" data-toggle="tooltip" title="Show"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#show" ><span class="glyphicon glyphicon-pencil"></span></button></p></td>
                        <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></button></p></td>
                    </tr>
                @endforeach

我的问题是,在我看来,我无法获取用户名,关系是多对多但我没有获取参与项目的用户名,数据库结构如您所想:

用户有 id , name , ......

项目有 id , name , location , user_id ,.....

很抱歉之前没有提到,但我也有这个公用表:

        Schema::create('project_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('project_id')->unsigned();
        $table->integer('user_id')->unsigned();

        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('project_id')->references('id')->on('projects');

        $table->timestamps();
    });

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    其实你的表设计不是多对多,而是一对多[用户有很多项目] 如果你想建立多对多关系必须做一个中间表(数据透视表)

    看看this
    This is helpful


    将此添加到您的迁移中

    Schema::create('project_user', function(Blueprint $table)
      {
          $table->integer('user_id')->unsigned()->nullable();
          $table->foreign('user_id')->references('id')
                ->on('users')->onDelete('cascade');
    
          $table->integer('project_id')->unsigned()->nullable();
          $table->foreign('project_id')->references('id')
                ->on('projects')->onDelete('cascade');
    
          $table->timestamps();
      });
    

    project_user 表是根据相关模型名称的字母顺序得出的,包含 user_id 和 project_id 列
    然后从您的项目表中删除 user_id 列

    终于可以访问用户名了

    $project->users[0]->name
    $project->users[0]->name
    

    【讨论】:

    • 我更新了我的代码,我仍然不明白在哪里使用这一行:Table => pivot_users_projects [user_id, project_id] ????查询项目表时如何获取用户名??
    • "Table => pivot_users_projects [user_id, project_id]" 这不是代码,我的意思是你必须创建一个数据透视表。 $project->users 是一个用户数组
    猜你喜欢
    • 1970-01-01
    • 2020-04-29
    • 2014-10-14
    • 1970-01-01
    • 2015-04-05
    • 2016-03-29
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    相关资源
    最近更新 更多