【问题标题】:How do you change the action with Form Model Binding in Laravel?如何在 Laravel 中使用 Form Model Binding 更改操作?
【发布时间】:2017-02-28 17:57:11
【问题描述】:

我有一个简单的模型绑定表单,刚刚检查了刀片生成的html,并且动作指向了错误的url。我写错路线了吗?没有任何文档对此有所帮助。 动作指向 /users 而不是 /users/{user},这是路由指向的位置。

@extends('layout')


@section('content')
    <h1>This is a test.</h1>
    <ul>
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
    {!! Form::model($user, ['method'=>'put', 'route'=>['users.update', $user->id], 'class'=>'form']) !!}

    <div class="form-group">
         {{ csrf_field() }}
        {!! Form::label('Your Name') !!}
        {!! Form::text('name', null, 
            ['required', 'class' => 'form-control', 'placeholder'=>'Your name']
        ) !!}
    </div>

    <div class="form-group">
        {!! Form::label('Your E-mail Address') !!}
        {!! Form::text('email', null, 
            ['required', 'class' => 'form-control', 'placeholder'=>'Your E-mail Address']
        ) !!}
    </div>
    <div class="form-group">
        {!! Form::submit('Submit', ['class'=>'btn btn-primary']) !!}
    </div>
    {!! Form::close() !!}

@stop

可以在此处找到路线打印输出:http://pastebin.com/4wpMsz4k

【问题讨论】:

    标签: php laravel-5 model laravel-blade laravel-form


    【解决方案1】:

    试试下面这个,

    {!! Form::model($user, array('route' => array('users.update', $user->id), 'method' => 'PUT','class' => 'form')) !!}
    

    【讨论】:

    • 不幸的是,这不起作用。仍然没有改变动作。
    【解决方案2】:

    问题解决了。这是 id 格式不正确的问题。它应该是 $user->ID,而不是 $user->id。

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 2015-09-07
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2015-11-03
      • 2021-07-15
      • 2014-12-21
      • 2019-08-02
      相关资源
      最近更新 更多