Blog is not displayed or stored correctly in the database
P粉111641966
P粉111641966 2023-09-01 17:46:54
0
1
510

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:

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

The problem apparently occurs at line 103, public function update: $blog->fill($request); 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粉111641966
P粉111641966

reply all (1)
P粉958986070

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

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

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

Open yourBlog.phpmodel and add the fields you want to batch assign to the array$fillable

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

The second optionis to use theupdatemethod (don’t forget to also add fields to$fillablein the model of the first option because ## The #updatemethod is also a batch assignment field):

$blog->update($request);

The third optionis to manually assign each field one by one, just like you did in thestoremethod:

$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!