Reputation: 1510
I'm working on a Laravel project and I want to create a REST API for a website. On my system, I have two tables:
Blogs and Categories. The table blogs have the category_id column, which is a key that references the column ID in the category table.
Blogs Migration
class CreateBlogsTable extends Migration
{
public function up()
{
Schema::create('blogs', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longtext('body');
$table->string('category_id');
$table->timestamps();
});
}
.....
}
Categories Migration
class CreateCategoriesTable extends Migration
{
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
....
}
My blogs Model
class Blog extends Model
{
protected $fillable = ['title', 'body', 'category_id'];
public function category() {
return $this->hasMany('app\Category');
}
}
My blogs Model
class Category extends Model
{
protected $fillable = ['name'];
public function blog() {
return $this->hasMany('app\Blog');
}
}
So, I created a BlogController and configured the routes to access the corresponding API function.
The api / blogs / via GET is for the index function of my controller, and the function looks like this:
public function index()
{
$blog = Blog::all();
return response()->json($blog, 200);
}
With this, I can get the data from the blogs table
[
{
"id": 1,
"title": "title from my blog",
"text": "text body here",
"category_id": "2",
"created_at": "2018-09-05 21:08:21",
"updated_at": "2018-09-05 21:08:21"
}
]
but I would like to merge the table of blogs and categories, and get a similar response to this
[
{
"id": 1,
"title": "title from my blog",
"text": "text body here",
"category_id": "2",
"created_at": "2018-09-05 21:08:21",
"updated_at": "2018-09-05 21:08:21"
"category": [{
"id": 2,
"name": "Development"
}]
}
]
someone to help?
Upvotes: 0
Views: 2512
Reputation: 163
You are using hasMany relation without a reference id for blog in categories table, so the datas were not eager loaded. If you change the migration as
class CreateCategoriesTable extends Migration
{
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
$table->integer('category_id');
});
}
....
}
and then you can use
Blog::with('category')->get();
Or change the relationship as
public function category(){
return $this->belongsTo(Category::class);
}
and use
Blog::with('category')->get();
Upvotes: 2
Reputation: 1510
I got the solution, I just needed to say that the blog belongs to a category, and a category can have several blogs
class Blog extends Model
{
protected $fillable = ['title', 'body', 'category_id',];
public function category()
{
return $this->belongsTo('App\Category');
}
}
Category Model
class Category extends Model
{
protected $fillable = ['name'];
public function blogs() {
return $this->hasMany('App\Blog');
}
}
So to list blogs with category details
public function index()
{
$blogs = Blog::with('category')->get();
return response()->json($blogs, 200);
}
So to list the categories with your blogs
public function index()
{
$categories = Category::with('blogs')->get();
return response()->json($categories, 200);
}
Upvotes: 0
Reputation: 35377
Load the relationship and it will be included in the serialized response.
$blog = Blog::with('category')->get();
This example is eager loading the category relation.
I suggest you read through the eloquent relationships docs: https://laravel.com/docs/5.6/eloquent-relationships
Upvotes: 0