0

How can I use append attributes with Laravel Eloquent and pagination? We are trying to load the data from Laravel Eloquent, wherein we have to append attributes. So we want to query based on the append attributes, which works fine when not using skip and take. But we need to add the pagination and need the option of skip and take, but return an error.

Model

protected $appends = ['isSlaBreach'];

public function getIsSlaBreachAttribute() 
{
    return true or false
}

Controller

$overdue_tickets->skip($skip);
$overdue_tickets->take($take);
$res = $overdue_tickets->get()->where('isSlaBreach', true);

Need guidance on the same.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
santosh
  • 51
  • 6

1 Answers1

0

but which error are you getting?

Note that querying over appended attributes it is not possible using directly the db: you are able to query over appended attributes by using Eloquent Collections.

I think in your code there are some problems:

1 - in the Model return true or false returns always true

2 - In the controller you should add a final all() in order to get filtered elements from the collection:

$res = $overdue_tickets->get()->where('isSlaBreach',true)->all();

Dharman
  • 30,962
  • 25
  • 85
  • 135
gecche
  • 1
  • 1
  • return true or false is just for example it return on basis of logic which i hav not added. i trued adding all but it returns just two response however total no of records return is high – santosh Jan 27 '22 at 17:19
  • @gecche That above is not correct statement. Check [here](https://stackoverflow.com/questions/34587457/difference-between-eloquent-modelget-and-all#answer-34587647). – Tpojka Jan 27 '22 at 19:42
  • why is not correct? the all() method is applied to the eloquent collection after the get() applied to the builder. i’m missing something? – gecche Jan 28 '22 at 21:01