Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default values on fields when creating #493

Closed
wfeller opened this issue Sep 8, 2018 · 6 comments
Closed

Add default values on fields when creating #493

wfeller opened this issue Sep 8, 2018 · 6 comments

Comments

@wfeller
Copy link

wfeller commented Sep 8, 2018

Would it be possible to add a default value on fields? This value would be used only when creating.

Something like this maybe:
Field::make('My field')->defaultValue('some value')

@ghost
Copy link

ghost commented Sep 9, 2018

You can pass this by $attributes in Model
Example: Project/app/Booking.php

use Illuminate\Database\Eloquent\Model;

class Booking extends Model
{
    protected $attributes = [
        'status' => 'pending',
        'price' => 0,
        'commission' => 0,
    ];
}

@wfeller
Copy link
Author

wfeller commented Sep 9, 2018

Sweet, tyvm 👍

@wfeller wfeller closed this as completed Sep 9, 2018
@zeevin
Copy link

zeevin commented Dec 25, 2018

also like this:

Text::make( 'vsn')->rules('required', 'max:64', 'unique:vendors,vsn,{{resourceId}}')
->withMeta(
[
'value' => (function () {
return $this->vsn ?? 2;
})(),
]
),

@dillingham
Copy link
Contributor

Also if you need method calls you can set it on the resource

public static function newModel()
{
    $model = new static::$model;
    $model->setAttribute('user_id', auth()->id());
    return $model;
}

@notflip
Copy link

notflip commented Sep 2, 2019

You can pass this by $attributes in Model
Example: Project/app/Booking.php

use Illuminate\Database\Eloquent\Model;

class Booking extends Model
{
    protected $attributes = [
        'status' => 'pending',
        'price' => 0,
        'commission' => 0,
    ];
}

Where in the nova source does this get read?

@dillingham
Copy link
Contributor

@notflip when nova instantiates a model, those values come pre-filled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants