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.
Related
Models:
Products
Variants
VariantValue
// Product.php
public function variants()
{
return $this->belongsToMany(Variant::class, 'product_variant', 'product_id', 'variant_id')
->with([
'values' => function ($builder) {
return $builder->whereHas('products', function ($q) {
return $q->where('products.id', $this->id);
});
},
]);
}
// Variant.php
public function values()
{
return $this->hasMany(VariantValue::class);
}
// VariantValue.php
public function products()
{
return $this->belongsToMany(Product::class, 'product_variant_value', 'variant_value_id', 'product_id');
}
The above works if we do the following:
Product::first()->variants()->get()->toArray()
But, it doe NOT work if we do the following (the values array is empty):
Product::with('variants')->get()->toArray()
Meaning the issue is that the product ID is not available inside the belongs to many query:
return $q->where('products.id', $this->id);
Any idea how I can make this work? The goal is to load relationship using:
Product::with('variants')->get()
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);
}
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.
Get data through belongsToMany relation between the tables:
Users Model:
public function favorites()
{
return $this
->belongsToMany('App\job', 'favourites', 'user_id', 'job_id')
->withTimestamps();
}
Job Model:
public function favorites()
{
return $this
->belongsToMany(job::class, 'favourites', 'job_id', 'user_id')
->withTimestamps();
}
Controller:
public function index()
{
$jobs = Auth::user()->favorites;
return view('home',compact('jobs'));
}
Nothing appears any errors and any data.
Check your Job model. Your relationship is pointing to the same (Job) model. Change it to this:
public function favorites()
{
return $this
->belongsToMany(User::class, 'favourites', 'job_id', 'user_id')
// ^^^^^^^^^^^^
->withTimestamps();
}
I am not able to paginate in laravel in this situation
return $this->hasMany('App\News','category_id')->orderBy('id','desc')->paginate(20);
but says error. in controller I have also tried
$byCategories=Category::findOrFail($id)->paginate(20);`
it also says error.
help me.
My Model is
class Category extends Model
{
public function news()
{
return $this->hasMany('App\News','category_id')->orderBy('id','desc');
}
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
public function user()
{
return $this->belongsTo('App\User');
}
public function getRouteKeyName()
{
return 'slug';
}
}
another model one is
class News extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
public function category()
{
return $this->belongsTo('App\Category');
}
}
my controller code is
public function byCategory($id)
{
$byCategories=Category::findOrFail($id);
return view('back-end/news/byCategory', compact('byCategories'));
}
Thank you so much.
Pagination is not done in the Model
it is to be done in the controller, For example remove (->paginate(20);) from here
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
keep it only as
public function newsMany()
{
return $this->belongsToMany('App\News');
}
and call the pagination in controller when returning the view
$news=Category::findOrFail($id)->newsMany()->paginate(20);
return view('view name', compact('news'));