1

I want to assign a default value in Laravel model but the value should come from the config file.

I am writing below code but its giving me error

    protected $attributes = [
        'is_generation'    => true,
        'price'       => config('test.MY_PRICE'),
    ];

it's showing me an error like Constant expression contains invalid operations

How can I get value from config file and set in $attribute variable in Laravel model file?

3
  • what do u mean by config file ? there is .env file in laravel where you can have global values and access them any where Commented Sep 5, 2019 at 9:17
  • env have MY_PRICE = 30 and in config file MY_PRICE = env('MY_PRICE'); Commented Sep 5, 2019 at 9:20
  • 1
    You can't assign dynamic value as a default value for an attribute, or for a constant of a class. better use a method that calls the config(...) if no value is in your attribute Commented Sep 5, 2019 at 9:20

3 Answers 3

1

You can use the saving model event by adding this code to your Eloquent Model:

protected static function boot()
{    
    // sets default values on saving
    static::saving(function ($model) {
        $model->price = $model->price ?? config('test.MY_PRICE');
    });
    parent::boot();
}

With this code in place, if the field price is null, it will have assigned a value from the config key just a moment before saving the Model in the database.

BTW you can change the check like if it's an empty string or less then a number and so on, this is only an example.

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

Comments

0

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

The only way you can make this work is :-

<?php

namespace App;

class Amazon
{
  protected $serviceURL;

  public function __construct()
  {
    $this->serviceURL = config('api.amazon.service_url');
  }
}

Comments

0

You can use attribute mutator as explained here: https://laravel.com/docs/5.8/eloquent-mutators#defining-a-mutator

Class Example{

 public function setPriceAttribute(){
    return $this->attributes['price'] = config('test.MY_PRICE');
 }
}

2 Comments

its not working for me as it only work when i try to set this attribute value while i am saving object but i dont want to do that.
It seems mutator only works with laravel eloquent like you tried using an object. Are you using query builder? Something like Model::create($data);.

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.