【问题标题】:Laravel - Database store function not workingLaravel - 数据库存储功能不起作用
【发布时间】:2023-03-12 09:55:01
【问题描述】:

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

这是我的表格:

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

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

【问题讨论】:

  • 这是一个愚蠢的问题,但是在Post 模式下,您的帖子字段是否定义为fillable?另外,请在您的问题中提供php artisan route:list 输出。
  • @zlatan 不幸的是,那里没有批量分配,不过是个好主意
  • $post-&gt;category_id = request('category'); 更改为$post-&gt;category_id = $request-&gt;category; 也更改其他请求();
  • 我在尝试 $request('something') 时遇到错误,请尝试@Zar Ni Ko Ko 的建议
  • 您的表单未设置为处理文件

标签: php laravel crud


【解决方案1】:

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

您的验证器正在返回到错误的最后一页。

将此 sn-p 代码添加到您的刀片

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

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

【讨论】:

  • 这应该是正在发生的事情,OP 的表单至少没有设置为处理文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多