Laravel old() 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41659717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:07:54  来源:igfitidea点击:

Laravel old() not working

phplaravellaravel-5

提问by BOTJr.

{!! Form::open(array('route' => 'posts.store', 'data-parsley-validate' => '')) !!}
    {{ Form::label('title', 'Title:') }}
    {{ Form::text('title',old('title'), array('class' => 'form-control', 'required' => '', 'maxlength' => '255')) }}
    {{ Form::label('slug', 'Slug:') }}
    {{ Form::text('slug',old('slug'), array('class' => 'form-control', 'required' => '', 'minlength' => '5', 'maxlength' => '255') ) }}
    {{ Form::label('category_id', 'Category:') }}
        <select class="form-control" name="category_id">
            @foreach($categories as $key=>$value)
                <option value='{{ $key }}'>{{ $value }}</option>
            @endforeach
        </select>  
    {{ Form::label('tags', 'Tags:') }}
        <select class="form-control select2-multi" name="tags[]" multiple="multiple">
            @foreach($tags as $tag)
                <option value='{{ $tag->id }}'>{{ $tag->name }}</option>
            @endforeach
        </select>
    {{ Form::label('body', "Post Body:") }}
    {{ Form::textarea('body',old('body'), array('class' => 'form-control','id'=>'editor1')) }}
    {{ Form::submit('Create Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }}
{!! Form::close() !!}

This is my form for something but however when i submit it and validate it on the server side and redirect to the same page , it doesn't display the old values.

这是我的表单,但是当我提交它并在服务器端验证它并重定向到同一页面时,它不会显示旧值。

Controller side

控制器端

$this->validate($request,[
    'title'=>'required|max:255',
    'body'=>'required',
    'slug'=>'required|max:255|alpha_dash|min:5|unique:posts,slug',
    'category_id'=>'required|integer'
]);

$post=new Post;
$post->title=$request->title;
$post->slug=$request->slug;
$post->body=$request->body;
$dom = new Dom;
$dom->load($request->body);
$img = $dom->find('img')[0];
if(is_null($img))
    return redirect()->back()->withErrors('Add atleast one image in the post.');

What would be the solution of the problem?

问题的解决方案是什么?

回答by Niklesh Raut

Use ->withInput()to go back with data

用于->withInput()返回数据

    return redirect()->back()->withInput()->withErrors('Add atleast one image in the post.');

Check in docs : Old inputsin https://laravel.com/docs/master/requests

签入文档:Old inputshttps://laravel.com/docs/master/requests