Laravel Nova: Pivot Table with 3 relationships - laravel

in my Laravel Nova project I have this Models:
- Ricetta
-id
- Ingrediente
-id
- UnitaMisura
-id
- RicettaHasIngrediente (many to many, pivot table)
-ricetta_id
-ingrediente_id
-qta
-unita_misura_id
Code:
class Ricetta extends Model
{
public function ingrediente()
{
return $this->belongsToMany('\App\Models\Ingrediente', 'ricetta_has_ingrediente', 'ricetta_id', 'ingrediente_id')->withPivot('qta', 'unita_misura_id');
}
}
class Ingrediente extends Model
{
public function ricetta()
{
return $this->belongsToMany('\App\Models\Ricetta', 'ricetta_has_ingrediente', 'ingrediente_id', 'ricetta_id')->withPivot('qta', 'unita_misura_id');
}
}
class UnitaMisura extends Model
{
public function ricettaHasIngrediente()
{
return $this->belongsTo('\App\Models\ricettaHasIngrediente', 'unita_misura_id', 'id');
}
}
class RicettaHasIngrediente extends Model
{
public function unitaMisura()
{
return $this->belongsTo('\App\Models\UnitaMisura', 'unita_misura_id', 'id');
}
}
I think this structure is correct.
With Laravel Nova, I have defined all the fields and the attributes of the pivot table, but I can't use the unita_misura_id:
...
BelongsToMany::make('Ingrediente')->fields(function () {
return [
Number::make('Quantità', 'qta'),
HasMany::make('unitaMisura', 'unitaMisura')
];
}),
...
When I open the form qta value; unita_misura is not displayed.
Can you help me?
thanks!

Related

Laravel Eloquent HasManyThrough through 3 tables with pivot tables

I need to make a list of scopes from my positions->areas->scopes on my Booking Model.
My tables look like that:
Booking
id
...
Position
id
booking_id
...
Area
id
..
Position_areas
id
area_id
position_id
Scope
id
...
Area_Scopes
id
area_id
scope_id
And this are my relations:
class Booking extends Model
{
...
public function positions()
{
return $this->hasMany(BookingPosition::class);
}
public function areas()
{
return $this->hasManyThrough(Area::class, PositionsAreas::class, 'area_id', 'id', 'position_id', 'area_id');
}
...
}
class BookingPosition extends Model
{
...
public function booking()
{
return $this->belongsTo(Booking::class);
}
public function areas()
{
return $this->belongsToMany(Area::class, 'position_areas', 'position_id', 'area_id')
->using(PositionsAreas::class);
}
...
}
class PositionsAreas extends Pivot
{
...
protected $table = 'position_areas';
public function positions(){
return $this->belongsTo(BookingPosition::class);
}
public function areas(){
return $this->belongsTo(Area::class);
}
...
}
class Area extends Model
{
...
public function bookingPositions()
{
return $this->belongsToMany(
BookingPosition::class
)->using(PositionsAreas::class);
}
public function scopes()
{
return $this->belongsToMany(Scope::class, table: 'scope_areas');
}
...
}
class Scope extends Model
{
...
public function areas(){
return $this->belongsToMany(Area::class, table: 'scope_areas');
}
...
}
And I want to have a list of all areas on my booking model, but I don't know how to achieve that.
So that I can do something like that
...
$booking->load('scopes');
[
id
date
...
scopes => [
{...},
{...}
]
]
I tried to create pivot models for position_areas but i cant even get a list of areas on my booking model.
I couldn't figure out how to solve this with a relation like hasManyThrough but as workaround I make all scopes available in my $bookings like that.
$booking = Booking::find($booking->id);
$booking->scopes = $booking->positions
->pluck('areas')
->flatten()
->pluck('scopes')
->flatten()
->pluck('name')
->unique()
->values()
->all();

create Update Actions for pivot table attribute on laravel nova belongsToMany Relationship

I have model looks like this folowing Product model code:
public class Product extends Model
{
public function types()
{
return $this->belongsToMany(Type::class)
->withPivot('published');
}
}
and here the Types model:
public class Type extends Model
{
public function products()
{
return $this->belongsToMany(Product::class)
->withPivot('published');
}
}
in nova ProjectResource I have this following field :
BelongsToMany::make('Types')
->fields(function() {
return [
Boolean::make('Published')
];
})->actions(function() { return new Action/UpdateProductTypeActions }),
in nova TypeResource I have this following field :
BelongsToMany::make('Projects')
->fields(function() {
return [
Boolean::make('Published')
];
}),
and I wanna make this published attribute from pivot table updateable using Nova Actions
and I already create UpdateProductTypeActions like this following code:
public function handle(ActionFields $fields, Collection $models)
{
foreach ($models as $model) {
}
}
my questions how to get product_id from those actions?
This will give you all products that have many-to-many relations with types which have many-to-many relations with $model that here is your project:
foreach($model->types as $type)
{
$products=$type->products->pluck(id);
}

