I have two models, Card
and Expense
. Both have a type
field. Whenever I add a new expense, I have a dropdown in the form to select a Card
. They are related by a belongsTo relation.
The thing is, I want to automatically set the type
field of my Expense to the same as my card's type. I do not want to have another dropdown for type
in my expense form. Also, I want to keep using the following on my store
method on my Expense Controller:
auth()->user()->expenses()->create($request->all());
I think this should be possible by using a mutator perhaps in the model, but I'm not entirely sure how to approach this.
So in short, what I need is, when creating a new Expense
, I need the type
field to be grabbed from the related Card
, and then inserted into the Expense type
field.
What I don't want to do in my controller, is something like:
$expense = new Expense;
$expense->name = ... And so on