php - LARAVEL 8: ErrorException Undefined index: private
Get the solution ↓↓↓I'm working with Laravel 8 to develop my project, and I have made this form, in order to add some data into db:
<form class="mb-3" method="POST" action="/ask">
@csrf
<div class="form-group">
<label for="topic" class="BKoodakBold">Question Title</label>
<input name="title" type="text" class="form-control BSinaBold" id="topic" required>
</div>
<div class="form-group">
<label for="topic" class="BKoodakBold">Question Body</label>
<textarea name="body" class="form-control BSinaBold" id="comment" required></textarea>
</div>
<label for="topic" class="BKoodakBold">Question Category</label>
<select class="form-control BSinaBold" name="category" id="category">
@foreach(\App\Models\Category::all() as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select></br>
<div class="form-check">
<label class="form-check-label BKoodakBold">
<input name="private" type="checkbox" class="form-check-input" id="checkbox" value="on">
private chat
</label>
</div></br>
<button type="submit" class="btn btn-primary BJadidBold">Send</button>
<button type="reset" class="btn btn-danger BJadidBold">Reset</button>
</form>
And at the Controller, I have added these codes:
public function postForm()
{
$validate_data = Validator::make(request()->all(),[
'title' => 'required',
'body' => 'required',
'private' => 'nullable',
'category' => 'required'
])->validated();
Question::create([
'title' => $validate_data['title'],
'body' => $validate_data['body'],
'private' => $validate_data['private'],
'category' => $validate_data['category']
]);
return redirect()->back();
}
Now the problem, if I UNCHECKED theprivate chat
checkbox, I get this as error:
ErrorException Undefined index: private
However, if I checked it, the form will be submitted successfully.
So what is your idea on this, how can I fix this issue?
Migration:
public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->string('category');
$table->string('private');
$table->timestamps();
});
}
Answer
Solution:
Use something like:
Arr::get($validate_data, 'private', 0)
or
$request->input('private', 0)
Note that with Arr::get() you don't need to specify third argument, since it is false by default. In cases like this, you should use some helpers with defult value. I imagine that this will work also:
'private' => $validate_data['private'] ?? false
Answer
Solution:
if your private checkbox not nullable() use $table->string('private');
'private' => 'required',
then uncheck private so your value will be add 'off' if it is checked it will be store 'on'.
if($request->get('private') == null){
$private= off;
}
else{
$private= request('private');
}
Answer
Solution:
public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->string('category');
$table->string('private')->nullable();
$table->timestamps();
});
}
Designate that the column allows NULL values
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: integrity constraint violation: 1452 cannot add or update a child row: a foreign key constraint fails
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.