How to Create Facebook for Developers for login Laravel 8

How to Create Facebook for Developers for login Laravel 8

Step 1: Create a Post Model and Migration

First, generate a model and migration for the posts.

php artisan make:model Post -m

This will create two files:

  1. Post model in app/Models/Post.php

  2. Migration file in database/migrations/xxxx_xx_xx_create_posts_table.php

Migration: Define the posts table

Open the migration file database/migrations/xxxx_xx_xx_create_posts_table.php and modify it as follows:

// database/migrations/xxxx_xx_xx_create_posts_table.php 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->foreignId('user_id')->constrained()->onDelete('cascade'); // Associate post with user $table->timestamps(); }); } public function down() { Schema::dropIfExists('posts'); } }

Run the migration to create the posts table:

php artisan migrate

Step 2: Define the Post Model

Next, update the Post model to include the relationships and fillable fields.

// app/Models/Post.php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory; protected $fillable = ['title', 'content', 'user_id']; // Relationship with User model public function user() { return $this->belongsTo(User::class); } }

Step 3: Create the Post Controller

Now, create a controller to handle the logic for CRUD operations related to posts.

php artisan make:controller PostController

This will create a controller in app/Http/Controllers/PostController.php. Open it and add the following code:

// app/Http/Controllers/PostController.php namespace App\Http\Controllers; use App\Models\Post; use Illuminate\Http\Request; use Auth; class PostController extends Controller { // Display all posts public function index() { $posts = Post::latest()->get(); return view('posts.index', compact('posts')); } // Show form to create a new post public function create() { return view('posts.create'); } // Store a newly created post public function store(Request $request) { $request->validate([ 'title' => 'required|string|max:255', 'content' => 'required|string', ]); Post::create([ 'title' => $request->title, 'content' => $request->content, 'user_id' => Auth::id(), ]); return redirect()->route('posts.index'); } // Show the form for editing the specified post public function edit(Post $post) { return view('posts.edit', compact('post')); } // Update the specified post public function update(Request $request, Post $post) { $request->validate([ 'title' => 'required|string|max:255', 'content' => 'required|string', ]); $post->update([ 'title' => $request->title, 'content' => $request->content, ]); return redirect()->route('posts.index'); } // Remove the specified post public function destroy(Post $post) { $post->delete(); return redirect()->route('posts.index'); } }

Step 4: Define Routes for Posts

Next, define the routes for handling the post actions. In routes/web.php, add the following:

// routes/web.php use App\Http\Controllers\PostController; Route::resource('posts', PostController::class);

This single resource route declaration will handle all necessary routes for the posts, such as:

  • GET /posts - List posts (index)

  • GET /posts/create - Show form to create a post

  • POST /posts - Store a new post

  • GET /posts/{post}/edit - Show form to edit a post

  • PUT /posts/{post} - Update a post

  • DELETE /posts/{post} - Delete a post

Step 5: Create Views for Posts

Now, create the views to display and manage posts. These files will be located in resources/views/posts/.

resources/views/posts/index.blade.php

This view will list all posts.

@extends('layouts.app') @section('content') <div class="container"> <h1>All Posts</h1> <a href="{{ route('posts.create') }}" class="btn btn-primary">Create Post</a> <div class="mt-3"> @foreach ($posts as $post) <div class="card mb-3"> <div class="card-body"> <h5 class="card-title">{{ $post->title }}</h5> <p>{{ Str::limit($post->content, 150) }}</p> <a href="{{ route('posts.edit', $post->id) }}" class="btn btn-warning">Edit</a> <form action="{{ route('posts.destroy', $post->id) }}" method="POST" style="display: inline;"> @csrf @method('DELETE') <button type="submit" class="btn btn-danger">Delete</button> </form> </div> </div> @endforeach </div> </div> @endsection

resources/views/posts/create.blade.php

This view will show the form to create a new post.

@extends('layouts.app') @section('content') <div class="container"> <h1>Create Post</h1> <form method="POST" action="{{ route('posts.store') }}"> @csrf <div class="form-group"> <label for="title">Title</label> <input type="text" id="title" name="title" class="form-control" required> </div> <div class="form-group mt-3"> <label for="content">Content</label> <textarea id="content" name="content" class="form-control" rows="5" required></textarea> </div> <button type="submit" class="btn btn-primary mt-3">Create Post</button> </form> </div> @endsection

resources/views/posts/edit.blade.php

This view will show the form to edit an existing post.

@extends('layouts.app') @section('content') <div class="container"> <h1>Edit Post</h1> <form method="POST" action="{{ route('posts.update', $post->id) }}"> @csrf @method('PUT') <div class="form-group"> <label for="title">Title</label> <input type="text" id="title" name="title" class="form-control" value="{{ $post->title }}" required> </div> <div class="form-group mt-3"> <label for="content">Content</label> <textarea id="content" name="content" class="form-control" rows="5" required>{{ $post->content }}</textarea> </div> <button type="submit" class="btn btn-primary mt-3">Update Post</button> </form> </div> @endsection

Step 6: Run the Application

After creating these routes and views, you can now run the Laravel development server.

php artisan serve

Navigate to http://localhost:8000/posts in your browser. You should be able to:

  • View a list of posts.

  • Create new posts.

  • Edit and delete posts.

This gives you a complete Laravel blog post system with CRUD functionality. You can further improve this by adding features like authentication, image uploads, categories, or tags. If you need further assistance with any of these features, feel free to ask!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close