dongxi6897 2017-03-29 01:29 采纳率: 0%
浏览 67
已采纳

Laravel在Eloquent Collection中嵌套ForEach

I'm trying to use a many to many relationship to accomplish the following workflow:

  1. There are many sections and many users. Each user has sections they have unlocked. So for example, if there are two sections (Section I and Section II) the first user, Jim (id = 1), has unlocked Section X, the second user, Debbie (id = 2), has unlocked Section I and II.

  2. To accomplish this, I have three databases, a standard Laravel users, then a sections which stores the section data (id = 1 for Section I and id = 2 for Section II) and then a user_section that I have successfully used as the joint table between the User and Section models. That join table is where users and sections intermingle, if there is an entry for a given user_id in that section, the corresponding section_id is unlocked.

I have the following function which is supposed to 1. Get all the sections for the view and 2. Let me know which of those sections are unlocked by the user.

The problem is I get duplicate Sections showing up, so it will say Section I is unlocked, then Section I is locked all in the same view, this must be in how I am traversing and comparing arrays. With tweaks to the code (where I place a break I can get rid of the duplicates, but then the wrong sections are locked.

My logic is here:

public function getSections(){
    $arrayofuserSections = array();
    $tempArray = array();
    $user = User::where("id",Auth::user()->id)->first();
    foreach ($user->section as $section) {
    $tempArray['Name'] = $section["name"];
    $tempArray['Goals'] = $section["goals"];
    array_push($arrayofuserSections,$tempArray);
}
    $finarray = array();
    $sections=Section::orderBy('order')->get();
    foreach ($sections as $section) {
    foreach($arrayofuserSections as $arraysection){
    if($section->name == $arraysection["Name"])
    {
    $arraysection["Unlocked"] = 1;  
    array_push($finarray,$arraysection);
    }
    else{
    $arraysection["Unlocked"] = 0;
    $arraysection["Name"] = $section->name;
    $arraysection["Goals"] = "";
    array_push($finarray,$arraysection);
    }
    break;
    }
    }
    return $finarray;
}

$user->section is derived from a method on the User model, here:

public function section()
    {
        return $this->belongsToMany('App\Models\Section','user_section')->withTimestamps();
    }

My seeding for the user Debbie is here:

        DB::table('user_section')->insert([
            'user_id' => 2,
            'section_id'=>1
        ]);
        DB::table('user_section')->insert([
            'user_id' => 2,
            'section_id'=>2
        ]);

And yet I get the following result when logged in as Debbie:

So even though Debbie has both sections in the join table, she gets only has one of them unlocked, and again this changes if I remove or move the break around.

Console Log

  • 写回答

1条回答 默认 最新

  • dphfwzn8269 2017-03-29 01:42
    关注

    I think that you're coming at this from the wrong direction.

    If you have set up your relationships correctly, you should be able to access sections from users, and users from sections.

    // Find user with sections they have unlocked
    $user = User::with('sections')->findOrFail(1);
    
    // Find sections with users who have unlocked them
    $section = Section::with('users')->findOrFail(1);
    

    If you approach this problem from the direction of sections, you can do the following:

    // Find current user id
    $userId = Auth::id();
    
    // Find all sections with users who have unlocked them, limited to only the current user
    $sections = Section::with([
            'users' => function ($query) use ($userId) {
                return $query->where('id', $userId);
            }
        ])->get();
    

    This will give you all sections, and eager load the users relation, where that is the current user. Thus, if the users relationship is empty, the current user hasn't unlocked that section.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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