Laravel-数据库存储功能不起作用

时间:2019-12-05 08:11:07

标签: php laravel crud

输入数据并弄清楚为什么我的数据没有存储到数据库时,我遇到了麻烦。我尝试使用资源路由和自定义路由代码,但似乎仍然没有任何效果。单击提交似乎只是刷新页面,没有错误显示

这是我的表格:

<div class="container">
  <div class="row">
    <div class="col-md-12">                            
        <nav class="navbar navbar-expand-sm navbar-dark primary justify-content-between">
            <div class="navbar-brand text-dark">Create a Story</div>
        </nav>
        <hr class="mt-3">   
        <form action="{{route('story.store')}}" method="post">
            @csrf
            <div class="row">
                <div class="col col-lg-6">
                    <div class="form-group my-3">
                        <label for="title">Title</label><br>
                        <input type="text" name="title" id="title" class="w-100 form-control{{ $errors->has('title') ? ' is-invalid' : '' }}" value="{{ old('title') }}" required>
                        @if ($errors->has('title'))
                            <span class="invalid-feedback" role="alert"><strong>{{ $errors->first('title') }}</strong></span>
                        @endif
                    </div>
                </div>
                <div class="col col-lg-6">
                    <div class="form-group my-3">
                        <label for="category">Category</label><br>
                        <select name="category" id="category" class="w-100 form-control{{ $errors->has('category') ? ' is-invalid' : '' }}" value="{{ old('category') }}" required>
                            <option value="Active" selected>Select Category</option>
                            @foreach ($category as $item)
                                <option value="{{$item->id}}">{{$item->nama}}</option>
                            @endforeach
                        </select>
                        @if ($errors->has('category'))
                            <span class="invalid-feedback" role="alert"><strong>{{ $errors->first('category') }}</strong></span>
                        @endif
                    </div>
                </div>
                <div class="col col-lg-6">
                    <div class="form-group my-3">
                        <label for="thumbnail">Story Thumbnail <span class="text-muted">(Recomended size is 650 x 350 pixel)</span></label><br>
                        <input type="file" name="thumbnail" id="thumbnail">
                        @if ($errors->has('thumbnail'))
                        <span class="invalid-feedback" role="alert"><strong>{{ $errors->first('thumbnail') }}</strong></span>
                        @endif                            
                    </div>
                </div>

                <div class="col col-lg-12">                                
                    <div class="form-group mt-4">
                        <textarea id="text-content"  name="content" class="w-100 form-control{{$errors->has('content') ? ' is-invalid' : ''}}" id="content" rows="3" value="{{old('content')}}"></textarea>
                        @if ($errors->has('content'))
                        <span class="invalid-feedback" role="alert"><strong>{{ $errors->first('content') }}</strong></span>
                        @endif
                    </div>
                </div>
            </div>

            <div class="modal-footer pb-0">
                <div class="float-right">                        
                    <button type="submit" class="btn btn-outline-primary btn-md">Publish Story</button>
                </div>
            </div>
        </form>                               
    </div>
</div>

控制器中的存储功能:

public function store(Request $request)
{                
    $user = Auth::user();
    $request->validate([
        'title' => 'required|string',
        'category' => 'required',
        'thumbnail' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',            
        'content' => 'required',
    ]);

    $thumbnailName = time().'.'.request()->thumbnail->getClientOriginalExtension();                

    $post = new Post;
    $post->title = request('title');
    $post->category_id = request('category');
    $post->thumbnail = request('thumbnail');
    $post->content = request('content');
    $post->title = request('title');
    $post->date = date('d-m-Y');
    $post->author_id = $user->id;
    $post->save();

    $request->thumbnail->move(asset('storage/uploads'), $thumbnailName);
    return redirect('me/stories')->with('Succes', 'Story has been published')->with('thumbnail', $thumbnailName);                    
}

这是路线:

Route::get('me/stories', 'PostController@index')->name('story');
Route::get('me/stories/create', 'PostController@create')->name('story.create');
Route::post('me/stories/store', 'PostController@store')->name('story.store');
Route::post('ck/image_upload', 'PostController@imageUpload')->name('ck.upload');

我根本没有收到任何错误消息,只是提交按钮什么都不做。非常感谢!

1 个答案:

答案 0 :(得分:1)

  

点击提交似乎刷新了页面,没有错误显示

您的验证程序正在返回错误的最后一页。

将此代码段添加到刀片中

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

此外,在提交之前检查页面,然后转到“网络”选项卡(在顶部检查“保留日志”),然后继续进行提交,您将获得更多有关发生的情况的详细信息。