Recents in Beach

Fetching Data with Conditions (Eloquent ORM)

Step 1: Import the model and namespace

At the top of your controller, you need to import the Post model and make sure the controller has the correct namespace.

<?php

namespace App\Http\Controllers;

use App\Models\Post; // Import the Post model
use Illuminate\Http\Request; // Import Request if needed

Step 2: Create the controller class

Make your controller extend Laravel’s base Controller class.

class PostController extends Controller
{
    // Controller methods will go here
}

Step 3: Create the index method to fetch all published posts

Here we fetch posts where status is published and pass them to a view.

public function index()
{
    // Fetch all posts with 'published' status
    $publishedPosts = Post::where('status', 'published')->get();

    // Pass the data to a view
    return view('posts.index', ['posts' => $publishedPosts]);
}

Explanation:

  • Post::where('status', 'published')->get() fetches all records where the status column is 'published'.

  • return view('posts.index', ['posts' => $publishedPosts]); sends this data to the posts.index Blade view.

Step 4: Fetch a single post by title

If you want a specific post by title:

public function getByTitle($title)
{
    // Fetch the first post with the given title
    $post = Post::where('title', $title)->first();

    // Check if post exists
    if (!$post) {
        abort(404, 'Post not found.');
    }

    return view('posts.show', ['post' => $post]);
}

Explanation:

  • first() returns the first match or null if not found.

  • abort(404) automatically shows a 404 page if the post does not exist.

Step 5: Fetch a single post by ID

If you want to fetch by primary key (id):

public function show($id)
{
    // Find the post by ID
    $post = Post::find($id);

    // Check if post exists
    if (!$post) {
        abort(404, 'Post not found.');
    }

    return view('posts.show', ['post' => $post]);
}

Explanation:

  • find($id) fetches a record by its primary key.

  • Again, we use abort(404) for error handling.

Step 6: Full controller after all improvements

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        $publishedPosts = Post::where('status', 'published')->get();
        return view('posts.index', ['posts' => $publishedPosts]);
    }

    public function getByTitle($title)
    {
        $post = Post::where('title', $title)->first();

        if (!$post) {
            abort(404, 'Post not found.');
        }

        return view('posts.show', ['post' => $post]);
    }

    public function show($id)
    {
        $post = Post::find($id);

        if (!$post) {
            abort(404, 'Post not found.');
        }

        return view('posts.show', ['post' => $post]);
    }
}

Full Code

<?php

 use App\Models\Post; // Import your model


class PostController extends Controller

{

    public function index()

    {

        // Fetch all posts with a specific status

        $publishedPosts = Post::where('status', 'published')->get();


        // Fetch the first post with a specific title

        $specificPost = Post::where('title', 'My First Post')->first();


        // Fetch a single record by its primary key (ID)

        $post = Post::find($id); // If you have an ID from a route parameter


        // Pass the data to a view

        return view('posts.index', ['posts' => $publishedPosts]);

    }

}


Post a Comment

0 Comments