doulang9953 2015-03-19 14:21
浏览 29
已采纳

如何在数据透视表Laravel中使用其他字段

I have two tables in relation users and projects. In users table I have addiotional field admin_id.

Pivot table is project_user with columns: id, user_id, project_id and additonal field admin_id

When I selecting projects from database I need also to check if admin_id from pivot table is equal with admin_id from users table is it possible???

Here is my User modal:

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

Here is Controller:

 public function project(Project $project){
            //TAKING PROJECTS FORM CURRENT ID
            $projects = User::findOrFail(Auth::user() -> id) ;
            //NOT IMPORTANTE
            $users = User::lists('name','id');
            return view('projects',array('projects' => $projects,'users' => $users ));
        }

Blade:

@foreach($projects -> projects as $project)
<a href="#" class="list-group-item">{{ $project -> name }}</a>
@endforeach
  • 写回答

2条回答 默认 最新

  • dougong2005 2015-03-19 15:40
    关注

    I found way....

    This linke:

    $projects = User::findOrFail(Auth::user() -> id) ;
    

    Should be changed with this:

    $projects = User::findOrFail(Auth::user() -> id)->projects()->where('admin_id', '=', $this->id)->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?