更新文章laravel

控制器文章

 public function update(Request $request, Article $article){
    $article->update($request->except('slug', 'image_path'));
    if ($request->hasFile('image_path')) {
        $image = $request->file('image_path');
        $new_name = rand() . '.' . $image->getClientOriginalExtension();
        $image->move(public_path('images'), $new_name);

        $article->image_path = $new_name;
        $article->save();
    }
    $article->categories()->detach();
    if ($request->has('categories')) {
        $article->categories()->attach($request->input('categories'));
    }
    $user=auth()->user();
    return redirect()->back()->with('message', 'Raksts atjaunots!');

}

public function edit(Article $article){
    $user=auth()->user();
        return view('edit',[
            'article' => $article,
            'categories' => Category::with('children')->where('parent_id',0)->get(),
            'delimiter' => ''
        ]);
}

编辑刀片

<form class="form-horizontal" action="{{route('update', $article)}}" method="post" enctype="multipart/form-data" style="display: flex;justify-content: center;flex-direction: column;">
    <input type="hidden" name="_method" value="put">
    {{ csrf_field() }}

    {{-- Form include --}}

    <img src="{{URL::to('/images').'/'.$article->image_path}}" alt="">

    @include('partials.form')

    <input type="hidden" name="modified_by" value="{{Auth::id()}}">
  </form>

链接到表格

<tbody>
    @foreach ($articles_suggest_user as $article)
        <a class="btn btn-default" href="{{route('edit', $article)}}"><i class="fa fa-edit"></i></a>
    <?php } ?>
    @endforeach
</tbody>

web.php

Route::get('/home/edit/{id}','Controller_Article_parents@edit', function () {
    return view('/home/edit');
 })->name('edit');


Route::get('/home/update/','Controller_Article_parents@update', function () {
  return view('/home');
 })->name('update');

When i clicked the link, i move to for example this url http://127.0.0.1:8000/home/edit/190

但是我的表格是空的...输入为空。打开表单时会显示输入信息怎么办?

请帮我

评论
  • zamet
    zamet 回复

    If you want to redirect with form input, you can use the withInput() function

    return back()->withInput();
    

    https://laravel.com/docs/7.x/redirects#creating-redirects

    现在,此功能旨在与表单验证一起使用,可能不是您想要的。

    What I have done in the past when combining create & edit views into a single template is something like this:

    {!! Form::text('name', isset($model) ? $model->name : null, [
            'class'         => 'form-control', 
            'placeholder'   => 'Please fill out this field &hellips;',
            'required'      => true
    ]) !!}
    

    This uses the Laravel Collective HTML library. However the principle is the same if you are using raw hmtl like so:

    <input type="text" value="{{ isset($model) ? $model->name : null }}">