Today we will learn how to create a fundamental crud application using laravel 8. To create a basic crud application we will work in view files, controller, modal, and migration files and create routes in routes.php.
Let’s start building the first crud application in laravel 8 by setting the project using composer. Below is the command to create a laravel project using the composer.
How to perform Laravel 8 CRUD Application operations
[code]composer create-project –prefer-dist laravel/laravel myproject [/code]
Then, you need to create a new database and update the .env file with your database credentials.
The next step is to, create a new migration for the table you want to use for your Laravel 8 CRUD operations. For example, if you want to create a posts table, run the below command:
[code]php artisan make:migration create_posts_table
[/code]
In the create_posts_table migration file, define the columns for your table using the Schema facade:
[code]
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create(‘posts’, function (Blueprint $table) {
$table->id();
$table->string(‘title’);
$table->text(‘content’);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists(‘posts’);
}
}
[/code]
Run the migration to create the posts table in your database:
[code]php artisan migrate
[/code]
Next, create a new model for your Post:
[code]php artisan make:model Post
[/code]
In the Post model file, define the table name and fillable columns:
[code]
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $table = ‘posts’;
protected $fillable = [‘title’, ‘content’];
}
[/code]
Create a new controller for your CRUD operations:
[code]php artisan make:controller PostController –resource
[/code]
In the PostController file, define the methods for each CRUD operation:
[code]
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
$posts = Post::all();
return view(‘posts.index’, compact(‘posts’));
}
public function create()
{
return view(‘posts.create’);
}
public function store(Request $request)
{
$post = new Post;
$post->title = $request->title;
$post->content = $request->content;
$post->save();
return redirect()->route(‘posts.index’);
}
public function show(Post $post)
{
return view(‘posts.show’, compact(‘post’));
}
public function edit(Post $post)
{
return view(‘posts.edit’, compact(‘post’));
}
public function update(Request $request, Post $post)
{
$post->title = $request->title;
$post->content = $request->content;
$post->save();
return redirect()->route(‘posts.index’);
}
public function destroy(Post $post)
{
$post->delete();
return redirect()->route(‘posts.index’);
}
}
[/code]
Create the views for your CRUD operations:
resources/views/posts/index.blade.php:
[code]
<h1>Posts</h1>
<a href=”{{ route(‘posts.create’) }}”>Create New Post</a>
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Content</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->content }}</td>
<td>
<a href=”{{ route(‘posts.show’, $post) }}”>View</a>
<a href=”{{ route(‘posts.edit’, $post) }}”>Edit</a>
<form action=”{{ route(‘posts.destroy’, $post) }}” method=”POST”>
@csrf
@method(‘DELETE’)
<button type=”submit”>Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
[/code]
resources/views/posts/create.blade.php:
[code]
<h1>Create Post</h1>
<form action=”{{ route(‘posts.store’) }}” method=”POST”>
@csrf
<label for=”title”>Title:</label><br>
<input type=”text” id=”title” name=”title”><br>
<label for=”content”>Content:</label><br>
<textarea id=”content” name=”content”></textarea><br>
<button type=”submit”>Create</button>
</form>
[/code]
resources/views/posts/show.blade.php:
[code]
<h1>{{ $post->title }}</h1>
<p>{{ $post->content }}</p>
<a href=”{{ route(‘posts.edit’, $post) }}”>Edit</a>
<form action=”{{ route(‘posts.destroy’, $post) }}” method=”POST”>
@csrf
@method(‘DELETE’)
<button type=”submit”>Delete</button>
</form>
[/code]
resources/views/posts/edit.blade.php:
[code]
<h1>Edit Post</h1>
<form action=”{{ route(‘posts.update’, $post) }}” method=”POST”>
@csrf
@method(‘PUT’)
<label for=”title”>Title:</label><br>
<input type=”text” id=”title” name=”title” value=”{{ $post->title }}”><br>
<label for=”content”>Content:</label><br>
<textarea id=”content” name=”content”>{{ $post->content }}</textarea><br>
<button type=”submit”>Update</button>
</form>
[/code]
Finally, define the routes for your CRUD operations in routes/web.php:
[code]
use App\Http\Controllers\PostController;
Route::get(‘/’, [PostController::class, ‘index’])->name(‘posts.index’);
Route::get(‘/posts/create’, [PostController::class, ‘create’])->name(‘posts.create’);
Route::post(‘/posts’, [PostController::class, ‘store’])->name(‘posts.store’);
Route::get(‘/posts/{post}’, [PostController::class, ‘show’])->name(‘posts.show’);
Route::get(‘/posts/{post}/edit’, [PostController::class, ‘edit’])->name(‘posts.edit’);
Route::put(‘/posts/{post}’, [PostController::class, ‘update’])->name(‘posts.update’);
Route::delete(‘/posts/{post}’, [PostController::class, ‘destroy’])->name(‘posts.destroy’);
[/code]
Hurray !!!!!
That’s it! You now have a basic Laravel 8 CRUD example.