简体   繁体   English

在表格中为 Laravel 7 创建默认值

[英]Create default value in form for Laravel 7

I have a model:我有一个 model:

namespace Modules\Acquisition\Entities;

use Illuminate\Database\Eloquent\Model;

class AcqMBudgets extends Model
{
    public $timestamps = false;
    
    protected $primaryKey = 'budget_id';
    
    protected $fillable = ['budget_code', 'budget_name', 'balance'];
    
    protected $attributes = [
        'balance' => 0,
    ];
}

On my controller:在我的 controller 上:

<?php

namespace Modules\Acquisition\Http\Controllers;

use Modules\Acquisition\Entities\AcqMBudgets;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class BudgetController extends Controller
{
    //...//
    
    public function add()
    {
        return view('acquisition::budget.add');
    }

    //...//
}

and lastly on my view:最后在我看来:

{{ Form::open(['url' => '/acquisition/master/budget/create', 'method' => 'POST']) }}

<div class="row">
    {{ Form::label('budget_code', 'Code') }}
    {{ Form::text('budget_code', '') }}
</div>

<div class="row">
    {{ Form::label('budget_name', 'Name') }}
    {{ Form::text('budget_name', '') }}
</div>

<div class="row">
    {{ Form::label('balance', 'Balance') }}
    {{ Form::text('balance', '') }}
</div>

{{ Form::close() }}

What I wanted is setting up the default value for balance to 0 when I displayed the form.我想要的是在显示表单时将balance的默认值设置为0 I used this method based on Laravel 5.x documentation, but the result is always a blank text input?我根据Laravel 5.x文档使用了这个方法,但是结果总是输入空白文本? By the way, the table structure in database is as following:顺便说一下,数据库中的表结构如下:

table name: acq_m_budgets
----------
budget_id serial NOT NULL, -- The primary key
budget_code character varying(10) NOT NULL,
budget_name character varying(100) NOT NULL,
balance numeric(16) DEFAULT 0,

You are looking for setting the default value of the text input.您正在寻找设置文本输入的默认值 You can do this like:你可以这样做:

    {{ Form::text('balance', '0') }}

Or, if you have access to the $balance variable:或者,如果您有权访问 $balance 变量:

    {{ Form::text('balance', $balance) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM