Attaching another table after a hasManyThrough relationship - php

i am trying to connect 4 tables together:
StepAnimation Model:
public function steps()
{
return $this->hasManyThrough('App\Models\Step','App\Models\WorkSequence');
}
public function step()
{
return $this->belongsTo('App\Models\Step');
}
Step Model:
public function workSequence()
{
return $this->belongsTo('App\Models\WorkSequence');
}
public function stepAnimation()
{
return $this->hasMany('App\Models\StepAnimation');
}
WorkSequence Model:
public function categoryWorkSequence()
{
return $this->hasMany('App\Models\CategoryWorkSequence');
}
public function step()
{
return $this->hasMany('App\Models\Step');
}
CategoryWorkSequence Model:
public function workSequence()
{
return $this->belongsTo('App\Models\WorkSequence');
}
I am currently selecting all the work sequences that have a step animation:
$this->stepAnimation = New StepAnimation();
$workSequence = $this->stepAnimation::with('step.workSequence')->get();
But now id like to attach all the categories to the worksequence, how do i need to extend the select for that to happen?

Then you can continue selecting the category relationship in your workSequence as long as it is available on your model. Like this.
$this->stepAnimation = New StepAnimation();
$workSequence = $this->stepAnimation::with('step.workSequence.categoryWorkSequence')->get();

Related

How can I get the combined query for two hasManyThrough

I have two relationships on the users' table:
public function tokens_records()
{
return $this->hasManyThrough(Record::class, Token::class);
}
and
public function websites_records()
{
return $this->hasManyThrough(Record::class, Website::class);
}
How can I merge both queries into one?
If you want to have the records for both relationships, this is gonna work:
class Entity extends Model
{
public function tokens_records()
{
return $this->hasManyThrough(Record::class, Token::class);
}
public function websites_records()
{
return $this->hasManyThrough(Record::class, Website::class);
}
}
$records = $entity->tokens_records
->merge($entity->websites_records);

How can I filter this with eloquent

I try to filter SurveyQuestionnaire which have Answer = 'poor' and their Questionnaire have step = 'rating'.
I've tried to look through the documentation of Eloquent and I've found nothing help.
This is my models.
class Questionnaire extends Model {
...
public function surveysQuestionnaires() {
return $this->hasMany(SurveyQuestionnaire::class, 'question_id');
}
public function answers() {
return $this->hasMany(Answer::class, 'question_id');
}
public function questionnaires() {
return $this->hasMany(QuestionnaireTranslation::class, 'question_id' );
}
}
class SurveyQuestionnaire extends Model {
public function survey() {
return $this->belongsTo(Survey::class ,'survey_id');
}
public function questionnaires() {
return $this->belongsTo(Questionnaire::class, 'question_id');
}
public function answer() {
return $this->belongsTo(Answer::class, 'answer_id');
}
}
Well, the hasMany method returns query builder, so you can simply add some conditions:
public function surveysQuestionnaires() {
return $this->hasMany(SurveyQuestionnaire::class, 'question_id')->where('Answer', 'poor');
}
Also, you can read this link Constraining Eager Loads and add your conditions manually after taking an instance of your model:
$items = App\Questionnaire::with(['surveysQuestionnaires' => function ($query) {
$query->where('Answer', 'poor');
}])->get();

How to sum of function value from related table?

I have two Classes: Share and related EquityGrant
I have this this math function in EquityGrant
public function share() //relationship
{
return $this->belongsTo(Share::class, 'share_id');
}
public function capitalCommitted() //my math function
{
return $this->share_price * $this->shares_amount;
}
But now I need to return in my Share class sum of capitalCommitted() function and here I'm stuck.
My Share class:
public function grants() //relationship
{
return $this->hasMany(EquityGrant::class);
}
public function totalIssued() //sum, works good
{
return $this->grants->sum('shares_amount');
}
public function totalCommitted() //Stuck here
{
//need help here, Must be like this: array_sum($this->grants->capitalCommitted());
}
Use this:
public function getCapitalCommittedAttribute()
{
return $this->share_price * $this->shares_amount;
}
public function totalCommitted()
{
return $this->grants->sum('capitalCommitted');
}

