标签的编辑

看了博主的教程后,自己试着自己去完成一个标签的管理功能,于是把标签当成文章那样改了,但是标签并不需要Request表单验证。

个人标签的编辑代码

@extends('app')
@section('content')
    <div class="col-md-10">
    <div class="panel panel-default">
        <div class="panel-heading">修改标签</div>
         {!! Form::model($tag,['url'=>'admin/tags/update']) !!}
        @if ($errors->has('error'))
        <div class="alert alert-danger alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">×</span></button>
            <strong>Error!</strong>
            { $errors->first('error', ':message') }
            <br />
            请联系开发者!
        </div>
        @endif

        <div class="panel-body">
            {!! Form::open(['route' => 'admin.tags.store', 'method' => 'post','class'=>'form-horizontal']) !!}


            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">标签名</label>
                <div class="col-sm-3">
                    {!! Form::text('name', $tag->name, ['class' => 'form-control']) !!}
                    <font color="red">{ $errors->first('name') }</font>
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    {!! Form::submit('修改', ['class' => 'btn btn-success']) !!}
                </div>
            </div>
            {!! Form::close() !!}
        </div>
    </div>
</div>
@endsection

TagsController里面update的代码:

    public function update($id)
    {
        //根据id查询到需要更新的tag
        $tag = Tag::find($id);
        //使用Eloquent的update()方法来更新,
        //request的except()是排除某个提交过来的数据,我们这里排除id
        $tag->update($id->except('id'));      
        return redirect('/admin/tags');
    }

结果就出现了这样的错误了:

ErrorException in D:\laravel5\app\Http\Controllers\Admin\TagsController.php line 74:

Missing argument 1 for App\Http\Controllers\Admin\TagsController::update()

74行就是public function update($id),
不知道少了哪个参数,求指导,怎样可以查看表单submit上传了什么数据?

JellyBool

你在{!! Form::model($tag,['url'=>'admin/tags/update']) !!}中的url并没有指定参数id,也就是说你在执行的时候,update($id)你并没有传递id啊,所以会报错的啊。

Kirits

@JellyBool 加上这一句{!! Form::hidden(‘id’,$tag->id) !!}?加上了还是显示少了一个参数

Kirits


{!! Form::model($tag,['url'=>'admin/tags/update']) !!}
改成

 {!! Form::model($tag, array('url' => URL::route('admin.tags.update', $tag->id), 'method' => 'PUT', 'class' => "am-form")) !!}
```,
再将update改成

public function update($id)
{
rules=array(name=>array(required,regex:/\w+rules = array( 'name' => array('required', 'regex:/^\w+/’),
);
Tag::find($id)->update(Input::only(‘name’));
return Redirect::to(‘admin/tags’);
}

就成功了
Kirits
$rules = array(
    'name' => array('required', 'regex:/^\w+$/'),
    );

这一段可以不用

JellyBool

@Kirits 你修改之后就是传入了参数啊

JellyBool

@Kirits Markdown代码块的三个标记分开写,就是每个标志占一行,这样就好看很多了

Kirits

@JellyBool 怎么分开?

JellyBool

@Kirits

^^^
这里是代码
^^^

像是上面这样,将^替换成代码的```

jw046

##title#

snail

好想法,楼主还教人发表文章