Still new to laravel, learning how the $request interacts with create.
here is my form for two of my variables for context:
<form method="POST" id="postForm"> {{-- @csrf --}} <input type="hidden" id="id_hidden" name="id" /> <div class="form-group"> <label for="title"> Title <span class="text-danger">*</span></label> <input type="text" name="title" id="title" class="form-control"> </div> <div class="form-group"> <label for="category_description"> Description <span class="text-danger">*</span></label> <textarea name="category_description" id="category_description" class="form-control"></textarea> </div> </form>
controller:
public function store(Request $request) { $request->validate([ 'title' => 'required', 'category_description' => 'required', ]); $post = HmsBbrCategory::create($request->all()); if(!is_null($post)) { return response()->json(["status" => "success", "message" => "Success! post created.", "data" => $post]); } else { return response()->json(["status" => "failed", "message" => "Alert! post not created"]); } }
model:
protected $table = 'hms_bbr_category'; protected $fillable = [ "category_id", "title", "category_description", "category_description_2" ];
my title
and category_description
is inserting fine with an auto incremented id column. What I am trying to do is just to add 2 columns: category_id
and category_description_2
that just copies the value inserted by id
and category_description
Question:
how does 'required'
retrieve the data from the form? I would like to have the same data thats taken and adding it to my two new columns. I am aware that I cannot just simple add 'category_description_2' => 'required',
because this won’t get an existing data.
so basically
$id = id $category_id = id $title = title $category_description = category_description $category_description_2 = category_description
Here is my table for reference. This form was given to me and I want to understand to know more about Laravel, thanks for reading and I hope I can get some suggestions on what to add.
Advertisement
Answer
You are running ->validate([])
on the $request variable which takes all of the information that is laravel puts together during the post request. If you do
dd($request->all());
you will be able to see all of the data that is passed from the form that you can run different validate rules on.
If you would like to add other data into your $request
variable in order to save it to your model, you can always just add it to the $request
array like so: $request['variable_1'] = 'data for variable one'
and so on
Since I see that you have category_id
that you would like to reference in your saved record, I would suggest you create a relation in your HmsBbrCategory
model and the parent model that category_id
belongs to. This will help you keep the integrity of your database in tact.
As another option, you can structure your url in such a way that passes the category_id
to your store method in the controller. You will then need to find that category id and make sure it exists and save it via the relation that you created:
public function store (Request $request, $category_id){ $main_category = Category:find($category_id); //make sure it exists $new_record = $main_category->exampleRelation()->save($request->all()); if(!$new_record){ return failed save } return successful save message }
By doing the above, it will automatically insert the category_id
into your saved record.
As another alternative, you could create a hidden field in your form that references category_id
and other fields that you would like to add to your record on save. However, keep in mind which “sensitive” information you would like the users to see if someone decide to view source on the browser window.