I am making an axios request to the address I specified below with Laravel.
I want the incoming data to come as follows.
I want to list the data with language en from the categories table and list the data matching (language) with the category_id in the pages table, but I was not successful. Where am I doing wrong?
http://localhost:3000/categories?lang=en
Mysql Table & Model
Mysql Table
SELECT * FROM `categories`
id
title
slug
language
Model
class Category extends Model
{
use HasFactory;
public function pages(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Page::class, 'category_id', 'id');
}
}
Mysql Table & Model
Mysql Table
SELECT * FROM `pages`
id
title
slug
content
category_id
language
Model
class Page extends Model
{
use HasFactory;
}
Routing
Route::prefix('categories')->group(function () {
Route::get('', [CategoryController::class, 'index']);
});
CategoryController
public function index()
{
return $category = Category::where('language', 'en')->with('pages')->get();
}
output
[
{
"id": 2,
"title": "corporate",
"slug": "corporate",
"seo": null,
"order": "0",
"status": "1",
"language": "en",
"created_at": "2022-04-04T13:31:27.000000Z",
"updated_at": "2022-04-04T13:31:27.000000Z",
"pages": null
},
{
"id": 4,
"title": "new category",
"slug": "new -category",
"seo": null,
"order": "0",
"status": "1",
"language": "en",
"created_at": "2022-04-08T12:23:57.000000Z",
"updated_at": "2022-04-08T12:23:57.000000Z",
"pages": null
}
]
what i want
[
{
"id": 2,
"title": "corporate",
"slug": "corporate",
"seo": null,
"order": "0",
"status": "1",
"language": "en",
"created_at": "2022-04-04T13:31:27.000000Z",
"updated_at": "2022-04-04T13:31:27.000000Z",
"pages": null,
"children": [
{
"id": "1",
"title": "hello word",
"slug": "hello-word",
"content": "",
"category_id": "2",
"language": "en"
}
]
},
{
"id": 4,
"title": "new category",
"slug": "new -category",
"seo": null,
"order": "0",
"status": "1",
"language": "en",
"created_at": "2022-04-08T12:23:57.000000Z",
"updated_at": "2022-04-08T12:23:57.000000Z",
"pages": null,
"children:": []
}
]
Assuming a page has one category, and a category has many pages, your relationship seems to be incorrect.
Change it to:
class Category extends Model
{
use HasFactory;
public function pages(): \Illuminate\Database\Eloquent\Relations\hasMany
{
return $this->hasMany(Page::class, 'category_id', 'id');
}
}
and in page model, add:
class Page extends Model
{
use HasFactory;
public function category(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Category::class, 'id', 'category_id');
}
}
then you should be able to use ->with('pages') on your Category model
Related
I have a many to many relation with table users,items and the pivot table user_item and i need to call the query : Select * from user_item where user_id=$user->id in laravel and return the results in json format. I try with
$user=User::find(session('user_id'))->items()->get();
return response ()->json($user);
But it doesn't work. How can I do that?
class User extends Authenticatable {
public function items (){
return $this->belongsToMany ("App\Models\Item", "user_item", "user", "item");
}
}
class Item extends Models {
public function users (){
return $this->belongsToMany ("App\Models\User", "user_item", "item", "user");
}
}
You can define columns in your pivote table using withpivot method
public function items (){
return $this->belongsToMany ("App\Models\Item", "user_item",
"user", "item")->withPivot(['column1', 'column2','another_column']);
}
to get relation instead of using get(), you should use like below:
$user=User::find(session('user_id'))->items;
return response ()->json($user);
above will give below json result:
[{
"id": 4,
"name": "PC",
"pivot": {
"column1": 1,
"column2": 4,
"another_column": "2016-03-03"
}
},
{
"id": 5,
"name": "Phone",
"pivot": {
"column1": 1,
"column2": 4,
"another_column": "2016-03-03"
}
}]
you can also include pivot in use user model using with():
$user=User::with('items')->find(session('user_id'));
return response ()->json($user);
give json result something like:
{
"id": 1,
"name": "User Name",
"email": "email#user.com",
"created_at": null,
"updated_at": null,
"items": [{
"id": 4,
"name": "PC",
"pivot": {
"column1": 1,
"column2": 4,
"another_column": "2016-03-03"
}
},
{
"id": 5,
"name": "Phone",
"pivot": {
"column1": 1,
"column2": 4,
"another_column": "2016-03-03"
}
}]
}]
I have attempted to achieve to pull the user's info and the product details (Which specified the nickname and name) when the product name equal to "Oops" but I have no idea why the getProducts does not return any things.
User Model
public function getProducts(){
return $this->hasMany('App\Models\Product','users_id');
}
Product Model
public function users(){
return $this->belongsTo(User::class);
}
The code of pulling data:
$products = User::with(['getProducts' => function($query){
$query->select("users_id","name","nickname");
}])->get();
The current output:
"data": [
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"get_products": [
]
},
{
"id": 2,
"name": "Kelvin Ooi",
"email": "kelvin.ooi#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-13T12:07:11.000000Z",
"role": 1,
"get_products": [
{
"nickname":"MCD",
"name":"Oops"
},
{
"nickname":"Mary Brown",
"name":"Oops"
},
{
"nickname":"Kentucy",
"name":"KFC"
},
{
"nickname":"Texas Chicken",
"name":"TXS"
}
]
}
]
The expected output
"data": [
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"get_products": [
]
},
{
"id": 2,
"name": "Kelvin Ooi",
"email": "kelvin.ooi#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-13T12:07:11.000000Z",
"role": 1,
"get_products": [
{
"nickname":"MCD",
"name":"Oops"
},
{
"nickname":"Mary Brown",
"name":"Oops"
}
]
}
]
getProducts is not a good name for the relationship, lets simply call it products.
public function products()
{
return $this->hasMany('App\Models\Product','users_id');
}
Your code in the comment doesn't work because you specify the where clause in the main query, not the sub query.
return User::with(['products' => function ($query) {
$query->select("users_id","name","nickname");
}])
// This got to be in the sub query.
->where("products.name","Oops")
->get();
So let's update your code to this:
$productName = 'Oops';
return User::with(['products' => function ($query) use ($productName) {
$query->select("users_id","name","nickname", "price")
->where("name","LIKE", "%{$productName}%");
}])
->get();
I have seen your comment to this answer. Let's define a total custom attribute for the User model:
class User extends Model
{
protected $appends = ['total'];
public function getTotalAttribute()
{
// This is higher order message, if you haven't used it: https://laravel.com/docs/8.x/collections#higher-order-messages
return $this->products->sum->price;
}
}
Then the total attribute will be part of any user.
I have categories with id, parent_id, slug , pivot table category_language which columns are id,category_id,language_id,value
As you can see I can translate parent category, but can't send desired $lang_id to children translations, so each children having all translations
here is what I get:
{
"id": 1,
"parent_id": 0,
"slug": "personal-computers",
"created_at": "2019-12-27 15:05:31",
"updated_at": "2019-12-27 15:05:31",
"children": [
{
"id": 3,
"parent_id": 1,
"slug": "accessories-for-pc",
"created_at": "2019-12-27 15:05:32",
"updated_at": "2019-12-27 15:05:32",
"translations": [
{
"id": 1,
"code": "en",
"name": "English",
"pivot": {
"category_id": 3,
"language_id": 1,
"value": "Acc for PC",
"id": 7
}
},
{
"id": 2,
"code": "ru",
"name": "Русский",
"pivot": {
"category_id": 3,
"language_id": 2,
"value": "Аксессуары для ноутбуков и ПК",
"id": 8
}
},
{
"id": 3,
"code": "ro",
"name": "Romana",
"pivot": {
"category_id": 3,
"language_id": 3,
"value": "aksessuari-dlya-noutbukov-i-pk-ro",
"id": 9
}
}
]
}
],
"translations": [
{
"id": 1,
"code": "en",
"name": "English",
"pivot": {
"category_id": 1,
"language_id": 1,
"value": "PC",
"id": 1
}
}
]
}
Controller:
return Category::with('children')
->with(array('translations'=>function($query) use ($lang_id){
$query->where('language_id',$lang_id);
}))
->where('parent_id',0)->first();
Model
class Category extends Model
{ ..
public function translations()
{
return $this->belongsToMany('App\Models\Translation','category_language', 'category_id' ,'language_id' )->withPivot('value','id');
}
public function children()
{
return $this->hasMany( 'App\Models\Category' , 'parent_id' , 'id' )->with('translations');
}
}
you can add condition in children method
public function children()
{
return $this->hasMany( 'App\Models\Category' , 'parent_id' , 'id' )->with('translations')->where('language_id', 1);
}
Dry7 answer was close to the one I've implemented later, so I upvoted him.
Finally in model I've added: ...->where('language_id',helper_SetCorrectLangIdForQuery());
and function helper_SetCorrectLangIdForQuery is using global helper of Laravel request()->lang . If lang=enz, than it takes default language from another helper.
I am working on a system where I need many users can have many accounts and they can have multiple roles per account.
How can I set that up in eloquent.
I am currently having in User model:
public function accounts() {
return $this->belongsToMany('App\Models\Account');
}
public function roles() {
return $this->belongsToMany('App\Models\Account')->withPivot('role');
}
I have a pivot table user_account where the role is defined.
My problem is that when I go
$user = User::with('accounts')
->with('roles')
->where('id', $user['id'])
->first();
My output is:
{
"id": 1,
"name": "John Doe",
"created_at": "2018-03-11 20:46:46",
"updated_at": "2018-03-11 20:46:46",
"accounts": [
{
"id": 1,
"name": "Acme Inc",
"type": "BUSINESS",
"created_at": "2018-03-11 20:46:46",
"updated_at": "2018-03-11 20:46:46",
"pivot": {
"user_id": 1,
"account_id": 1
}
},
{
"id": 1,
"name": "Acme Inc",
"type": "BUSINESS",
"created_at": "2018-03-11 20:46:46",
"updated_at": "2018-03-11 20:46:46",
"pivot": {
"user_id": 1,
"account_id": 1
}
}
],
"roles": [
{
"id": 1,
"name": "Acme Inc",
"type": "BUSINESS",
"created_at": "2018-03-11 20:46:46",
"updated_at": "2018-03-11 20:46:46",
"pivot": {
"user_id": 1,
"account_id": 1,
"role": "SYSTEMADMINISTRATOR"
}
},
{
"id": 1,
"name": "Acme Inc",
"type": "BUSINESS",
"created_at": "2018-03-11 20:46:46",
"updated_at": "2018-03-11 20:46:46",
"pivot": {
"user_id": 1,
"account_id": 1,
"role": "USER"
}
}
]
}
What I want is for the roles to contain just the data from the pivot table with user_id, account_id and the role name. Any pointers? I would also like the accounts output to contain just one account.
Through extensive searching on the net I actually found a solution.
You need to make an AccountUser model and add the role to the account_user table.
In the User class you set up these relations:
public function roles() {
return $this->hasMany('\App\Models\AccountUser');
}
public function accounts() {
return $this->hasManyThrough('\App\Models\Account', '\App\Models\AccountUser', 'id', 'id');
}
And in the AccountUser class
public function user() {
return $this->belongsTo('App\Models\User');
}
public function account() {
return $this->belongsTo('App\Models\Account');
}
And in the Account class
public function accountUser() {
return $this->hasMany('\App\Models\AccountUser');
}
Now I can do
$user = User::with('roles')
->with('accounts')
->where('email', $data['email'])
->first();
And I get my expected output :)
Hope this helps someone...
My Village model;
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Village extends Model {
public function positions() {
return $this->belongsTo(Map::class, 'id', 'field_id');
}
}
My Map class migration;
Schema::create('map_data', function (Blueprint $table) {
$table->increments('id');
$table->integer('field_type');
$table->integer('field_id');
$table->string('x');
$table->string('y');
$table->timestamps();
});
My "villages" methon on "VillageController" class;
public function villages() {
$villages = Village::with([
'positions' => function ($query) {
$query->select('x', 'y');
}
])->get();
return $villages;
}
Result;
{
"villages": [
{
"id": 1,
"name": "village 1",
"created_at": "2016-10-26 18:36:34",
"updated_at": "2016-10-26 18:36:34",
"positions": null
},
{
"id": 2,
"name": "village 2",
"created_at": "2016-10-26 18:36:34",
"updated_at": "2016-10-26 18:36:34",
"positions": null
}
]
}
"Select" method only needs to be mentioned, but not the column returns NULL.
If I delete the $query->select('x', 'y'); code returns the following results.
{
"villages": [
{
"id": 1,
"name": "village 1",
"created_at": "2016-10-26 18:36:34",
"updated_at": "2016-10-26 18:36:34",
"positions": {
"id": 1,
"field_type": "1",
"field_id": "1",
"x": "21",
"y": "21",
"created_at": "2016-10-26 18:36:34",
"updated_at": "2016-10-26 18:36:34"
}
},
{
"id": 2,
"name": "village 2",
"created_at": "2016-10-26 18:36:34",
"updated_at": "2016-10-26 18:36:34",
"positions": {
"id": 2,
"field_type": "1",
"field_id": "2",
"x": "0",
"y": "0",
"created_at": "2016-10-26 18:36:34",
"updated_at": "2016-10-26 18:36:34"
}
}
]
}
But I use the $query->select('x'); code and the result should be the following
resource: https://stackoverflow.com/a/32185643/3287225
When you try to eagerly load positions relation for villages with
Village::with(['positions'])->get();
3 things happen:
Eloquent loads all villages
Eloquent loads all positions
Eloquent assigns positions to corresponding Village objects using the field_id column
In order for it to work, fetched positions need to have field_id column fetched, otherwise Eloquent is unable to match them with their corresponding Villages.
When you do
$query->select('x', 'y');
you're fetching only x and y columns from your positions table. field_id column is not fetched, that's why Eloquent is unable to fetch them with Village objects and that's why you're getting null in instead of a collection of positions.
Replace
$query->select('x', 'y');
with
$query->select('field_id', 'x', 'y');
to make your code work as expected.