0

There is a model User and a model Payments. I need to create a resource such that returns user with successful payment which is identified by a status fields. This is my solutions:

class User extends BaseModel
{
    use HasFactory, SearchableTrait, SortableTrait;

    protected $fillable = ['name', 'mobile', 'email', 'username', 'hash_id'];
    public $searchable = ['name', 'mobile', 'email', 'id', 'user:referred'];
    public $sortable = ['name', 'mobile', 'email', 'id'];

    protected $table = 'users';


    public function invoices()
    {
        return $this->hasMany(Invoice::class,'user_id');
    }
}

and in controller

public function index()
{
    $users = new User();
    $users = $users::with(['invoices'])->filtered()->sorted();
    return UserResource::collection($users->paginate());
}

and

<?php

namespace Modules\User\Transformers;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{  

public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'mobile' => $this->mobile,            
        'invoices' => count($this->invoices()->where('status', 2999)->get()),            
    ];
}
}

and model

class Invoice extends Model
{

    protected $fillable = [];
    protected $table = 'payment_invoices';


    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function scopeSuccessful($query)
    {
        return $query->where('status', 2999);
    }
}

It seems to be messy solution. I am looking for something to use the scope inside the model relation inside the resource. How can I do that?

2
  • $users = new User(); You're creating a new user here, does a new user have invoices? (Btw, I'd name it $user instead of $users since it only is really one user) Commented Nov 18, 2020 at 11:23
  • @kerbh0lz it returns all users in paginate Commented Nov 18, 2020 at 11:26

2 Answers 2

1

in your controller you can just do this :

use Illuminate\Database\Eloquent\Builder;

public function index()
{
    $users = User::with(['invoices'])->whereHas('invoices', function(Builder $query) {
      $query->where('status', 2999);
    });
    return UserResource::collection($users->paginate());
}

Sign up to request clarification or add additional context in comments.

Comments

1
<?php
class User extends BaseModel
{
    use HasFactory, SearchableTrait, SortableTrait;

    protected $fillable = ['name', 'mobile', 'email', 'username', 'hash_id'];
    public $searchable = ['name', 'mobile', 'email', 'id', 'user:referred'];
    public $sortable = ['name', 'mobile', 'email', 'id'];

    protected $appends = ['success_payments']; // append this value in all queries    

    protected $table = 'users';


    public function invoices()
    {
        return $this->hasMany(Invoice::class,'user_id');
    }

    //the function for it
    public function getSuccessPaymentsAttribute(){
        return App\Payment::where('user_id', $this->id)->where('status', 'success')->get(); //replace the attribute name or condition as per your db structure
    }
}

You can use this in your User Model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.