MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/www/ezwork/app/Http/Controllers/UserSoftwareToolController.php
<?php

/**
 * UserSoftwareToolController class file
 *
 * PHP Version 7.2
 *
 * @category Controller
 * @package  Ez
 * @author   Imenso Software <admin@imensosoftware.com>
 * @license  http://imensosoftware/license.php GNU Public License
 * @link     http://imensosoftware.com/recipes
 */

namespace App\Http\Controllers;

use App\Models\UserSoftwareTool;
use App\Models\SoftwareTool;
use App\Models\Software;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use DB ;

/**
 * UserSoftwareToolController class
 *
 * The class manage the User Software Tool
 *
 * @category UserSoftwareToolController
 * @package  Ez
 * @author   Imenso Software <admin@imensosoftware.com>
 * @license  http://imensosoftware/license.php GNU Public License
 * @link     http://imensosoftware.com/recipes
 */

class UserSoftwareToolController extends Controller
{
    /**
     * GetUserSoftwareTools function gets the software tools for the specified user
     * and return it in json format.
     *
     * @param  \Illuminate\Http\Request  request
     * @return json
     */
    public function getUserSoftwareTools(Request $request): \Illuminate\Http\JsonResponse
    {
        $user_id = Auth::user()->id ;
        $userSoftwareTools = Software::
        select("user_software_tools.*", 'software_tools.software_id', 'software_tools.tool', 'softwares.software')
        ->leftJoin('software_tools', 'software_tools.software_id', '=', 'softwares.id')
        ->leftJoin('user_software_tools', function ($join) use ($user_id) {
            $join->on('user_software_tools.software_tool_id', '=', 'software_tools.id')
            ->where('user_software_tools.user_id', '=', $user_id);
        })
        ->orderBy('softwares.id', 'asc')->get();

        $userSoftwareToolCount = UserSoftwareTool::where('user_id', $user_id)->count();


        $userSoftwareCount = Software::
        select("softwares.*", DB::raw('count(user_software_tools.id) as userSoftwareCount'))
        ->leftJoin('software_tools', 'software_tools.software_id', '=', 'softwares.id')
        ->leftJoin('user_software_tools', function ($join) use ($user_id) {
            $join->on('user_software_tools.software_tool_id', '=', 'software_tools.id')
            ->where('user_software_tools.user_id', '=', $user_id);
        })
        ->groupBy('softwares.id')->get();



        return response()->json(
            [
                'status' => 'success',
                'userSoftwareTools' => $userSoftwareTools->toArray(),
                'userSoftwareToolCount' => $userSoftwareToolCount,
                'userSoftwareCount' => $userSoftwareCount
            ],
            200
        );
    }

    /**
     * Update function updates the software tools for the specified user in the storage
     * and return json.
     *
     * @param  \Illuminate\Http\Request  request
     * @return json
     */
    public function update(Request $request): \Illuminate\Http\JsonResponse
    {
        $user_id = Auth::user()->id ;
        $i = 0 ;
        $ids = [] ;
        if ($request->software_tool_id) {
            foreach ($request->software_tool_id as $software_tool_id) {
                $ids[] = $software_tool_id ;
                $userSkill = UserSoftwareTool::updateOrCreate(
                    ['user_id' => $user_id,'software_tool_id' => $software_tool_id],
                    ['user_id' => $user_id,'software_tool_id' => $software_tool_id]
                );
                $i++;
            }
        }
        
        if (sizeof($ids) > 0) {
            UserSoftwareTool::where('user_id', $user_id)->whereNotIn('software_tool_id', $ids)->delete();
        } else {
            UserSoftwareTool::where('user_id', $user_id)->delete();
        }
        
    
        $userSoftwareTools = Software::
        select("user_software_tools.*", 'software_tools.software_id', 'software_tools.tool', 'softwares.software')
        ->leftJoin('software_tools', 'software_tools.software_id', '=', 'softwares.id')
        ->leftJoin('user_software_tools', function ($join) use ($user_id) {
            $join->on('user_software_tools.software_tool_id', '=', 'software_tools.id')
            ->where('user_software_tools.user_id', '=', $user_id);
        })
        ->orderBy('softwares.id', 'asc')->get();

        $userSoftwareToolCount = UserSoftwareTool::where('user_id', $user_id)->count();

         $userSoftwareCount = Software::
            select("softwares.*", DB::raw('count(user_software_tools.id) as userSoftwareCount'))
            ->leftJoin('software_tools', 'software_tools.software_id', '=', 'softwares.id')
            ->leftJoin('user_software_tools', function ($join) use ($user_id) {
                $join->on('user_software_tools.software_tool_id', '=', 'software_tools.id')
                ->where('user_software_tools.user_id', '=', $user_id);
            })
            ->groupBy('softwares.id')->get();

        return response()->json(
            [
                'status' => 'success',
                'message' => 'Software & tools updated successfully.',
                'userSoftwareTools' => $userSoftwareTools->toArray(),
                'userSoftwareToolCount' => $userSoftwareToolCount,
                'userSoftwareCount' => $userSoftwareCount
            ],
            200
        );
    }
}