Blog is not displayed or stored correctly in the database
P粉111641966
P粉111641966 2023-09-01 17:46:54
0
1
433
<p>Once I delete a blog, it is completely deleted. I can create a new blog but it won't show up on the website or database. This is my BlogController: </p> <pre class="brush:php;toolbar:false;"><?php namespace App\Http\Controllers; use App\Models\Blog; use Illuminate\Http\Request; class BlogController extends Controller { /*** Display resource list. * * @return \Illuminate\Http\Response*/ public function index() { $blog = Blog::paginate(5); return view('blogs.index', compact('blog')) ->with('i',(request()->input('page',1)-1)*5); } /*** Display the form for creating new resources. * * @return \Illuminate\Http\Response*/ public function create() { return view('blogs.create'); Blog::create($request->all()); return redirect()->route('blogs.index') ->with('success','The blog was created successfully.'); } /*** Store newly created resources in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response*/ public function store(Request $request) { $request->validate([ 'title' => 'required', 'description' => 'required', ]); $blog = new Blog; $blog->title = $request->title; $blog->description = $request->description; $blog->save(); return redirect()->route('blogs.index'); } /*** Display the specified resource. * * @param \App\Blog $blog * @return \Illuminate\Http\Response*/ public function show(Blog $blog) { return view('blogs.show', compact('blog')); } /*** Display the form for editing the specified resource. * * @param \App\Blog $blog * @return \Illuminate\Http\Response*/ public function edit(Blog $blog) { return view('blogs.edit', compact('blog')); } /*** Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Blog $blog * @return \Illuminate\Http\Response*/ public function update(Request $request, Blog $blog) { $request->validate([ 'title' => 'required', 'description' => 'required', ]); // $blog->title = $request->title; // $blog->description = $request->description; $blog->fill($request); //dd($blog); return redirect()->route('blogs.index') ->with('success','Blog updated successfully'); } /** * Remove the specified resource from storage.* * @param \App\Blog $blog * @return \Illuminate\Http\Response */ public function destroy(Blog $blog) { $blog->delete(); return redirect()->route('blogs.index') ->with('success','Blog deleted successfully'); } }</pre> <p>The problem apparently occurs at line 103, public function update: <code> $blog->fill($request);</code> It is neither stored in the database nor in the web page/blog visible. I tried removing that line but got the same result. Nothing changes. I don't understand what the problem could be. Can anyone help? </p>
P粉111641966
P粉111641966

reply all(1)
P粉958986070

First optionIn order for the fill method to work, you must call $blog->save() after this.

$blog->fill($request); 
$blog->save();

Also, when you use the fill method, you are doing a bulk assignment. By default, Laravel protects you from bulk-assigned fields.

Open your Blog.php model and add the fields you want to batch assign to the array $fillable

/**
 * 可以批量赋值的属性。
 *
 * @var array
 */
protected $fillable = [
    'title',
    'description',
];

The second option is to use the update method (don’t forget to also add fields to $fillable in the model of the first option because ## The #update method is also a batch assignment field):

$blog->update($request);

The third option is to manually assign each field one by one, just like you did in the store method:

$blog->title = $request->title;
 
$blog->description = $request->description;

$blog->save();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!