I need to add data to more than one table with belongsToMany in the project I developed with Laravel filament.
Example DB:
Category: id, name
Anime: id, name, description
AnimeCategory: id, category_id, anime_id
Code:
Anime Resource: https://codeshare.io/3AEg7v
Anime Model: https://codeshare.io/mpydBj
Category Model: https://codeshare.io/lorKBD
AnimeCategory Model: https://codeshare.io/r9A3BY
I am getting categoryId not found error in Anime Table.
Found in Anime Resource:
Select::make('categoryId')
->label('Kategori seçiniz')
->placeholder('Kategori Seçiniz')
->required()
->multiple()
->options(function () {
return Category::all()->pluck('title', 'id');
}),
How can I add selected multi-category id data to AnimeCategory table?
Solution
Anime Model
public function categories()
{
return $this->belongsToMany(Category::class, 'anime_categories', 'animeId', 'categoryId');
}
Anime Resource CategoryId Multiple Select
Select::make('categoryId')
->relationship('categories', 'title')
->label('Kategori seçiniz')
->placeholder('Kategori Seçiniz')
->required()
->multiple()
->preload()
Related
I'm using Laravel 8 Framework. My database is like this:
categories
id - name - category_url
products
id - category_id - products_name - products_url
Database Relationship: categories->id == products->category_id
My question: How can I list the products in the category?
Thanks.
You define your relation in category model like below
public function products()
{
return $this->hasMany('App\Products', 'category_id');
}
And finally in controller you can use with()
Category::with('products')->get();
I have 3 tables:
Events
Ages
Books
Which I was relating them in the following way:
ages
id
name
books
id
title
age_book
id
age_id
book_id
events
id
name
event_age
id
event_id
age_id
event_age_book
event_age_id
book_id
Basically, a book can have many ages, an age can have many books, an event can have many ages, an event age can have many books.
My Event Model looks like this and works fine to get all event ages
class Event extends Model
{
public function ages()
{
return $this->belongsToMany(Age::class, 'event_age');
}
}
Age Model:
class Age extends Model
{
public function books(): BelongsToMany
{
return $this
->belongsToMany(Book::class)
->withTimestamps();
}
}
Book Model
class Book extends Model
{
public function ages(): BelongsToMany
{
return $this
->belongsToMany(Age::class, 'book_age', 'book_id','age_id')
->withTimestamps();
}
}
Im having trouble figuring out how to get all edition age books, is it possible to do it on the Event model?
Let's Review The Relationships
Seems you should only need 3 models, and 5 tables.
Books Belong to Ages
Events Belong to Ages
Ages Belongs to Books
Ages Belongs to Events
Your other models are alright, but your age model is missing a relationship to Event:
class Age extends Model
{
public function books(): BelongsToMany
{
return $this->belongsToMany(Book::class)
->withTimestamps();
}
public function events(): BelongsToMany
{
return $this->belongsToMany(Events::class,'event_age')
->withTimestamps();
}
}
That would allow:
$books->ages;
$events->ages;
$ages->book;
$ages->events;
And chaining...
$books = collect();
foreach($event->ages as $age){
$books->merge($ages->books);
}
$books->unique();
Books and Events
So, I gon't think you want age_event_books. I think what you really want is:
Events belong to Books
Books belong to Events
such that
book_events
- id
- book_id
- event_id
- age_id
And you'd have in book:
public function events()
{
return $this->BelongsToMany('App\Event')
->withTimestamps()
->withPivot('age_id');
}
And in event:
public function books()
{
return $this->belongsToMany('App\Book')->withTimestamps()
->withPiviot('age_id')->groupBy('age_id');
}
Giving you:
$event->books
$book->events
On the Front End
[O]n the frontend i'll need to get the most recent event and group books by age and books can belong to more than one age
$event = Event::latest();
$books = $event->books();
Then on the blade
#foreach($books as $age_id => $books)
<h4>{{Age::find($age_id)->name}}</h4>
#foreach($books as $book)
<div>$book->name</div>
#endforeach
#endforeach
Helpful Tip
You are supplying the relation table, which you have to do because you didn't follow the naming convention for joining tables. The convention is that the classes being joined should be listed alphabetically. So age_event instead of event_age.
I have 3 models:
Tournament, Category, Team
A Tournament hasMany Category
A Category hasMany Team
Tables:
Tournament: Only attributes
Category: id, tournament_id, name
Teams: id, category_id, name
I would like to get all teams from a tournament by: $tournament->teams
I tried :
public function teams()
{
return $this->hasManyThrough(Team::class,CategoryTournament::class);
}
Then I need an extra relation of my team: team->category->name;
but the result of this HasManyThrough has no relationship....
Any Idea???
In Category model :
public function team ()
{
return $this->hasMany('App\Teams');
}
In Teams model :
public function category ()
{
return $this->belongsTo('App\Category', 'category_id');
}
I think should be like this, Try.
According to laravel documentation :
countries
id - integer
name - string
users
id - integer
country_id - integer
name - string
posts
id - integer
user_id - integer
title - string
With this you can add relationship :
public function posts()
{
return $this->hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id');
}
I have two models:
class Order extends Eloquent
{
public function User()
{
return $this->belongsTo('User');
}
public function Product()
{
return $this->belongsToMany('Product');
}
}
and the second one is:
class Product extends Eloquent
{
public function Order()
{
return $this->belongsToMany('Order');
}
}
My question is how can i access a value of second table column using pivot table:
product table:
id
title
image
order table:
id
status
pivot table(order_product):
id
product_id
order_id
I need to access title column of products from orders. for example if a user orders many products in one order I can fetch all product title and show theme.
I don't like use join , instead I like to use Laravel functionality itself.
I found the answer:
$orders = Order::orderBy('created_at', 'DESC')->paginate($page_number);
foreach($orders as $item){
foreach($item->product as $item){
{{$item->title}}<br>
}
}
I can access products from order.
Please any help me that how i pull all the fields from both tables in laravel many to many relationship.
I have two table
categories
id
cname
ctitle
description
products
id
pname
ptitle
pdescription
price
and bridge table
products_to_categories
id
category_id
product_id
So i have pull all the fields from both tables based on product id.
UPD: As commented patricus, the "bridge" table should be called "category_product"
First, you should define the "belongsToMany" relationship in the Product model:
// ...
class Product extends Eloquent {
// ...
function categories()
{
return $this->belongsToMany('Category');
}
}
After it you should load the product by id:
$product = Product::find($product_id);
And now you can access its categories through the "categories" property:
foreach($product->categories as $category)
{
// do what you want with the $category
}
Official docs for M2M relationships:
http://laravel.com/docs/4.2/eloquent#many-to-many