我正在尝试将图像上传到预先存在的表单,但是在提交表单后,我得到了一个SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
错误。
我设法在表单上获取实际的按钮和文件选择器,但是,提交后,它会引爆上述错误。关于此问题的任何帮助或资源都将是很棒的。
PostController
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::all();
return view('post.index', compact('posts'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$post = new Post();
return view('post.create', compact ('post'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = request()->validate([
'title' => 'required',
'body' => 'required',
]);
$post = \App\Post::create($data);
return redirect('/posts');
}
/**
* Display the specified resource.
*
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
return view('post.show', compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
return view('post.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
$post->update($this->validatedData());
$this->storeImage($post);
return redirect('/posts');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
$post->delete();
return redirect('/posts');
}
private function validatedData()
{
return request()->validate([
'title' => 'required',
'body' => 'required',
'image' => 'sometimes|file|image|max:5000',
]);
}
private function storeImage($post)
{
if (request()->has('image')){
$post->update([
'image' => request()->image->store('uploads', 'public'),
]);
$image = Image::make(public_path('storage/' . $post->image))->fit(500, 500, null, 'top-left');
$image->save();
}
}
}
表格
<div class="card-body">
<form action="/posts/create" method="POST" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="title" aria-describedby="titleHelp" placeholder="Enter Title" autocomplete="off">
<small id="titleHelp" class="form-text text-muted">Give your post a title that will describe your post easily</small>
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea type="text" name="body" cols="30" rows="3" class="form-control" id="body" aria-describedby="bodyHelp" placeholder="Enter Post Body" autocomplete="off"></textarea>
<small id="bodyHelp" class="form-text text-muted">Enter as much detail you'd like!</small>
</div>
<div class="form-group d-flex flex-column">
<label for="image">Post Image</label>
<input type="file" name="image" class="py-2">
<div>{{ $errors->first('image') }}</div>
</div>
<button type="submit" class="btn btn-primary">Create Post</button>
@if (session('message'))
<div>
{{ session('message') }}
</div>
@endif
</form>
发布模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $guarded = [];
}
最后迁移
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->string('title');
$table->longText('body');
$table->string('image')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
我认为默认情况下user_id没有默认值。在您的帖子模型中,
protected $fillable = ['user_id', 'title', 'body', 'image'];
还需要在商店功能中包括当前登录用户中的用户。
use Auth;
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'body' => 'required',
]);
$post->user_id = Auth::user()->id;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect('/posts');
}