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?
Ок seine Ordnung. Aber ich wähle andere Methode – htclog81