Laravel many to many relation not working as expected - php

this might be a silly question, but I am trying to connect 1 table to 2 related tables.
Inside the values array component_id 1 shows up when requesting data for component with id 2.
The relation inside Component::class
public function fields()
{
return $this->hasManyThrough(Field::class, FieldValue::class, 'component_id', 'id', 'id', 'field_id');
}

Maybe you make mistake, try determine relationships like that:
public function fields()
{
return $this->hasManyThrough(Field::class, FieldValue::class);
}
OR
public function fields()
{
return $this->hasManyThrough(Field::class, FieldValue::class, 'component_id', 'field_id', 'id', 'id');
}
And of course, check correct your code reading official documentation how to use this relationship.

Component::
public function fields()
{
return $this->hasMany(Field::class);
}
public function values()
{
return $this->hasManyThrough(Field::class, FieldValue::class);
}
Field:: (table) id,component_id
public function component()
{
return $this->belongsTo(Component::class);
}
public function values()
{
return $this->hasMany(FieldValue::class);
}
FieldValue:: (table)id,field_id
public function field()
{
return $this->belongsTo(Field::class);
}

Related

Laravel Eloquent. I try to intersect two intermediate table. Is there any query more efficient than this?

Laravel Eloquent. I try to intersect two intermediate table. Is there any query more efficient than this ?
Unit Model :
public function products()
{
return $this->belongsToMany('App\Product');
}
public function users()
{
return $this->belongsToMany('App\User');
}
Product Model :
public function units()
{
return $this->belongsToMany('App\Unit');
}
public function users()
{
return $this->belongsToMany('App\User');
}
User Model :
public function units()
{
return $this->belongsToMany('App\Unit');
}
public function products()
{
return $this->belongsToMany('App\Product');
}
Query Eloquent :
Product::get()
->filter(function ($product) {
return $product->units
->pluck('id')
->intersect(auth()->user()->units->pluck('id'))
->isNotEmpty();
})
I try to retrieve all product where product unit is equal user login unit
UPDATE
I think code below is more clean
Product::whereHas('units', function (Builder $query) {
$query->where([
'units.id' => auth()->user()->units->pluck('id'),
]);
})->get();
I think your solutions looks optimal! It looks like a way to compare attributes from two pivot tables in the way
Product::with('users')
->with('units')
->compareAttributesFromPivotTables('attribute_a','attribute_b')
->get();
isn't yet implemented.

Laravel morphedByMany Query Returns NULL

I have this model
class Permission extends Model
{
public function details(): MorphToMany
{
return $this->morphedByMany('App\Models\Details', 'model', 'model_has_permissions', 'permission_id', 'model_id');
}
}
class Details extends Model
{
public function permission()
{
return $this->morphedByMany('App\Models\Permission','model','model_has_permissions','model_id','permission_id');
}
}
I'm execute this query
Details::with('permission')->find(55);
and got empty array
why happen this?and what is the correct query?
You have a typo in your permission() method
change this
return $this->morphedByMany('App\Models\Permission','model','.model_has_permissions','model_id','permission_id');
to this
return $this->morphedByMany('App\Models\Permission','model','model_has_permissions','model_id','permission_id');
I don't think it's possible to chain find after with. Here are your options.
Lazy Loading.
Details::find(55)->load('permissions');
Eager loading with where clause
Details::with('permissions')->where('id', 55)->get();
UPDATE
Shouldn't this be morphToMany?
public function details(): MorphToMany
{
return $this->morphedByMany('App\Models\Details', 'model', 'model_has_permissions', 'permission_id', 'model_id');
}
Or this?
public function permission()
{
return $this->morphedByMany('App\Models\Permission','model','model_has_permissions','model_id','permission_id');
}

2 foreign keys on one column in laravel 5.2

here's my database schema
and I have these models:
Admin
User
Bet
Match
Team
I'm confused how to define the relationShip between matches and teams in models
here Is what I did till now...
User.php
public function bets()
{
return $this->hasMany('\App\Bet');
}
Bet.php
public function user()
{
return $this->belongsTo('\App\User');
}
public function match()
{
return $this->belongsTo('\App\Match');
}
Match.php
public function bets()
{
return $this->hasMany('\App\Bet');
}
//?????????????
Team.php
//?????????????
actually what I need Is the code that should be placed instead of //???... in both Team.php and Match.php so that I can easily do such things...
$team->matches();
$match->team1();
$match->team2();
thanks
It should be something like this:
Match.php
public function team1()
{
return $this->belongsTo('\App\Team', 'team1_id');
}
public function team2()
{
return $this->belongsTo('\App\Team', 'team2_id');
}
Team.php
public function matches()
{
return $this->hasMany('\App\Match', 'team1_id')
->orWhere('team2_id', $this->id);
}
You can specify which column should be targeted for each relationship:
public function team1() {
return $this->belongsTo('\App\Match', 'team1_id');
}
public function team2() {
return $this->belongsTo('\App\Match', 'team2_id');
}
Let me know if this helps.
It would be something like this. Give it a try.
Match.php
public function team1(){
return $this->belongsTo('App\Team', 'team1_id');
}
public function team2(){
return $this->belongsTo('App\Team', 'team2_id');
}
Team.php
public function matches1(){
return $this->hasMany('App\Match', 'team1_id', 'id');
}
public function matches2(){
return $this->hasMany('App\Match', 'team2_id', 'id');
}

Laravel - belongsToMany getting all records, not ::find(1)

I trying to create relation between some tables in DB, so I do:
Accreditation:
Model
public function access() {
return $this->belongsToMany( 'App\Sections' );
}
AccreditationAccess:
Model
public function acreditation() {
return $this->belongsToMany( 'App\Accreditations' );
}
public function section() {
return $this->belongsToMany( 'App\Sections' );
}
Sections:
Model
public function access() {
return $this->belongsToMany( 'App\Accreditations' );
}
Now I want to display accreditation with section so I do:
App\Accreditations::find(1)->with('access')->get();
but I getting all accreditations without sections - why?
You should use this:
App\Accrediations::with('access.section')->find(1);
Because ->with('access')->get();returns collection of Accrediations with access relations.

When to use getXXAttribute vs a relationship in Laravel

I am putting together a model of mine and I am wondering what is the most relevent method to use for something like "author".
I have:
public function images() {
return $this->morphMany('App\Models\Image', 'imageable');
}
public function ratings() {
return $this->hasMany('App\Models\Rating');
}
public function favorited() {
return $this->hasMany('App\Models\Favorite');
}
public function author() {
return $this->hasOne('User');
}
public function getMyFavoriteAttribute() {
return $this->favorited->where('user_id', Auth::user()->id)->count();
}
public function getFavoritesAttribute() {
return $this->favorited->count();
}
public function getRatingAttribute() {
return $this->ratings->sum('rating');
}
I have created a relationship called author as seen above. When would it be preferred to create this as a getAuthorAttribute?
get__Attribute is only necessary if you want to manipulate the original value of the stored attribute. Due to the fact that Author is already accessible through your relation it's not necessary to add an additional getter.

Categories