Joins in eloquent orm Php laravel

I have a form in which user have to select from one table to last one i.e
buisnessarea->katogorie->name->genauer->geometrie->ergebnisse
I have a database something like this.
buisnessarea
**buisnessarea_katogorie(pivot table for buisnessarea and katogorie )**
katogorie
**katogorie_name(pivot table for katogorie and name )**
name
**genauer_name(pivot table for name and genauer)**
genauer
**genauer_geometrie(pivot table for genauer and geometrie )**
geometrie
ergebnisse(foreign keys for name and geometrie)
now i want to join all of these tables.The purpose for this is.I want to get the buisnessarea in terms of geometrie.
e.g if i got the geometrie id of 10.So i want to get the buisnessrea which lead to the geometrie id of 10.
Models
Buisnessarea
public function katogorie()
{
return $this->belongsToMany('App\Models\Katogorie')->withTimestamps();
}
Katogorie
public function buisnessarea()
{
return $this->belongsToMany('App\Models\Buisnessarea')->withTimestamps();
}
public function name()
{
return $this->belongsToMany('App\Models\Name');
}
Name
public function katogorie()
{
return $this->belongsToMany('App\Models\Katogorie');
}
public function genauer()
{
return $this->belongsToMany('App\Models\Genauer');
}
public function ergebnisse()
{
return $this->hasMany('App\Models\Ergebnisse');
}
Genauer
public function name()
{
return $this->belongsToMany('App\Models\Name');
}
public function geometrie()
{
return $this->belongsToMany('App\Models\Geometrie');
}
Geometrie
public function genauer()
{
return $this->belongsToMany('App\Models\Genauer');
}
public function ergebnisse()
{
return $this->hasMany('App\Models\Ergebnisse');
}
public function stufen()
{
return $this->hasMany('App\Models\Stufen');
}
Ergebnisse
public function name()
{
return $this->belongsTo('App\Models\Name');
}
public function geometrie()
{
return $this->belongsTo('App\Models\Geometrie');
}
public function katogorie()
{
return $this->hasManyThrough('Katogorie','Name',)
}
Assuming you're using Laravel 5 you can do something along the lines of this:
$geometrie = Geometrie::with("genauer.name.katogorie.buisnessarea")->find($id);
$buisnessareaOfGeometrie = data_get($geometrie, "genauer.name.katogorie.buisnessarea");
Check https://laravel.com/docs/5.3/eloquent-relationships#eager-loading for more details.

Eager loading on polymorphic relations

class Admin {
public function user()
{
return $this->morphOne('App\Models\User', 'humanable');
}
public function master()
{
return $this->hasOne('App\Models\Master');
}
}
class Master {
public function admin()
{
return $this->hasOne('App\Models\Admin');
}
}
class User {
public function humanable()
{
return $this->morphTo();
}
public function images()
{
return $this->hasOne('\App\Models\Image');
}
}
class Image {
public function user()
{
return $this->belongsTo('\App\Models\User');
}
}
Now if I dump this:
return \App\Models\Admin::where('id',1)->with(array('user.images','master'))->first();
I get the perfect result one master, one user and one image record.
But if I do this
return $user = \App\Models\User::where('id',1)->with(array('humanable','humanable.master'))->first();
I only get one Admin record, the query get * from masters doesn't even run.
Any idea what I'm doing wrong, I'm sure this is possible.
If I remember correctly Laravel has lots of pitfall. You can try to use the protected $with var in Admin model instead of query builder with function.
class Admin {
protected $with = ['master'];
public function user() {
return $this->morphOne('App\Models\User', 'humanable');
}
public function master() {
return $this->hasOne('App\Models\Master');
}
}
In query builder, only need to include humanable. Now you should see master inside the humanable object.
return $user = \App\Models\User::where('id',1)->with('humanable')->first();
Hope this help.

Categories