I have a question about eloquent in laravel.
DB tables
categories: id root_id name
products id name etc..
product_categories id product_id category_id
So it might be CategoryA which has a child CategoryB and CategoryB by itself has a child CategoryC.
When I click in CategoryA I want to find all products that are part of CategoryA, CategoryB, CategoryC
Category Model
public function cats()
{
return $this->hasMany(Category::class);
}
public function childrenCategories()
{
return $this->hasMany(Category::class)->with('cats');
}
Product Model
public function categories()
{
return $this->belongsToMany(Category::class);
}
Controller
//firsty I get all id of all categories, subcategories of all levels.
$categories = Category::where('category_id',$category_id)->with('childrenCategories')->get();
$all_cat_array=array();$all_prod=array();
foreach ($categories as $category)
{
foreach ($category->childrenCategories as $childCategory) {
array_push($all_cat_array, $childCategory->id);
}
array_push($all_cat_array,$category->id);
}
//then i get all id of products
foreach ($all_cat_array as $cat)
{
if(CategoryProduct::where('category_id',$cat)->exists()) {
$prod=CategoryProduct::where('category_id',$cat)->pluck('product_id');
array_push($all_prod,$prod );
}
}
But I don't want to use of all these foreach, since I want to optimise the code.
What can I do to make it simplier ???
For more information please read Laravel Docs
DATABASE
Category A database
Schema::create('category_as', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('product_id');
...
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});
Category B database
Schema::create('category_bs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('category_a_id');
...
$table->timestamps();
$table->foreign('category_a_id')->references('id')->on('category_as')->onDelete('cascade');
});
MODEL
Products model
public function categories_a() {
return $this->hasmany(Category_a::class);
}
Category A model
public function categories_b() {
return $this->hasmany(Category_b::class);
}
public function product() {
return $this->belongsTo(Product::class);
}
Category B model
public function category_a() {
return $this->belongsTo(Category_a::class);
}
public function clients() {
return $this->hasManyThrough(Client::class, Code::class);
}
CONTROLLER
public function index() {
$products = Product::all();
$query = $products->categories_a->categories_b;
dd($query);
}
EDITED
DATABASE
Category A database
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('product_id');
...
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});
MODEL
Products model
public function categories() {
return $this->hasmany(Category::class);
}
Category model
public function product() {
return $this->belongsTo(Product::class);
}
CONTROLLER
public function index() {
$products = Product::all();
$query = $products->categories;
dd($query);
}
I recommend you to use nested set structure in Category model.
It is well implemented in this package https://github.com/lazychaser/laravel-nestedset.
You could get products of category and all it's descendants (with unlimited nesting) with 2 queries.
If Category belongsToMany Product:
$categoryIds = Category::descendantsAndSelf($categoryId)->pluck('id');
$products = Product::whereHas('categories', function ($query) use ($categoryIds) {
$query->whereIn('categories.id', $categoryIds);
});
If Category hasMany Product
$categoryIds = Category::descendantsAndSelf($categoryId)->pluck('id');
$products = Product::whereIn('category_id', $categoryIds);
Related
I've a payment gateway, I want to access the product items after returning, so I created several tables Payments, Orders, OrderItems and configured the relationships between them.
Each Payment is for one Order and each Order contains several OrderItem and each OrderItem contains one product. Now I want to access each Product through an object of the Payment Model, which returns the following error.
Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found:
1054 Unknown column 'orders.order_item_id' in 'field list' (SQL:
select products.*, orders.order_item_id as laravel_through_key
from products inner join orders on orders.id =
products.order_id where orders.order_item_id = 68 limit 1)
$product_order_item = $payment->order->order_items->map(function ($order_item) {
dd($order_item->product);
});
Payment Model:
protected $table = "payments";
protected $guarded = [];
public function order() {
return $this->belongsTo(Order::class);
}
Order Model:
protected $table = "orders";
protected $guarded = [];
public function payment() {
return $this->hasOne(Payment::class);
}
public function order_items() {
return $this->hasMany(OrderItem::class);
}
OrderItem Model:
protected $table = "order_items";
protected $guarded = [];
public function order() {
return $this->belongsTo(Order::class);
}
public function product() {
return $this->hasOneThrough(Product::class, Order::class);
}
Product Model:
protected $table = "products";
protected $guarded = [];
public function order_items() {
return $this->belongsToMany(OrderItem::class);
}
That is migrations:
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE');
$table->unsignedInteger('amount');
$table->unsignedInteger('res_code');
$table->enum('status', ['paid', 'unpaid']);
$table->timestamps();
});
=====================================================
Schema::create('order_items', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('order_id');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('CASCADE');
$table->unsignedBigInteger('product_id');
$table->foreign('product_id')->references('id')->on('products')->onDelete('CASCADE');
$table->unsignedInteger('price');
$table->timestamps();
});
===========================================================
Schema::create('payments', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('order_id');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('CASCADE');
$table->enum('status', ['paid', 'unpaid']);
$table->unsignedInteger('ref_id');
$table->unsignedInteger('res_id');
$table->enum('gateway', ['idpay', 'zarinpal']);
$table->timestamps();
});
what is the problem?
Thanks in advance...
You should use belongsTo for the product relationship of OrderItem Model
OrderItem Model:
public function product() {
return $this->belongsTo(Product::class);
}
https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-inverse
And hasMany for the order_items relationship of Product Model
Product Model:
public function order_items() {
return $this->hasMany(OrderItem::class);
}
https://laravel.com/docs/8.x/eloquent-relationships#one-to-many
using these tables I need to make category filter
Food Table: Migration
$table->bigIncrements('id');
$table->unsignedBigInteger('category_id')->nullable();
$table->unsignedBigInteger('image_id')->nullable();
$table->string('name')
$table->float('price',9,2)->default(0);
$table->float('offer_price',9,2)->nullable()->default(0);
$table->boolean('visible')->default(0);
$table->timestamps();
categories table: Migration
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('parent_id')->nullable();
$table->unsignedBigInteger('image_id')->nullable();
$table->boolean('visible')->default(0);
$table->timestamps();
});
Food Model relation
public function category()
{
return $this->belongsTo(Category::class, 'category_id')->withDefault();
}
Category Model relation
public function food()
{
return $this->hasMany(Food::class)->orWhereIn('category_id' , $this->subcategory()->pluck('id')->toArray())->visible();
}
public function subcategory()
{
return $this->hasMany(self::class, 'parent_id', 'id');
}
````[When clicking on any category it should show food, that related to the category I clicked on ][1]
[1]: https://i.stack.imgur.com/zpVb8.png
you can simply do this in your controller
public function index(Request $request)
{
$query=Food::query();
// filter by categoryid
if ($request->has('category_id')){
$query->whereCategoryId($request->get('category_id'));
}`
// sort and exexute
$query=$query->orderBy('id','DESC');
return response($query);
}
Having multiple tables with data that relates to each other, i'm trying to display that data in a view using Laravel.
I must be confused with how Laravel runs its queries and I need help to sort how to do, what in PHP&SQL would be a left join.
My Asset Model:
public function category(){
return $this->hasOne(Category::class);
}
My Category Model:
public function asset()
{
return $this->belongsTo(Asset::class);
}
My Country Model:
public function country()
{
return $this->belongsTo(Asset::class);
}
And my AssetsController:
public function asset($id)
{
$asset = Asset::find($id);
return view('admin.assets.asset')->with('assets', $asset);
}
And the Router:
Route::get('/admin/assets/asset/{id}', [
'uses' => 'AssetsController#asset',
'as' => 'assets.asset'
//Show the Asset
]);
And the View:
<p><strong>Price:</strong>{{$assets->price}} €</p>
<p><strong>Description:</strong>{{$assets->description}}</p>
<p><strong>Country:</strong>{{$assets->country}}</p>
<p><strong>Category:</strong>{{$assets->name}}</p>
So in the 'asset.blade.php' I get the id from a previous index.blade.php that has a list of all the assets. I want to get via the ID, an asset page that displays the category name and the country name, instead of the ID that belongs to the Asset table.
So it should echo something like $assets->country->country_name and $assets->category->name
dd($asset);
EDIT: Additional information about migrations
categories_table migration
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('url');
$table->timestamps();
});
}
assets_table migration:
public function up()
{
Schema::create('assets', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->double('price',15,2);
$table->mediumText('description');
$table->integer('country');
$table->integer('category');
$table->integer('subcategory');
$table->integer('subsubcategory');
$table->integer('broker');
$table->string('featured');
$table->string('slug');
$table->timestamps();
});
}
Got it working.
class Category
{
public function asset()
{
return $this->hasMany(Asset::class);
}
}
class Asset
{
public function category()
{
return $this->belongsTo(Category::class);
}
}
use App\Asset;
use App\Category;
use App\Country;
use App\Subcategory;
use Illuminate\Http\Request;
class AssetController extends Controller
{
public function asset($id)
{
$asset = Asset::find($id);
return view('admin.assets.asset')
->with('asset', $asset)
->with('category', Category::all())
->with('subcategory', Subcategory::all())
->with('country', Country::all());
}
}
The other models have the same relationship towards the Asset model and vice-versa.
The View:
<p><strong>Category:</strong>{{$asset->category->name}}</p>
<p><strong>Sub-Category:</strong>{{$asset->subcategory->name}}</p>
It now shows the name matching the id of the corresponding tables.
I hope this thread find everyone healthy.
What is wrong with the following code? I am getting an error on my many to many relationship. For the past 4 hours i am stuck with this problem. I would be very thankful if you find the bug or source of problem.
Here is my code and tables:
Schema::create('products', function(Blueprint $table)
{
$table->increments('id');
$table->integer('category');
$table->string('thumbnail');
$table->string('images');
$table->string('logo');
$table->string('main_photo');
$table->timestamps();
});
//categories table
Schema::create('product_categories', function(Blueprint $table)
{
$table->increments('id');
$table->string('category');
});
//relationship table
Schema::create('product_productcategory', function(Blueprint $table)
{
$table->increments('id');
$table->integer('product_id'); // the id of the bear
$table->integer('productcategories_id'); // the id of the picnic that this bear is at
});
products model:
class Product extends Model
{
protected $table = 'products';
public function product_category() {
return $this->belongsToMany('App\product_category', 'App\product_productcategory', 'product_id', 'productcategories_id');
}
}
And pivot model
class product_category extends Model
{
protected $table = 'product_categories';
public function product() {
return $this->belongsToMany('App\Product', 'App\product_productcategory', 'productcategories_id', 'product_id');
}
}
Thanks in advance!
Updates: The problem is solved thanks to #mimo.
But now i want to translate the categories in different languages so i created a table like this:
Schema::create('category_translations', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('product_categories')->onDelete('cascade');
$table->integer('locale_id')->unsigned();
$table->foreign('locale_id')->references('id')->on('locales')->onDelete('cascade');
$table->unique(['category_id', 'locale_id']);
});
And my product categories with new relationship to the translations
class product_category extends Model
{
protected $table = 'product_categories';
public function product() {
//return $this->belongsToMany('App\Product', 'App\product_productcategory', 'productcategories_id', 'product_id');
return $this->belongsToMany('App\Product', 'product_productcategory');
}
public function translation($lang) {
return $this->hasMany('App\Category_Translation')->where('locale_id', '=', $lang);
}
}
And in category translation:
class Category_Translation extends Model
{
protected $table = 'category_translations';
public function product_category() {
return $this->belongsTo('App\product_category');
}
}
And finally my Product:
class Product extends Model
{
protected $table = 'products';
public function translation($lang) {
return $this->hasMany('App\Product_Translation')->where('locale_id', '=', $lang);
}
public function product_category() {
//return $this->belongsToMany('App\product_category', 'App\product_productcategory', 'product_id', 'productcategories_id');
//return $this->belongsToMany('App\product_category', 'product_productcategory');
return $this->belongsToMany('App\product_category', 'product_productcategory')->translation(1);
}
}
Now when i run:
$product = App\Product::find(1)->first();
echo $product->product_category;
I get error:
Call to undefined method Illuminate\Database\Query\Builder::translation()
Your migration file is wrong:
//relationship table
Schema::create('product_productcategory', function(Blueprint $table)
{
$table->integer('product_id')->unsigned()->index(); // the id of the bear
$table->foreign('product_id')->references('id')->on('products');
$table->integer('productcategories_id')->unsigned()->index(); // the id of the picnic that this bear is at
$table->foreign('productcategories_id')->references('id')->on('product_categories');
});
This code will made the relationship on the database level.
When you have 12 min free time you can check out this tutorial about many to many relationships in laravel:
https://laracasts.com/series/laravel-5-fundamentals/episodes/21
Update:
you need to update your relationships too:
class Product extends Model
{
protected $table = 'products';
public function product_category() {
return $this->belongsToMany('App\product_category', 'product_productcategory');
}
}
and:
class product_category extends Model
{
protected $table = 'product_categories';
public function product() {
return $this->belongsToMany('App\Product', 'product_productcategory');
}
}
I have a many to many relationship between products and a product_categories tables. Now I want to create a translation table for categories.
But how do I relate them in eloquent? i am learning database relationships so forgive my ignorance.
Here is my tables:
//languages
Schema::create('locales', function(Blueprint $table)
{
$table->increments('id');
$table->string('code', 2);
$table->string('name');
});
//products
Schema::create('products', function(Blueprint $table)
{
$table->increments('id');
$table->string('thumbnail');
$table->timestamps();
});
//categories
Schema::create('product_categories', function(Blueprint $table)
{
$table->increments('id');
$table->string('category');
});
//relationship table
Schema::create('product_productcategory', function(Blueprint $table)
{
$table->integer('product_id')->unsigned()->index(); // the id of the bear
$table->foreign('product_id')->references('id')->on('products');
$table->integer('product_category_id')->unsigned()->index(); // the id of the picnic that this bear is at
$table->foreign('product_category_id')->references('id')->on('product_categories');
});
//category translations
Schema::create('category_translations', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('product_categories')->onDelete('cascade');
$table->integer('locale_id')->unsigned();
$table->foreign('locale_id')->references('id')->on('locales')->onDelete('cascade');
$table->unique(['category_id', 'locale_id']);
});
Product Model:
class Product extends Model
{
protected $table = 'products';
public function product_category() {
return $this->belongsToMany('App\product_category', 'product_productcategory')->translation(1);
}
}
Product categories:
class product_category extends Model
{
protected $table = 'product_categories';
public function product() {
return $this->belongsToMany('App\Product', 'product_productcategory');
}
}
Category Translations
class Category_Translation extends Model
{
protected $table = 'category_translations';
public function product_category() {
return $this->belongsTo('App\product_category');
}
}
But when i run:
$product = App\Product::find(1)->first();
echo $product->product_category;
I get error:
Call to undefined method Illuminate\Database\Query\Builder::translation()
You should not call translation in the Product model category relation (because you still interacting with the query builder), instead you should access the translation as a property on the category object:
echo $product->product_category->first()->getTranslatedIn(1)->name;
So you could refactor your code like this:
Product Model:
class Product extends Model
{
protected $table = 'products';
public function product_category() {
return $this->belongsToMany('App\product_category', 'product_productcategory');
}
}
Product categories:
class product_category extends Model
{
protected $table = 'product_categories';
public function product() {
return $this->belongsToMany('App\Product', 'product_productcategory');
}
public function translations() {
return $this->hasMany('App\Category_Translation', 'category_translations');
}
// relation filtered by language (cannot be accessed as a property unless you define the parameter as optional)
public function translatedIn($language) {
return $this->hasOne('App\Category_Translation', 'category_id')->where('locale_id', $language);
}
// access translated object
public function getTranslatedIn($language) {
return $this->translatedIn($language)->getResults();
}
}