php – 定义Laravel表单字段的默认值

前端之家收集整理的这篇文章主要介绍了php – 定义Laravel表单字段的默认值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
要预先填充表单字段,我们可以在create.blade.PHP中为表单字段添加“value”:
{{ Form::text('title','Some default title') }}

有没有办法在其他地方完成这项任务(可能在模型或控制器?).我想在create& amp;中创建相同的表单字段代码.编辑视图.谢谢!

好的,我们在这里……我在示例中使用了Laravel的表单模型绑定. (我使用User model / db table).
如果您不清楚此主题,请查看此 http://laravel.com/docs/html#form-model-binding
// Controller

class UsersController extends BaseController
{

    ...

    // Method to show 'create' form & initialize 'blank' user's object
    public function create()
    {
        $user = new User;
        return View::make('users.form',compact('user'));
    }

    // This method should store data sent form form (for new user)
    public function store()
    {
        print_r(Input::all());
    }

    // Retrieve user's data from DB by given ID & show 'edit' form   
    public function edit($id)
    {
        $user = User::find($id);
        return View::make('users.form',compact('user'));
    }

    // Here you should process form data for user that exists already.
    // Modify/convert some input data maybe,save it then...
    public function update($id)
    {
        $user = User::find($id);
        print_r($user->toArray());
    }

    ...

}

这里是控制器提供的视图文件.

// The view file - self describing I think
<!doctype html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    @if(!$user->id)
    {{ Form::model($user,['route' => 'admin.users.store']) }}
    @else
    {{ Form::model($user,['route' => ['admin.users.update',$user->id],'method' => 'put']) }}
    @endif
        {{ Form::text('firstName') }}

        {{ Form::text('lastName') }}

        {{ Form::submit() }}
    {{ Form::close() }}
</body>
</html>

猜你在找的Laravel相关文章