Wednesday, March 29, 2017

Setting value comparing two arrays in Laravel

I have a function that compares 2 arrays. One array is filled with a group of users. The other array comes from a formular. It contains a list of IDs.

If an ID matches any ID from the formular, the user should be saved as "released". If not it should be not matched as released.

My formular works if I check them, but when they are unchecked and therefore not in the array, they are still saved as released. Here is my code:

    public function save_user_list(Requests $request)

    {
        $user_id_list = array();
        $counter = 0;

        if(Auth::user()->role == "owner"){
        $users = User::where('owner_id',Auth::user()->owner_id)
            ->get();
        }else{
            $users = User::where('owner_id',Auth::user()->owner_id)
                ->where('role',"user")
                ->get();
        }
        foreach($users as $user)
        {
            $user_id_list[$counter] = $user->id;
            $counter++;
        }

     for($i=0;$i<count($request->release);$i++)
        {
            $release = 0;
            for($a=0;$a<$counter;$a++)
            {
                if($request->release[$i] == $user_id_list[$a])
                {
                    $release = 1;
                }
            }
            $userrelease = User::where('id',$request->release[$i])
                ->where('owner_id',Auth::user()->owner_id)
                ->first();
            if($userrelease)
            {
            $userrelease->active = $release;
            $userrelease->save();
            }
        }

        return back();



via prgrm

Advertisement