I am very green at Laravel (first project) so bear with me if I'm making novice mistakes. I'm trying to create this project while running through Laracasts, so I'm using his suggestions. I'm using Laravel 5.4 and PHP 7.1.4
I have a checkbox on a form for a Boolean field. If the checkbox is not checked when the form is submitted then it returns null. I do not want null values for Boolean's so I have a validator ensuring it only accepts true/false values. In order for this to work I had to create a mutator to change the value to false if it was null.
I have two models, Item and ItemNote. I'm trying to create the ItemNote from the Item model. On the Item page there is a place to add the ItemNote which runs through to the ItemNoteController, I then call a method in Item to add the ItemNote. The issue is I can't get the mutator to run in the ItemNote model so the validation fails because the Boolean field (calendar_item) is null.
I was at first trying to create ItemNote from the relationship with Item, according to this stack overflow Laravel 5 mutators only work when I create a record and not when I update a record answer the mutator will not run when creating via a relationship $this->notes()->create($request->all()). You have to use the model $this->notes->create($request->all()) notice the absence of parenthesis after notes. So I've tried everything I can possibly think of to try to create the object via the model and I still can't get the mutator to run.
Here are the relationship declarations in my models:
Item
public function notes() { return $this->hasMany(ItemNote::class); }
ItemNote
public function item() { return $this->belongsTo(Item::class); }
Mutator in ItemNote for calendar_item
protected function setCalendarItemAttribute($value) { $this->attributes['calendar_item'] = isset($value) ? $value : FALSE; }
The validation rules in ItemNote
public static $validationRules = array('note_date' => 'required|date',
'resolve_date' => 'nullable|date',
'notes' => 'required|string',
'cost' => 'nullable|numeric',
'calendar_item' => 'required|boolean',
'attachment_path' => 'nullable|string|max:200');
This is the action in ItemNoteController that runs when adding a ItemNote from the Item page
public function store(Item $item)
{
$this->validate(request(), ItemNote::$validationRules);
$item->addNote(new ItemNote(request(['item_note_category_id', 'note_date', 'resolve_date',
'notes', 'cost', 'calendar_item', 'attachment_path'])));
return back();
}
Here is the function addNote in the Item model
public function addNote(ItemNote $note)
{
$this->item_note->save($note);
}
Here are the different things I have tried in addNote, they all fail to run the mutator. The create statements have the field assignments listed out but I removed them here for brevity.
$this->notes->save($note);
$this->notes()->save($note);
$this->item_note->save($note);
$this->notes->create
$this->item_notes->create
$this->item_notes()->create
$this->item_note->create
$this->item_note()->create
$this->ItemNote->create
$this->ItemNote()->create
ItemNote::create
All of the above work, although I would think $this->item_notes->create shouldn't work at all because the relationship name is notes but it doesn't complain which makes me think it may not be getting to this code and it's failing on the validate statement in the controller. How do I get the mutators run before the validation? Or is there a better way to clean up the data before the validation?
I also tried putting the item_id field in the validation rules but that always fails because the item_id is not assigned until I create the object via the relationship. I'd like to require it but haven't figured out how to get it assigned in the request.
Any help is appreciated. Sorry for the long post.