2017-11-15 7 views
0
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'); 
    } 


    ... 
} 

In den AnsichtenLaravel eloquent Zeichen Objekte mit Bezug

@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 

Diese Arbeit gut. Und gibt die Standardzahlungsmethode für den Benutzer aus.

Aber ich möchte nicht alle Zahlungsmethoden für Benutzer ausgeben. Und für jede Methode Ausgabe Label, wenn dies für den Benutzer Standard ist.

ich want't DB nicht :: RAW oder Unterabfragen in cicle

@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 

Wie dieses cicle beeinflussen und Modellcode ausgegeben, wenn Methode default?

Antwort

1

Sie können die Zahlungsmethoden durchlaufen und die IDs mit der ID der Standard-Zahlungsmethode vergleichen. Sie können übereinstimmen, Sie wissen, dass es die Standardeinstellung ist.

@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 
+0

Ок seine Ordnung. Aber ich wähle andere Methode – htclog81

0

gefunden Lösung:

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>