我有一个Laravel查询,其中带有where子句语句和一个参数。来自源的$ plan是字符串,值是:
每日,每周,每月,钱包
但目标,用户表($ users)中$ plan的值是: 1,2,3,4
参数来源:
@foreach($billings as $key => $billing)
<tr>
<td>{{ ++$key }}</td>
<td><a class="btn btn-info" href="{{ route('revenueDetail',$billing->plan) }}">{{ $billing->plan }}</a></td>
<td>{{ $billing->total_plans }}</td>
<td>{{ $billing->total_amount }}</td>
</tr>
@endforeach
<tr>
<td colspan="8">
{{ $billings->links() }}
</td>
</tr>
控制器:查询
public function revenueDetail($plan = null)
{
$revenuedetails = DB::table("users")
->select(
"users.username",
DB::raw("DATE(users.created_at) as subscription_date")
)
->where('users.plan', $plan)
->get();
}
我要实现的是在RevenueDetail()函数的where子句中,如果:
$ plan =每日,然后是users.plan = 1
$ plan =每周,然后是users.plan = 2
$ plan =每月,然后是users.plan = 3
$ plan =钱包,然后是users.plan = 4
我该如何实现?
答案 0 :(得分:0)
基于最后一句话,我假设您需要映射== 1的每日字符串
protected static $type = [
'Daily' => 1,
'Weekly' => 2,
'Montly' => 3,
]
然后
public function revenueDetail($plan = null)
{
$revenuedetails = DB::table("users")
->select("users.username", DB::raw("DATE(users.created_at) as subscription_date"))
->where('users.plan', self::$type[$plan] ?? null) // ?? will escape exception if $plan is null
->get();
}
答案 1 :(得分:0)
从视图用户应该发送ID而不是值。但是,如果您要发送价值,则应向计划表询问您发送的价值ID。我建议将计划存储在缓存中。另一个解决方案是像您的计划一样让Constans上课(如果您将来不想扩展它)