How to sum of function value from related table? - php

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

Related

Attaching another table after a hasManyThrough relationship

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();

App\PageList::coupon must return a relationship instance in laravel 5.5

I am trying to get a count() from database but I am getting an error.
I am trying to find how many N are in is_ordered field.
My Model look like:
public function coupon()
{
return $this->hasOne('App\PageCoupon','page_id','business_id')
->where('is_ordered','N')->count();
}
and my blade view:
<p>{{optional($value->coupon)->is_ordered}}</p>
I am unable to find a solution.
your help will be highly appreciated!
public function pageListHere()
{
$list = PageList::all();
return view('page-list',compact('list'));
}
class PageList extends Model
{
protected $table = 'page_master';
protected $fillable = ['business_id', 'page_url', 'page_name'];
public function particulars()
{
return $this->hasOne('App\Sale','user_id','business_id');
}
public function coupon()
{
return $this->hasOne('App\PageCoupon','page_id','business_id');
}
}
You can try doing it this way.
Leave your first function coupon like this:
public function coupon()
{
return $this->hasOne('App\PageCoupon','page_id','business_id')->where('is_ordered','N');
}
And then you can have a second function as so:
public function couponCount()
{
return $this->coupon->count();
}
After that you can use it in blade on your variable like so: $coupon->couponCount();
Your code and what you want is little bit confusing. try this:
PageList.php
public function coupon()
{
return $this->hasMany('App\PageCoupon','page_id','business_id');
}
Controller
public function pageListHere()
{
$list = PageList::all();
return view('page-list',['list'=>$list]);
}
View
#foreach($list as $listItem)
<p>{{$listItem->coupon()->where('is_ordered','N')->count()}}</p>
#endforeach

Calling a function that exists inside another function

i just want to know how i would include a function inside another function. TRying to query my database for users 3 levels deep.
<?php
namespace App\Repositories\Referrals;
use Auth;
/**
*
*/
class EloquentReferrals implements ReferralRepository
{
function __construct(User $model)
{
return $this->model->all();
}
function __construct(User $model)
{
return $this->model->all();
}
public function getallreferrals()
{
return $this->model->where('referred_by', Auth::user()->referral_id)->get();
}
public function getallreferrals2gen()
{
}
public function getallreferrals3gen(){
}
public function getallreferralsbyID($id){
}
public function getallreferrals2gen($id){
}
public function getallreferrals3gen($id){
}
}
in my repository the get all referrals function returns all direct referrals. I want to use the result to get the referrals of those referrals. How do i include it inside my getallreferrals2gen function?
use $this to call a function from a function in the same class;
property, like so;
public function getallreferrals()
{
return $this->model->where('referred_by', Auth::user()->referral_id)->get();
}
public function getallreferrals2gen()
{
$this->getallreferrals()(
}

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.

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

Categories