Once I delete a blog, it is deleted completely. I can make a new one, but it does not show on the website or in the database. This is my BlogController:
<?php
namespace App\Http\Controllers;
use App\Models\Blog;
use Illuminate\Http\Request;
class BlogController extends Controller
{
/**
* Display a listing of the resource.
*
* @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);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('blogs.create');
Blog::create($request->all());
return redirect()->route('blogs.index')
->with('success','Blog created successfully.');
}
/**
* Store a newly created resource 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'));
}
/**
* Show 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');
}
}
And the problem is apparently the 103th line, public function update: $blog->fill($request);
It does not store in the database nor is it visible on the webpage/blog. I tried deleting that specific line, but it is the same. Nothing changes. I do not understand what the issue could be. Can someone help?
CodePudding user response:
1ST OPTION In order for the fill
method to work, you must call $blog->save()
after that.
$blog->fill($request);
$blog->save();
Also when you use fill
method you are mass assigning values. By default Laravel protects you from mass assigning fields.
Open your Blog.php
model and add the fields you want to mass assign to the array $fillable
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'description',
];
2ND OPTION is to use the update
method (don't forget to also add fields in $fillable
in the model from 1st option since update
method is also mass assigning fields):
$blog->update($request);
3RD OPTION 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();
CodePudding user response:
The fill
function dose not update data in database.
This function only assignment value to attribute in protected $fillable = ['title','description'];
in Blog model.
Using update
function to update data to database.
$blog->fill($request);
$blog->update();
CodePudding user response:
In your create function, you must the $request as parameter make only one return. The first return is not neccessay