Retrieve post by category id laravel

laravel wherehas
wherebetween laravel
how to display category in laravel
laravel wherehas morph
laravel where null
laravel join eloquent
laravel limit eloquent
laravel many to many sync

I want to retrieve all post with given category, i am developing api, at input i will receive category name, and then i want to retrieve that category by its id in posts table,i want to do this because if some changed category name in the future i dont have to change that in my code i have create like this

PostController.php

  public function getPostsByCategory(Request $request)
{

    $posts=Posts::where('category',$request->category)->get();
    return response()->json(['posts'=>$posts]); 
}

Post model

<?php

 namespace App;

 use Illuminate\Database\Eloquent\Model;

class Posts extends Model
{   

protected $fillable = [
    'post_title','post_description', 'category', 'user_id',
];

Category Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Posts_Category extends Model
{

}

can you please help me with this, please suggest me any idea


You can fetch the category by its name first, and then filter posts by category id

public function getPostsByCategory(Request $request)
{
    $category = Posts_Category::whereName($request->category)->firstOrFail();
    $posts=Posts::where('category',$category->id)->get();
    return response()->json(['posts'=>$posts]); 
}

How to display Posts via the Category, This is my first laravel Application, and my first database based Posts table: Id title body slug Category_id timestamps. Lets say i I want when i go to /​computers to be able to get all the posts that are specific to that category. the code is running without a problem but it’s not retrieving the parent category name or title. when i tried to loop through the data coming from the DB it have the parent id but not the parent category name i need to get the parent category name here is the code for both controller and model. CategoryController.php


i think you are looking for with eager load function

You must store category id not the name of the category..

class Posts extends Model
{   
     protected $fillable = [
        'post_title','post_description', 'category_id', 'user_id',
     ];

     public function category()
     {
          return $this->belongsTo(Posts_Category::class, 'category_id', 'id');
     }
}

use as

$posts = Posts::where('category_id',$request->category)->with('category')->get();

If you really want to search by name

$posts = Posts::whereHas('category', function ($q) {
   $q->where('category_name', 'like', "%{$request->category}%");
})->get();

How to display posts of a certain category by post status , I'm trying to display the posts by category for my CMS, but I want to display $​posts_in_cats = []; foreach($categories as $cat) { $posts_in_cats[$cat->id] Have a look at https://laravel.com/docs/5.4/eloquent-relationships#  If you would like to obtain an instance of a PSR-7 request instead of a Laravel request, you will first need to install a few libraries. Laravel uses the Symfony HTTP Message Bridge component to convert typical Laravel requests and responses into PSR-7 compatible implementations:


you're doing this the wrong way from the get go. best practice is to store category_id in posts table, of course this is only if you need a one-to-many relation between posts and categories table. plus I wouldn't recommend to break the convention of naming models unless you have no other way. I suggest you to read this section more thorough

https://laravel.com/docs/5.6/eloquent-relationships

Fetch latest posts for each category, If we have a blog, sometimes we want to display all latest posts by category. save each post id that we have already collected //in order to avoid to put it twice. Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks.


Please i need a fix solution to this, i want to fetch all the latest post from a specific category in my laravel blog project

These are my models

Category model

    public function posts()
    {
        return $this->belongsToMany('App\Post')->withTimestamps();
    }


Post model

 public function categories()
    {
        return $this->belongsToMany('App\Category')->withTimestamps();
    }

What's they next thing to do?
please help

Get category name from category id laravel, I am very new to laravel and have two table one post table and one category table I have only one category related to one post so i add Become a Laravel Partner. Laravel Partners are elite shops providing top-notch Laravel development and consulting. Each of our partners can help you craft a beautiful, well-architected project.


$posts = Posts::where('category_id', $category->id)->get();

How to get category id of current post?, function catName($cat_id) { $cat_id = (int) $cat_id; $category = &get_category($​cat_id); return $category->name; } function catLink($cat_id) { $category  When injecting a model ID to a route or controller action, you will often query to retrieve the model that corresponds to that ID. Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes.


Display posts in category (Laravel), If you have category model like this: class Category extends Model { public function posts() { return $this->hasMany('\App\Post', 'category_id', 'id'); } } You can​  Today, we are share with you how to get last inserted ID in laravel with 4 simple example. we have needed some it last inserted ID in laravel application. so, here we have find 4 diffrent way to get last inserted ID in laravel. you can use it and get last ID of inserted record in laravel application.


Retrieve post by category id laravel, I want to retrieve all post with given category, i am developing api, at input i will receive category name, and then i want to retrieve that category by its id in posts​  Retrieve data from database using Laravel, Ajax - Learn Retrieve data from database using Laravel and Ajax with complete source code and demo.


Get related posts based on post category, I am trying to get my related posts based on the category id. When i dd($related) it brings me this. Collection {#846 ▽ #items: [] }. it only return that if i don't have  Description # Description. This tag may be used outside The Loop by passing a post id as the parameter. Note: This function only returns results from the default "category" taxonomy.