Now, find Post.php model file inside Laravel8Crud/app/Models directory. And open it then add the fillable property code into Post.php file, like following:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title', 'description'
];
}
Then, find create_posts_table.php file inside Laravel8Crud/database/migrations/ directory. Then open this file and add the following code into function up() on this file:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description');
$table->timestamps();
});
}
Now, open again your terminal and type the following command on cmd to create tables into your selected database:
php artisan migrate
Step 4 – Create Routes
In step 4, open your web.php file, which is located inside routes directory. Then add the following routes into web.php file:
use App\Http\Controllers\PostCRUDController;
Route::resource('posts', PostCRUDController::class);
Step 5 – Creating Resource Controller
In step 5, create resource controller by using the following command:
So open PostCRUDController.php file and add the following code into it:
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostCRUDController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$data['posts'] = Post::orderBy('id','desc')->paginate(5);
return view('posts.index', $data);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* 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',
]);
Post::create($request->all());
return redirect()->route('posts.index')
->with('success','Post has been created successfully.');
}
/**
* Display the specified resource.
*
* @param \App\post $post
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
return view('posts.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('posts.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)
{
$request->validate([
'title' => 'required',
'description' => 'required',
]);
$post->update($request->all());
return redirect()->route('posts.index')
->with('success','Post updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Post $post
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
$post->delete();
return redirect()->route('posts.index')
->with('success','Post has been deleted successfully');
}
}
Step 6 – Create Blade Views File
In step 6, create one directory name posts inside resource/views directory. Then create new 3 blade views file that named create.blade.php, edit.blade.php and index.blade.php inside resources/views/posts directory.
The following code display error message in laravel add and edit blog post forms. So do not forget to add the following code laravel add edit blog posts forms:
@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
Don’t worry i have already added the validation error message display code along with each form fields.
So, you can add the following php and html form code into create.blade.php, edit.blade.php and index.blade.php: