Laravel补丁方法仅刷新页面,不更新数据库字段

时间:2018-11-15 16:35:01

标签: laravel

控制器功能:

    public function setFlag(Request $request)
{
    $user = User::find((int)$request->input('id'));
    $user->flag = 1;
    $user->save();
}

刀片文件

@extends ('layouts.app')
@section('content')
@if($user->user_picture)
    <img src= "data:{{$user->user_picture_type}};base64,{{$user->user_picture}}" height="100" width="100">
@else
    <img src="{{ URL::to('/') }}/images/blankProfile.png" height="100" width="100">
@endif
<div>Name: {{$user->name}}</div>
<div>Bio: {{$user->bio}}</div>
<div>Child Bio: {{$user->child_bio}}</div>
<div>Parent Age: {{$user->parent_age}}</div>
<div>Child Age: {{$user->child_age}}</div>
<div>City: {{$user->city}}</div>
<div>State: {{$user->state}}</div>
<a href="/ratings/{{$user->id}}"><button type="submit" class="btn btn-primary">View Users Ratings</button></a>
<br />
<br />
  {{Form::open(array('action' => array('ProfileController@setFlag', $user->id), 'method' => 'PATCH'))}}
 {{ csrf_field() }}
 <input type="hidden" value = "{{$user->id}}" id = "id" name = "id">
<button type="submit" class="btn btn-primary">Report User</button>
{{Form::close()}}
@if(Auth::user()->level == 1)
    <br />
    <br />
    <a href="/"><button type="submit" class="btn btn-primary">Delete User</button></a>
    @if($user->flag == 1)
    <a href="/"><button type="submit" class="btn btn-primary">Unflag</button></a>
    @endif
@endif  
@endsection

配置文件控制器的所有路由:

Route::get('/profile', 'ProfileController@index')->middleware('auth')->name('profile.index');
Route::get('/profile/edit', 'ProfileController@edit')->middleware('auth');
Route::resource('profile', 'ProfileController')->middleware('auth');
Route::get('/profile/{userID}', 'ProfileController@show')->middleware('auth');
Route::patch('/profile/{userID}','ProfileController@setFlag')->middleware('auth');

所以我的问题是,当用户单击“提交”按钮时,它只会刷新页面,而在控制器内部什么也不做。我在控制器内部有一个dd($ request),它甚至从未达到dd。我已经花了几个小时尝试不同的解决方案,但到目前为止没有任何效果。我尝试使用默认的html表单,以不同的laravel表单更改路线名称,与控制器搞混,并尝试在按钮按下时强制为特定用户设置标志,但这些都不起作用。

我认为问题与路由有关。...但是我不知道如何路由默认Route :: resource中未包含的修补方法。是因为/ profile / {userID}有两条不同的路由吗?感谢您的帮助,感谢您的宝贵时间

编辑:profilecontroller内部的更新功能

 public function update($id, Request $request)
    {                
        $user = User::find(Auth::user()->getId());          

        $request->validate([
                'name' => 'required',
                'image' => '|image|mimes:jpeg,png,jpg,svg|max:2048',
                'bio' => 'required',
                'child_bio' => 'required',
                'parent_age' => 'required|integer|min:18',
                'city' => 'required',
                'state' => 'required',
                'child_age' => 'required|integer|between:1,8',

        ]);
        if($request->hasFile('image')) {
            $user_picture = base64_encode(file_get_contents($request->file('image')));
            $user_picture_type = $_FILES['image']['type'];

            DB::table('users')->where('id', $id)->update(
                [
                    'user_picture' => $user_picture,
                    'user_picture_type' => $user_picture_type
                ]
            );
        }


        $name = $request->input('name');
        $bio = $request->input('bio');
        $child_bio = $request->input('child_bio');
        $parent_age = $request->input('parent_age');
        $city = $request->input('city');
        $state = $request->input('state');
        $child_age = $request->input('child_age');
        $geocoder = new \OpenCage\Geocoder\Geocoder('8c0bbd03698f4bdaa4e35fe38c30fbd1');
        $result = $geocoder->geocode($city . ' ' . $state);
        $first = $result['results'][0];
        $lat = $first['geometry']['lat'];
        $lng = $first['geometry']['lng'];
        //$level = 1;        


        DB::table('users')->where('id', $id)->update(
            [
                'name' => $name,
                'bio' => $bio,
                'child_bio' => $child_bio,
                'parent_age' => $parent_age,
                'city' => $city,
                'state' => $state,
                'child_age' => $child_age,
                'lat' => $lat,
                'lng' => $lng,

            ]
        ); 
       return redirect('/profile');

    }

1 个答案:

答案 0 :(得分:0)

首先,您可以检查收到的数据类型

    public function setFlag(Request $request)
{
    dd($request);
    //$user = User::find((int)$request->input('id'));
    //$user->flag = 1;
    //$user->save();
}

如果一切正常,则可以检查用户模型,然后找到相同的ID并更新所需的内容