0

I have the following files:

LicensesController.php:

namespace App\Http\Controllers;

use App\Models\License;

class LicencesController
{

    public function showLicense()
    {
        return License::query()
            ->join('plans', 'licenses.plan_id', '=', 'plans.id')
            ->where('licenses.client_id', 1)
            ->get(['plans.id', 'plan_name', 'storage', 'price','expires_at']);
    }
}

Licenses.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class License extends Model
{
    public $timestamps = false;
    protected $appends = ['licenses'];


    public function getLicensesAttribute($licenses): array
    {
        return [
            'total_licenses' => ENTIRE_RESULT_OF_THE_CONSULTATION_HERE,
            'licenses_available' => '123'
        ];
    }
}

How do I, to do the following SQL:

$totalLicenses = License::query()
    ->where('client_id', 1)
    ->get('licenses_quantity');

And with this SQL getting only the integer value of 'licenses_quantity' to populate the 'total_licenses' field in the getLicensesAttribute function?

  • Are you trying to get the total number of licenses per plan for a given client id? – user3532758 Feb 17 '22 at 03:00
  • @user3532758 Yes!, and this information I get from the last SQL I put here, I just need to know how I can get only the field value of this result (as an integer) and how to pass this value to my function "getLicensesAttribute($licenses)" – Lucas Fernandes Feb 17 '22 at 11:02

2 Answers2

0

Total licenses per given client id would be:

$totalLicenses = License::query()
    ->where('client_id', 1)
    ->count();

Tho I'm not sure what you are trying to do with the get attribute.

P. K. Tharindu
  • 2,565
  • 3
  • 17
  • 34
  • I don't need to count the lines to know the number of licenses, this number already comes from a predefined field in my database. Pure SQL example: SELECT licenses_quantity FROM licenses WHERE client_id = 1. This result has to be somehow passed to the "getLicensesAttribute()" function and it has to be entered there as an integer, only the number that is in my bank, which is the number of licenses my client has. I can do the SQL, I just can't pass its result as an integer to the function I want – Lucas Fernandes Feb 17 '22 at 11:58
0

try something this:

$totalLicenses = License::query()
->where('client_id', 1)->first()->licenses['total_licenses'];
  • If you write "Try", it looks like you are guessing. Better explain what the issue was and how your solution resolves that. – trincot Feb 17 '22 at 19:10