Ad
Laravel5 Eloquent Foreign Key HasOne()
I've got a transaction table containing the data of transactions like: id|user_id|amount|payment_method_id|...
The payment_methods table contains the different payment methods. Table design is: id|name
Content of the table is like:
1|paypal
2|credit card.
Now I would like to display a transaction history. This works fine but laravel always displays the number of the payment method, not the name. My transaction model looks like:
public function payment_method_id()
{
return $this->hasOne('App\Payment_Method', 'id', 'tpayment_method_id');
}
All the time it displays the number, but not the correct name of payment method. Where's my fault?
Ad
Answer
You should define relationship this way:
public function paymentMethod()
{
return $this->belongsTo('App\Payment_Method', 'payment_method_id');
}
And now you can display payment method name this way:
echo $transaction->paymentMethod->name;
In your example you used the same name of relationship as column and you used hasOne
relationship instead of belongsTo
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms
Ad