Laravel - 数据库存储功能不工作

Laravel - Database store function not working

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

这是我的表格:

<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');

我根本没有收到任何错误消息,只是提交按钮没有任何作用。非常感谢!

Clicking submit just seems to refresh the page with no errors to show

您的验证器正在对最后一页执行 return 错误。

将这段代码添加到您的 blade

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

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