如何调用控制器的更新功能

时间:2019-02-20 11:45:07

标签: php laravel controller

我正在尝试在 dashboard.blade.php 页面上使用控制器的更新功能,但是当我按“保存”时,它将切换为 show.blade.php 页面而不是更新。

我的 dashboard.blade.php 页面:

                    @if(count($posts) > 0 )
                            <table class="table table-striped">
                                <tr>
                                    <th>Title</th>
                                    <th>Body</th>
                                    <th>Employee Number</th>
                                    <th>Edit</th>
                                    <th>Save</th>
                                </tr>
                                @foreach($posts as $post)
                                    <tr>
                                        <td>{{$post->title}}</td>
                                        <td><input type='text' name='body'class='form-control' value='{{$post->body}}'></td>
                                        <td>{{$post->employee_no}}</td>
                                        <td><a href="{{ action("PostsController@update", $post->id) }}" >Save</a></td>
                                        <td><a href="/lsapp/public/posts/{{$post->id}}/edit" class="btn btn-default">Edit</a></td>
                                    </tr>
                                @endforeach
                            </table>
                    @else 
                        <p>You Have No Posts</p>
                    @endif

我在 PostsController.php 页面上的更新功能:

public function update(Request $request, $id)
    {
        $this->validate($request,[
            //'title' => 'required',
            'body' => 'required'
            ]);
    
            //Update Post
            $post = Post::find($id);
            //$post->title = $request->input('title');
            $post->body = $request->input('body');
            $post->save();
    
           return redirect('/posts')->with('success','Post Updated');
    }

我了解到“动作”将进入与模式匹配的第一条路线,但我不知道如何解决此问题。

我的 web.php 页面:

Route::get('/', 'PagesController@index');
Route::get('/about', 'PagesController@about');
Route::get('/services', 'PagesController@services');

Route::resource('posts','PostsController');


Auth::routes();

Route::get('/dashboard', 'DashboardController@index');

如何从 dashboard.blade.php 页面正确调用更新功能?

3 个答案:

答案 0 :(得分:0)

要更新数据,必须使用formput方法。 路线将是这样,

Route::put('dashboard/update/{id}',[
            'uses' => 'DashboardController@update',
            'as' => 'dashboard.update'
        ]);

更多Laravel Routing

希望它会有所帮助:)

答案 1 :(得分:0)

您可以通过使用带有隐藏PUT方法的表单提交来解决此问题。

<form action="{{ url('customers') }}/{{$data['customer']->id}}" method="POST">
        <input type="hidden" name="_method" value="PUT">
        @csrf
</form>

答案 2 :(得分:0)

表中的每一行都有一个简单的表单:

@if(count($posts) > 0 )
    <table class="table table-striped">
        <tr>
            <th>Title</th>
            <th>Body</th>
            <th>Employee Number</th>
            <th>Edit</th>
            <th>Save</th>
        </tr>
        @foreach($posts as $post)
            <tr>
                <td>{{$post->title}}</td>
                <td>{{$post->employee_no}}</td>
                <td>
                    <!-- Form for each row -->
                    <form action="{{ route("posts.update", [ 'post' => $post->id ]) }}" method="POST">
                        @method('PUT')
                        @csrf
                        <input type='text' name='body'class='form-control' value='{{$post->body}}'></td>
                        <button type="submit">Save</button>
                    </form>
                    <!-- Form for each row -->
                <td><a href="/lsapp/public/posts/{{$post->id}}/edit" class="btn btn-default">Edit</a></td>
            </tr>
        @endforeach
    </table>
@else 
    <p>You Have No Posts</p>
@endif