Laravel 5 blade 中与查询的模型关系
Laravel 5 Model Relation to Query in blade
架构用户Table
ID|NAME
1 |John
2 |Doe
架构财务Table
ID|USER_ID|PROFIT |DATE
1 |1 |1000 |2016-12-22
2 |2 |-2000 |2016-12-22
3 |1 |2000 |2016-12-24
4 |2 |-2000 |2016-12-24
用户型号
class User extends Model
{
public function Financial()
{
return $this->hasMany('App\Financial');
}
}
财务模型
class Financial extends Model
{
public function financial()
{
return $this->belongsTo('App\User');
}
}
我的控制器
class MyController extends Controller
{
public function index()
{
$user = User::get();
$financial = Financial::get();
return view('page.index',compact('user','financial'));
}
}
我的Blade
@foreach ($user as $u)
<tr>
<td>{{$u->id}}</td>
<td>
{{$u->financial->sum('gross')}}
{{-- Above code doesn't work --}}
{{-- Run something link --}}
{{select (sum(profit) as total) from financial where user_id = $u->id}}
</td>
</tr>
@endforeach
问题
如何从我的 blade 中实现 select?我打算使用 @inject('DB','Illuminate\Support\Facades\DB') 这样我就可以使用 DB::raw我的 blade,但我对如何执行查询以实现 select 感到困惑:(
通过使用 Laravel Collection Method Pluck and Laravel Blade Service Injection 解决。下面是我为归档我想要的内容所做的代码:
@inject('financial','App\Financial') {{-- inject before foreach --}}
@foreach ($user as $u)
<tr>
<td>{{$u->id}}</td>
<td>{{$financial->where('user_id','=',$u->id)->get()->pluck('profit')->sum()}}</td>
</tr>
@endforeach
评估发生了什么:
$financial->where('user_id','=',$u->id)->pluck('profit')->sum()
// get financial where user_id = $u->id // get the profit // sum the profit
干杯!
你可以简单地这样做:
{{$u->financial()->sum('gross')}}
如果您是运行关系查询,请在调用函数时添加函数括号。
架构用户Table
ID|NAME
1 |John
2 |Doe
架构财务Table
ID|USER_ID|PROFIT |DATE
1 |1 |1000 |2016-12-22
2 |2 |-2000 |2016-12-22
3 |1 |2000 |2016-12-24
4 |2 |-2000 |2016-12-24
用户型号
class User extends Model
{
public function Financial()
{
return $this->hasMany('App\Financial');
}
}
财务模型
class Financial extends Model
{
public function financial()
{
return $this->belongsTo('App\User');
}
}
我的控制器
class MyController extends Controller
{
public function index()
{
$user = User::get();
$financial = Financial::get();
return view('page.index',compact('user','financial'));
}
}
我的Blade
@foreach ($user as $u)
<tr>
<td>{{$u->id}}</td>
<td>
{{$u->financial->sum('gross')}}
{{-- Above code doesn't work --}}
{{-- Run something link --}}
{{select (sum(profit) as total) from financial where user_id = $u->id}}
</td>
</tr>
@endforeach
问题
如何从我的 blade 中实现 select?我打算使用 @inject('DB','Illuminate\Support\Facades\DB') 这样我就可以使用 DB::raw我的 blade,但我对如何执行查询以实现 select 感到困惑:(
通过使用 Laravel Collection Method Pluck and Laravel Blade Service Injection 解决。下面是我为归档我想要的内容所做的代码:
@inject('financial','App\Financial') {{-- inject before foreach --}}
@foreach ($user as $u)
<tr>
<td>{{$u->id}}</td>
<td>{{$financial->where('user_id','=',$u->id)->get()->pluck('profit')->sum()}}</td>
</tr>
@endforeach
评估发生了什么:
$financial->where('user_id','=',$u->id)->pluck('profit')->sum()
// get financial where user_id = $u->id // get the profit // sum the profit
干杯!
你可以简单地这样做:
{{$u->financial()->sum('gross')}}
如果您是运行关系查询,请在调用函数时添加函数括号。