dongyuan1983 2017-11-17 07:30
浏览 51

Laravel 5.5在刀片和许多SQL查询中雄辩的请求

I need output user payments history. With payment method detail. Like card or paypall data for each payment

<table border="1">
        <tr>
        <th>
        </th>
        <th>
            summ
        </th>
        <th>
            date
        </th>
        <th>
            payment method
        </th>
        <th>
            subscription
        </th>
        <th></th>
        </tr>
    @foreach($user -> payments()->get() as $payment)
        <tr>
            <td>
            {{$payment->type}}
            </td>
            <td>
            {{$payment->amount}}$
            </td>
            <td>
            {{$payment->created_at}}
            </td>
            <td>
                   @if($payment -> payment_method()->first()->type == PaymentMethod::PAYPAL_ACCOUNT)
                        <div><strong>Paypal: </strong> {{ $payment -> payment_method() -> first() -> paypal_email }}</div>
                    @else
                        <div><strong>Card: </strong>{{ $payment -> payment_method() -> first() -> card_brand }} **** **** **** {{ $payment -> payment_method() -> first() -> card_last_four }}</div>
                    @endif
            </td>
            <td>
                <div><strong>plan:</strong> {{ $payment->subscription->plan->name }}</div>
                <div><strong>period:</strong> {{ $payment->period_start->format('d.m.Y')}} - {{ $payment->period_end->format('d.m.Y') }}</div>
            </td>
            <td>
                @if (!empty($payment -> void_at))
                    <div>void {{ $payment -> void_at }}</div>
                @elseif (!empty($payment->refund_at))
                    <div>refunded {{ $payment->refund_at }} </div>
                @elseif(!empty($payment->refunded_transaction_id ))
                     <div>refund</div>
                @endif
            </td>  
        </tr> 
    @endforeach
</table>

This code generates many sql request's in each cycle iteration:

select * from `payment_method` where `payment_method`.`id` = '3' and `payment_method`.`deleted_at` is null limit 1

select * from `payment_method` where `payment_method`.`id` = '3' and `payment_method`.`deleted_at` is null limit 1

My models:

User class. With realation to payment one to many

class User extends Authenticatable
{

    public function payments()
    {
        return $this->hasMany(Payment::class, $this->getForeignKey())->orderBy('created_at', 'desc');
    }

}

Payment class with relation to payment method one to one

class Payment extends Model
{

    public function payment_method()
    {
        return $this->belongsTo(PaymentMethod::class, 'payment_method_id', 'id');
    }

How change my code for avoid this

  • 写回答

2条回答 默认 最新

  • dqyin0101 2017-11-17 09:01
    关注

    You can try like this:

    $user = User::with("payments")->find($user_id);
    

    and use foreach

    @foreach($user->payments as $payment)
        {{$payment->type}}
    @endforeach
    

    Off course, you can change this piece of code as you wish.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP