Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporarily disable Laravel appends

Tags:

php

laravel

Is it possible to temporarily disable the appends functionality in Laravel 5.4 during testing?

protected $appends = [
        'full_name',
    ];

I want to ignore that ^.

I've made a model factory but when I'm testing I don't want to have these append items on my model.

like image 794
Jenssen Avatar asked Jul 23 '17 12:07

Jenssen


2 Answers

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.

like image 157
elegisandi Avatar answered Nov 06 '22 00:11

elegisandi


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

like image 32
Ahmed Sayed Sk Avatar answered Nov 06 '22 02:11

Ahmed Sayed Sk