Laravel 8.x, 3 models and many to many relationship

I am new to laravel and trying the following:
I have these tables:
disciplines: id | name
specialties: id | name
categories: id | name
discipline_specialty (pivot table): id | discipline_id | specialties_id
Discipline model:
public function specialties()
{
return $this->belongsToMany(Specialty::class);
}
Specialty model:
public function disciplines()
{
return $this->belongsToMany(Discipline::class);
}
My question is:
how can I relate (many to many) the categories to the pivot table discipline_specialty in order to access the category name with the discipline and specialty ids?
I had thought of an additional pivot table that linked category id and discipline_specialty id but I don't know if it's the best solution and how to do it. Do you have any suggestions? Any help is appreciated.
You can introduce a junction/pivot model that will relate these 3 relations as many-to-one/belongsTo and one-to-many/hasMany from Discipline/Speciality/Category.
Discipline Speciality Category
\\ || //
\\ || //
DisciplineSpecialityCategory
This DisciplineSpecialityCategory model will have following attributes or FKs
Table: discipline_speciality_category
discipline_id
speciality_id
category_id
Now you model definitions will be like
class Discipline extends Model
{
public function disciplineSpecialityCategory()
{
return $this->hasMany(DisciplineSpecialityCategory::class, 'id', 'discipline_id');
}
}
class Speciality extends Model
{
public function disciplineSpecialityCategory()
{
return $this->hasMany(DisciplineSpecialityCategory::class, 'id', 'speciality_id');
}
}
class Category extends Model
{
public function disciplineSpecialityCategory()
{
return $this->hasMany(DisciplineSpecialityCategory::class, 'id', 'category_id');
}
}
class DisciplineSpecialityCategory extends Model
{
public function discipline()
{
return $this->belongsTo(Discipline::class, 'id', 'discipline_id');
}
public function speciality()
{
return $this->belongsTo(Speciality::class, 'id', 'speciality_id');
}
public function category()
{
return $this->belongsTo(Category::class, 'id', 'category_id');
}
}

How to get data from table related through pivot table?

I have 4 tables: countries, activities, country_activities and packages.
countries and activities are related through pivot table country_activity, and packages is related to country_activity.
Now, How do I eager load all packages related to each activity in a country?
class Country extends Model
{
public function activities() {
return $this->belongsToMany('App\Models\Activity','country_activities')->using('App\Models\CountryActivity')->as('country_activities');
}
}
class Activity extends Model
{
public function countries() {
return $this->belongsToMany('App\Models\Country','country_activities')->using('App\Models\CountryActivity')->as('country_activities');
}
}
class Package extends Model
{
public function country_activities() {
return $this->belongsToMany('App\Models\CountryActivity');
}
}
class CountryActivity extends Pivot
{
protected $table = 'country_activities';
public function packages() {
return $this->hasMany('App\Models\Package');
}
}
So, this worked for me.
class Country extends Model
{
public function activities() {
return $this->belongsToMany('App\Models\Activity','country_activities')->using('App\Models\CountryActivity')->withPivot(['id'])->as('country_activities');
}
Now, In my controller, I do this
$country = Country::with(['activities'=> function($q) {$q->where('name','Trekking');}])->where('name','Nepal')->first(['id','name']);
$country->activities->map(function ($i){
$i->country_activities->load('packages');
return $i;
});
I did something similar in a project i worked on. I'm not sure it will work but it's worth the shot:
$country = Country::find(1);
$country->activities = $contry->activities()->get()->each(function ($i, $k){
$i->packages = $i->pivot->packages;
//$i->makeHidden('pivot'); -> This is useful if you want to hide the pivot table
});
var_dump($country);

join table in laravel

I have 3 table in laravel:
class General extends Mode
{
public function populars()
{
return this->hasMany('App\Popular');
}
}
enter code here
and
class Popular extends Model
{
public function general()
{
return this->belongsTo('App\General');
}
}
and
class Specific extends Model
{
public function popular(){
return this->belongsTo('App\Popular');
}
}
...
how to join tables and return this list result:
1. generals
2.popular
3.Specific
I assume Popular has many Specific, you could add another mapping in Popular model as
class Popular extends Model
{
public function general()
{
return this->belongsTo('App\General');
}
public function specific()
{
return this->hasMany('App\Specific');
}
}
Doing with eloquent way you could write it as
$generals = General::with('populars.specific')->get();
Using query builder you could join them as
DB::table('general as g')
->join('popular as p', 'g.id','=','p.general_id')
->join('specific as s', 'p.id','=','p.popular_id')
->get();

Resources