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.
Related
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);
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();
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');
}
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');
}
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.