管理我的 CRUD 的资源控制器卡在“更新”部分,我不明白为什么。我尝试更改为 body->nullable();但这没有用。下面是我的前端设计的图片:
PostsController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Models\Post;
// use DB;
class PostsController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth', ['except' => ['index', 'show']]);
}
/**
* Display a listing of the resource.
*/
public function index()
{
// $posts = Post::all();
// return $post = Post::where('title', 'Post Two')->get();
// $posts = DB::select('SELECT * FROM posts');
// $posts = Post::orderBy('created_at', 'desc')->take(1)->get();
// $posts = Post::orderBy('created_at', 'desc')->get();
$posts = Post::orderBy('created_at', 'desc')->paginate(10);
return view('posts/index')->with('posts', $posts);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('posts/create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
// Validating Create Post form requirements
$this->validate($request, [
'title' => 'required',
'description' => 'required',
'body' => 'required',
'cover_image' => 'image|max:1999'
]);
// Handle File Upload
if($request->hasFile('cover_image')) {
// Get filename with the extension
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
//get just ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
// Upload the Image
$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
// Creating a new Post
$post = new Post;
$post->title = $request->input('title');
$post->description = $request->input('description');
$post->body = $request->input('body');
$post->user_id = auth()->id();
if ($request->hasFile('cover_image')) {
$post->cover_image = $fileNameToStore;
}
$post->save();
return redirect('/posts')->with('success', 'Posts Created');
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
$post = Post::find($id);
return view('posts/show')->with('post', $post);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$post = Post::find($id);
// Check for correct user
if (auth()->user()->id !== $post->user_id) {
return redirect('/posts')->with('error', 'Unauthorized Page');
}
return view('posts/edit')->with('post', $post);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
// Handle File Upload
if($request->hasFile('cover_image')) {
// Get filename with the extension
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
//get just ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
// Upload the Image
$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
}
$post = Post::find($id);
$post->title = $request->input('title');
$post->description = $request->input('description');
$post->body = $request->input('body');
$post->user_id = auth()->user()->id;
if ($request->hasFile('cover_image')) {
$post->cover_image = $fileNameToStore;
}
$post->save();
return redirect('/posts')->with('success', 'Post Updated');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$post = Post::find($id);
// Check for correct user to prevent unauthorized editing
if (auth()->user()->id !== $post->user_id) {
return redirect('/posts')->with('error', 'Unauthorized Page');
}
if ($post->cover_image != 'noimage.') {
//Delete
Storage::delete('public/cover_images/'.$post->cover_image);
}
$post->delete();
return redirect('/posts')->with('success', 'Post Removed');
}
}
这是错误的屏幕截图:
我尝试过更改第 133 行:
$post->body = $request->input('body');
对此:
$post->body = $request->input('body')->nullable();
这是编辑表格。我正在使用CKeditor5:
编辑帖子{{-- The Editor --}}
{!! Form::open(['action' => ['App\Http\Controllers\PostsController@update', $post->id], 'enctype' => 'multipart/form-data']) !!}
<div class="form-group" style="margin-bottom: 20px;">
{{ Form::text('title', $post->title, ['class' => 'form-control', 'placeholder' => 'Title']) }}
</div>
<div class="form-group" style="margin-bottom: 20px;">
{{ Form::text('description', $post->description, ['class' => 'form-control', 'placeholder' => 'Description']) }}
</div>
<div class="form-group">
{{ Form::file('cover_image') }}
</div>
<div class="form-group">
{{ Form::textarea('body', '', ['class' => 'form-control', 'style' => 'margin-top: 20px;']) }}
</div>
{{ Form::hidden('_method', 'PUT')}}
{{ Form::submit('Submit', ['class' => 'btn btn-primary', 'style' => 'width: 100px; height: 40px; margin-top: 20px;']) }}
{{ Form::submit('Delete', ['class' => 'btn btn-danger', 'style' => 'width: 100px; height: 40px; margin-top: 20px;']) }}
{!! Form::close() !!}
这是帖子模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'body',
];
//Table Name
protected $table = 'posts';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;
public function user() {
return $this->belongsTo('App\Models\User');
}
}
@lagbox 在评论中为我指明了正确的方向。
我完全忘记修改表单来请求数据。它与模型或控制器无关。检查一下你的 JavaScript 人员!
表格:
<div class="form-group">
{{ Form::textarea('body', '', ['class' => 'form-control', 'style' => 'margin-top: 20px;']) }}
</div>
至:
<div class="form-group">
{{ Form::textarea('body', $post->body, ['class' => 'form-control', 'style' => 'margin-top: 20px;']) }}
</div>