J'ai cree un blog mais sur ma page d'acceuill jai 28 request je sais pas pourqui or jai Iderloger

class Post extends Model
{
    use HasFactory;

    protected $with = ['category'];
    }

public function index(Request $request)
    {

        $posts = Post::query()->with(['tags', 'category']);

        if ($search = $request->search)
        {
            $posts->where('title', 'LIKE', '%'. $search .'%')
            ->orWhere('description', 'LIKE', '%'. $search .'%');
        }

        return view('posts.index', [
            'posts' => $posts->latest()->paginate(10),
            'categories' => Category::all(),
        ]);
    }

Je veux moins de request comment faire svp ?



namespace App\Http\Controllers;

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

class PostController extends Controller
{

    public function index(Request $request)
    {

        $posts = Post::query()->with(['tags', 'category']);

        if ($search = $request->search)
        {
            $posts->where('title', 'LIKE', '%'. $search .'%')
            ->orWhere('description', 'LIKE', '%'. $search .'%');
        }

        return view('posts.index', [
            'posts' => $posts->latest()->paginate(10),
            'categories' => Category::all(),
        ]);
    }

    public function show(string $slug, Post $post)
    {
        if ($post->slug !== $slug) {
            return to_route('posts.show', ['slug' => $post->slug, 'post' => $post->id]);
        }
        return view('posts.show', [
            'post' => $post,
        ]);
    }

    public function postByCategory(Category $category)
    {
        return view('posts.index', [

            // 'posts' => $category->posts()->paginate(10),
            'categories' => Category::all(),
            'posts' => Post::query()->where('category_id', $category->id)->latest()->paginate(10),
        ]);
    }
}

1 réponse


Bonjour ,

Peux-tu partager ta vue post.index ?

Que donne un dd($posts) ?
Est-ce que les relations sont bien présentes ?

Merci.