File: /home/imensosw/www/ezwork/app/Models/UserSoftwareTool.php
<?php
/**
* UserSoftwareToolModel class file
*
* PHP Version 7.2
*
* @category Model
* @package Ez
* @author Imenso Software <admin@imensosoftware.com>
* @license http://imensosoftware/license.php GNU Public License
* @link http://imensosoftware.com/recipes
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use DB;
/**
* UserSoftwareToolModel class
*
* The class manage User Software Tool related queries
*
* @category UserSoftwareToolModel
* @package Ez
* @author Imenso Software <admin@imensosoftware.com>
* @license http://imensosoftware/license.php GNU Public License
* @link http://imensosoftware.com/recipes
*/
class UserSoftwareTool extends Model
{
public $table = "user_software_tools";
public $fillable = ['user_id','software_tool_id'];
public $timestamps = false;
/**
* GetUserSoftwareTools function gets the software tools of a specified user
* and return it.
*
* @param int $user_id
* @return \App\Models\Software
*/
public static function getUserSoftwareTools($user_id)
{
return \App\Models\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()->toArray();
}
/**
* GetUserSoftwareToolCount function gets count of the software tools of a specified user
* and return it.
*
* @param int user_id
* @return \App\Models\UserSoftwareTool
*/
public static function getUserSoftwareToolCount($user_id)
{
return UserSoftwareTool::where('user_id', $user_id)->count();
}
/**
* GetUserSoftwareCount function gets count of the software of a specified user
* and return it.
*
* @param int user_id
* @return \App\Models\Software
*/
public static function getUserSoftwareCount($user_id)
{
return \App\Models\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()->toArray();
}
}