Php laravel用关系雄辩地标记对象

Php laravel用关系雄辩地标记对象,php,laravel,eloquent,laravel-eloquent,laravel-5.5,Php,Laravel,Eloquent,Laravel Eloquent,Laravel 5.5,在视图中 class PaymentMethod extends Model { .... public function user() { return $this->belongsTo(User::class, 'user_id', 'id'); } ... } class User extends Authenticatable { ... public function payment_methods(

在视图中

class PaymentMethod extends Model
{
     ....

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
    ...
}

class User extends Authenticatable
{
    ...

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

    public function default_payment_method()
    {
        return $this->hasOne(PaymentMethod::class, 'id', 'default_payment_method_id');
    }


    ...
}
@if($user->getDefautPaymentMethod()->type==PaymentMethod::PAYPAL\u账户)
Paypal:{{$user->getDefautPaymentMethod()->Paypal\u email}
@否则
卡片:{{$user->getDefautPaymentMethod()->Card_brand}*********{{$user->getDefautPaymentMethod()->Card_last_four}
@恩迪夫
这工作做得很好。并为用户输出默认的付款方式

但我不想为用户输出所有付款方法。如果这是用户的默认值,则为每个方法输出标签

我不想在cicle中使用DB::RAW或sub查询

@if($user -> getDefautPaymentMethod()->type == PaymentMethod::PAYPAL_ACCOUNT)
    <div><strong>Paypal: </strong> {{$user -> getDefautPaymentMethod()-> paypal_email}}</div>
@else
    <div><strong>Card: </strong>{{$user -> getDefautPaymentMethod()-> card_brand}} **** **** **** {{$user -> getDefautPaymentMethod()-> card_last_four}}</div>
@endif
@foreach($user->payment\u methods()->get()作为$payment\u method)
@如果($payment\u method->type==PaymentMethod::PAYPAL\u账户)
贝宝:{{$payment\u method->Paypal\u email}
@否则
卡:{{$payment\u method->Card\u brand}*********{{$payment\u method->Card\u last\u four}
@恩迪夫
@endforeach

如果方法默认,如何影响此cicle和模型代码的输出?

您可以循环查看付款方法,并将ID与默认付款方法的ID进行比较。它们可以匹配,您知道这是默认值

    @foreach($user->payment_methods()->get() as $payment_method)
        @if($payment_method ->type == PaymentMethod::PAYPAL_ACCOUNT)
            <div><strong>Paypal: </strong> {{$payment_method -> paypal_email}}</div>
        @else
            <div><strong>Card: </strong>{{ $payment_method -> card_brand }} **** **** **** {{$payment_method -> card_last_four}}</div>
        @endif
    @endforeach
@foreach($user->payment-methods()->get()作为$payment\u方法)
@如果($user->get\u default\u payment\u method()->id===$payment\u method->id)
默认付款方式
@恩迪夫
{{$payment_method}}
@endforeach
我找到了解决方案:

@foreach ($user->payment-methods()->get() as $payment_method)
    <div>
        @if ($user->get_default_payment_method()->id === $payment_method->id)
            <strong>Default Payment Method</strong>
        @endif
        {{ $payment_method }}
    </div>
@endforeach
public函数defaultForUser()
{
返回$this->belongsTo(用户::类,'id','default\u付款方法\u id');
}
付款方式
@foreach($user->payment\u methods()->withCount('defaultForUser')->get()作为$payment\u方法)
id:{{$payment\u method->id}
网关id:{{$payment\u method->braintree\u id}
@如果($payment\u method->type==PaymentMethod::PAYPAL\u账户)
贝宝:{{$payment\u method->Paypal\u email}
@否则
卡:{{$payment\u method->Card\u brand}*********{{$payment\u method->Card\u last\u four}
@恩迪夫
@如果($payment\u method->default\u for\u user\u count)
默认值
@恩迪夫
@endforeach

öimk_很好。但我选择了另一种方法
public function defaultForUser()
{
    return $this->belongsTo(User::class, 'id', 'default_payment_method_id');
}

<h3>Payment methods</h3>
<table border="1">
<tr><th></th><th></th><th></th></tr>
@foreach($user->payment_methods()->withCount('defaultForUser')->get() as $payment_method) 
    <tr>
    <td>
        <div><strong>id:</strong> {{$payment_method->id }}</div>
        <div><strong>gateway id:</strong> {{ $payment_method->braintree_id }}</div>
    </td>
    <td>
        @if($payment_method ->type == PaymentMethod::PAYPAL_ACCOUNT)
            <div><strong>Paypal: </strong> {{$payment_method -> paypal_email}}</div>
        @else
            <div><strong>Card: </strong>{{ $payment_method -> card_brand }} **** **** **** {{$payment_method -> card_last_four}}</div>
        @endif

    <td>
        @if ($payment_method -> default_for_user_count)
            <strong style = "color:green">Default</strong>
        @endif
    </td>
    </td>
    </tr>
@endforeach
</table>