Help me clear my confusion about storing fields in the controller
abrhambas01 opened this issue · 5 comments
I have a confusion with the readme , how do I store what's inside the controller ?
I have only two fields in my
projects
table : project_title and project_description
<form action="{{ route('project.store') }}" method="POST" class="bg-black p-4 w-1/2">
@csrf
<h5>New Project:</h5>
<div class="form-group">
<label for="exampleInputEmail1">Project Title : </label>
<input type="text" name="project_title" class="form-control p-2" placeholder="Bloody Friday Sale">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Complete Description:</label>
<template>
@trix(\App\Project::class, 'project_description')
</template>
</div>
<div class="form-group">
<input type="submit">
</div>
</form>
Is this how you store data inside the controller ?
Project::create([
'project_title' => request('project_title'),
'attachment-project-trixFields' => request('attachment-trixFields'),
'project_description' => request('project-trixFields')
]);
will be like this and HasTrixRichText trait must be used inside Project model
//storing must follow this convention (model lowered class name)-trixFields
//and for attachment must follow attachment-(model lowered class name)-trixFields
Project::create([
'project-trixFields' => request('project-trixFields'),
'attachment-project-trixFields' => request('attachment-project-trixFields'),
'project_title' => request('project_title'),
]);
also you can debug what's in the request by doing this in your controller
dd(request()->all());
Project::create([
'project_title' => request('project_title'),
'attachment-project-trixFields' => request('attachment-project-trixFields'),
'project_description' => request('project-trixFields')
]);
Alright so I can do it like request()->input('project-trixFields.project_description');
?
In the trix_rich_texts
table, Is it optional to have a data to be entered inside it after a successful entry..? Or it's ok to just not be able to have an entry there..
Joining the question. The documentation is indeed quite unclear about storing values coming from editor. In my case, I'm not using mass assignment so I have to discover custom approaches how to insert value into model property as it is forbidden to use dashes in properties:
// Can't do that!
$post->post-trixFields = $request->post-trixFields;
Yep, I still can use Model::fill()
to fill in that special attributes, but I had no luck doing that way either.