Temporarily disable Laravel appends Temporarily disable Laravel appends laravel laravel

Temporarily disable Laravel appends


I have had experience with this too. I've found a good solution here.

But, if you like a one-liner solution, you can also use the ff methods of Eloquent's Model class:

  • setHidden(array $hidden)

  • makeHidden(array|string $attributes)

You can check these here.


I was using this code is suitable:testing for Model name Product for example

// get product with "id = 1" for example$needed_product = Product::find(1)->toArray();// remove un-used attributes$product = new Product;foreach ($product->appends as $attr) {    unset($needed_product[$attr]);}

Now the $needed_product gets without any appends attributes


I was thinking something like this:

/** * Get all appended items. * * @return array */public function getAppends(){    $vars = get_class_vars(__CLASS__);    return $vars['appends'];}/** * Unset all appended items. * * @return $this */public function unsetAppends(){    collect($this->getAttributes())->pull($this->getAppends());    return $this;}

But @elegisandi thanks that works great.