Laravel : Insert multiple data into database - laravel

I have been creating Quesionnaire where there are 3 tables : standarts,questions,and responses.
Inside each standart have their own questions.
here is how my form question look like
#foreach($standarts as $s)
<form action="/standart/{{ $s->id }}/answer/post" method="post">
#endforeach
#csrf
<div class="row pt-3 mb-3">
<div class="col-auto">Dokumen Pendukung :</div>
<div class="col-7">
<input class="form-control form-control-sm" name="files_link" type="text" placeholder="Link google drive" aria-label="files_link">
</div>
</div>
<h3 class="text-center">Penilaian</h3>
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th scope="col" class="text-center">No</th>
<th scope="col">Pertanyaan</th>
#foreach($standarts as $s)
#if($s->type == 'Likert')
<th scope="col" class="text-center">1</th>
<th scope="col" class="text-center">2</th>
<th scope="col" class="text-center">3</th>
<th scope="col" class="text-center">4</th>
#else
<th scope="col" class="text-center">Ya</th>
<th scope="col" class="text-center">Tidak</th>
#endif
#endforeach
</tr>
</thead>
<tbody>
#foreach($standarts as $s)
#foreach($s->questions as $q)
<tr>
<td style="width: 7%;" class="text-center">{{ $loop->iteration }}</td>
<td>
<input type="hidden" id="question" name="question[]{{ $q->id }}" value="{{$q->question}}">
<input type="hidden" id="question" name="question_id[]{{ $q->id }}" value="{{$q->id}}">
{{$q->question}}
</td>
#if($s->type == 'Likert')
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio1" value="1">
</td>
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio2" value="2">
</td>
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio3" value="3">
</td>
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio4" value="4">
</td>
#else
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio5" value="Ya">
</td>
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio6" value="Tidak">
</td>
#endif
</tr>
#endforeach
#endforeach
</tbody>
</table>
<div class="row pt-3 mb-4">
<div class="col-auto">Keterangan :</div>
<div class="col-10">
<textarea name="description" class="form-control" id="description" rows="3"></textarea>
</div>
</div>
<hr>
<div class="row pt-3 mb-4">
<div class="col-auto pe-0">
<button type="submit" class="btn btn-success">Simpan</button>
</div>
<div class="col-auto">
<a type="button" href="{{route('auditee.dashboard')}}" class="btn btn-secondary">Batal</a>
</div>
</div>
</form>
this is my controller :
$request->all();
foreach ( $request->get('answers') as $answer) {
$answers[] = [
'user_id' => \Auth::user()->id,
'files_link' => $request->files_link,
'question' => $request->question,
'question_id' => $request->question_id,
'answer' => $request->answers,
'description' => $request->description,
];
}
dd($answers);
and here is how it look like at diedump :
array:3 [▼
0 => array:6 [▼
"user_id" => 3
"files_link" => "http://127.0.0.1:8000/auditee/1/respons/"
"question" => array:3 [▼
0 => "Lorem Ipsum is "
1 => "Lorem Ipsum is "
2 => "Lorem Ipsum is "
]
"question_id" => array:3 [▼
0 => "6"
1 => "7"
2 => "8"
]
"answer" => array:3 [▼
0 => "Ya"
1 => "Tidak"
2 => "Ya"
]
"description" => "asdasd"
]
1 => array:6 [▶]
2 => array:6 [▶]
]
While what i want is like this :
array:3 [▼
0 => array:6 [▼
"user_id" => 3
"files_link" => "http://127.0.0.1:8000/auditee/1/respons/"
"question" => "Lorem Ipsum is "
"question_id" => "6"
"answer" => "Ya"
"description" => "asdasd"
]
1 => array:6 [▼
"user_id" => 3
"files_link" => "http://127.0.0.1:8000/auditee/1/respons/"
"question" => "Lorem Ipsum is"
"question_id" => "7"
"answer" => "Tidak"
"description" => "asdasd"
]
2 => array:6 [▶]
]
and then insert it to the database.
im new at laravel and i have been searching the whole day on internet some example to make it work.
Thanks before

You can do somethink like this
#foreach($standarts as $s)
#foreach($s->questions as $key=>$q)
<tr>
<td style="width: 7%;" class="text-center">{{ $loop->iteration }}</td>
<td>
<input type="hidden" id="question" name="standart[{{$key}}][question]" value="{{$q->question}}">
<input type="hidden" id="question" name="standart[{{$key}}][question_id]" value="{{$q->id}}">
{{$q->question}}
</td>
#if($s->type == 'Likert')
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio1" value="1">
</td>
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio2" value="2">
</td>
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio3" value="3">
</td>
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio4" value="4">
</td>
#else
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio5" value="Ya">
</td>
<td style="width: 10%;" class="text-center">
<input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio6" value="Tidak">
</td>
#endif
</tr>
#endforeach
#endforeach
So in your controller method you can
dd($request->all()); or dd($request->standart)

Related

Laravel vue js 3 need to edit single record from table

I have this following:
<div class="modal-body" >
<form v-for="lead in editleads" id="" class="form-horizontal validate-form">
<input v-model="editl" type="text" class="form-control my-2 py-2" :placeholder="lead">
</form>
</div>
<table class="table table-hover align-middle mb-0">
<thead class="">
<tr>
<th><input type="checkbox" class="form-check-input" v-model="selectAll" title="Select All"></th>
<th v-for="(header, index) in visibleHeaders" :key="index" scope="col">
{{ header }}
</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr v-show="leads.length" v-for="(column, index) in visibileColumn" :key="index">
<td>
<input type="checkbox" class="form-check-input" v-model="selected" :value="column.id" />
</td>
<td v-for="atr in column">
{{atr}}
</td>
<td>
<button #click="editLead(column.id)" type="button" class="btn btn-sm btn-secondary" data-mdb-toggle="modal" data-mdb-target="#editLeadModal" >
<i class="fa-solid fa-pen-to-square"></i>
</button>
</td>
</tr>
<tr v-show="!leads.length">
<td colspan="12" class="text-center">Sorry :( No data found.</td>
</tr>
</tbody>
</table>
data() {
reurn {
headers: [],
leads: [],
...
}
},
editLead(id) {
axios.get('/leads/'+id+'/edit')
.then(response => {
this.editleads = response.data.leads
})
},
I want to edit each table record one by one. I mean to edit single record, but the problem is i return an array object from backend and i dont know how to edit each input. If i click now on form all input filled with same value. can someone help with this

Insert Multiple record in database laravel 8

I need to insert all the number in my database. I used foreach loop to view students and marks to insert.
When click save all data of class test partA partB will store in database.
I am facing difficulties in controller
`<form action="{{ route('teacher.add_all_student_results',[$session_id,$semester_id,$course_id,$course_credit]) }}" method="POST">
<table class="table table-bordered table-hover table-sm">
<thead>
<tr>
<th>IMG</th>
{{-- <th>Department Name</th> --}}
<th>Reg.No</th>
<th>ID.No</th>
<th>Student Name</th>
<th>Attendance</th>
<th>Class Test</th>
<th>PartA</th>
<th>PartB</th>
{{-- --}}
</tr>
</thead>
<tbody>
#csrf
#php $i=1;
#endphp
#foreach($semester_students as $semester_student)
<tr data-widget="expandable-table" aria-expanded="false">
<td>
<input type="hidden" name="student_id" value={{ $semester_student->id }} placeholder="add marks" class="form-control">
</td>
<td><img class="img-circle img-bordered-sm" src="" alt="U" width="50"></td>
<td>{{$semester_student->registration_number}}</td>
<td>{{$semester_student->roll_number}}</td>
<td>{{$semester_student->firstname}} {{$semester_student->lastname}}</td>
<td>
<input type="text" name="attendance" placeholder="add marks" class="form-control">
</td>
<td>
<input type="text" name="class_test" placeholder="add marks" class="form-control">
</td>
<td>
<input type="text" name="parta" placeholder="add marks" class="form-control">
</td>
<td>
<input type="text" name="partb" placeholder="add marks" class="form-control">
</td>
</tr>
#endforeach
</tbody>
</table>
<button type="submit" class="btn btn-success mt-1"><i class="fa fa-check-circle"
aria-hidden="true"></i>
Save</button>
</form>`
To use multiple inputs by looping them you need to define input names as arrays.
For example;
<input name="student_id[]">
<input name="student_id[]">
<input name="student_id[]">
<input name="student_id[]">
<input name="student_id[]">
If I place this inputs in a form and submit it, I can receive request()->input('student_id') as an array which contains all 5 values in it.
So, in your foreach loop you need to define every multiple input as an array.
Also you can give custom indexes to them to fetch them easily. For example;
<input name="student_data[][student_id]">
<input name="student_data[][student_id]">
<input name="student_data[][student_id]">
<input name="student_data[][student_id]">
<input name="student_data[][student_id]">
Now you can access all looped rows with request()->input('student_data') or directly request()->student_data
It will be an array and you can use it in a foreach. dd(request()->student_data) for example;
array:5 [▼
0 => array:1 [▼
"student_id" => "123"
]
1 => array:1 [▼
"student_id" => "234"
]
2 => array:1 [▼
"student_id" => "345"
]
3 => array:1 [▼
"student_id" => "456"
]
4 => array:1 [▼
"student_id" => "567"
]
]
Now if you give them manual indexes for each row, yo can group everything together;
<input name="student_data[0][student_id]">
<input name="student_data[0][student_name]">
<input name="student_data[1][student_id]">
<input name="student_data[1][student_name]">
<input name="student_data[2][student_id]">
<input name="student_data[2][student_name]">
<input name="student_data[3][student_id]">
<input name="student_data[3][student_name]">
<input name="student_data[4][student_id]">
<input name="student_data[4][student_name]">
Now request()->student_data will be like this;
array:5 [▼
0 => array:2 [▼
"student_id" => "1"
"student_name" => "1st name"
]
1 => array:2 [▼
"student_id" => "2"
"student_name" => "2nd name"
]
2 => array:2 [▼
"student_id" => "3"
"student_name" => "3rd name"
]
3 => array:2 [▼
"student_id" => "4"
"student_name" => "4th name"
]
4 => array:2 [▼
"student_id" => "5"
"student_name" => "5th name"
]
]
So; you need to update your inputs' names.
<form action="{{ route('teacher.add_all_student_results',[$session_id,$semester_id,$course_id,$course_credit]) }}"
method="POST">
<table class="table table-bordered table-hover table-sm">
<thead>
<tr>
<th>IMG</th>
{{-- <th>Department Name</th> --}}
<th>Reg.No</th>
<th>ID.No</th>
<th>Student Name</th>
<th>Attendance</th>
<th>Class Test</th>
<th>PartA</th>
<th>PartB</th>
{{-- --}}
</tr>
</thead>
<tbody>
#csrf
#php $i=1;
#endphp
#foreach($semester_students as $semester_student)
<tr data-widget="expandable-table" aria-expanded="false">
<td>
<input type="hidden" name="student_data[{{ $semester_student->id }}][student_id]" value={{ $semester_student->id }} placeholder="add marks"
class="form-control">
</td>
<td><img class="img-circle img-bordered-sm" src="" alt="U" width="50"></td>
<td>{{$semester_student->registration_number}}</td>
<td>{{$semester_student->roll_number}}</td>
<td>{{$semester_student->firstname}} {{$semester_student->lastname}}</td>
<td>
<input type="text" name="student_data[{{ $semester_student->id }}][attendance]" placeholder="add marks" class="form-control">
</td>
<td>
<input type="text" name="student_data[{{ $semester_student->id }}][class_test]" placeholder="add marks" class="form-control">
</td>
<td>
<input type="text" name="student_data[{{ $semester_student->id }}][parta]" placeholder="add marks" class="form-control">
</td>
<td>
<input type="text" name="student_data[{{ $semester_student->id }}][partb]" placeholder="add marks" class="form-control">
</td>
</tr>
#endforeach
</tbody>
</table>
<button type="submit" class="btn btn-success mt-1"><i class="fa fa-check-circle" aria-hidden="true"></i>
Save</button>
</form>
Now use it in your controller as request()->student_data
Let's loop it;
foreach(request()->student_data as $student_id => $student_row) {
// you can do whatever you want with each row
$student_row['attendance']; // you can access all inputs in this way and run your insert queries for each of rows
}
If you want to use validation on array inputs you can still achieve it;
$request->validate([
'student_data.*.student_id' => 'required|numeric',
'student_data.*.attendance' => 'required',
// and others...
]);

How to set table cells values independently with using x-for alpine.js?

I am trying to make a table includes table in every row, and nested x-for is used with alpine.js.With clicking the button located in the lower right corner of the table, new row is added and all cells in the rows have the same value. I couldn't set the rows' values independently. Could you please help?
Thanks in advance for your help,
Cem
function handler() {
return {
fields1: [],
fields: [],
rows: [],
addRate() {
this.fields1.push({
value1: '',
value2: '',
value3: '',
rType: '',
typeA: '',
value4: '',
typeBDateFrom: '',
typeBDateTo: ''
});
},
listField() {
this.fields.splice(0);
this.fields1.splice(0);
this.rows.push({
id: this.id++
});
this.fields1.push({
value1: '',
value2: '',
value3: '',
rType: '',
typeA: '',
value4: '',
typeBDateFrom: '',
typeBDateTo: ''
});
this.fields.push({
value5: '3',
value6: '20',
value7: 'DC',
value8: '40',
value9: '523',
value10: '654',
value11: 'TEST',
value12: 'true',
value13: 'Class1',
value14: '5656',
value15: 'TRM78799',
id:'1'
});
},
}
};
function setDecimal(event) {
this.value = parseFloat(this.value).toFixed(3);
};
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="container col-md-10 offset-md-1">
<div class="row" x-data="handler()" x-init="listField()">
<div class="col">
<table class="table align-items-center table-sm" style="text-align: center;">
<tr>
<template x-for="(row, index) in rows" :key="index">
<p>
<table class="table align-items-center table-sm table-bordered" style="text-align: center;">
<tbody>
<template x-for="(field1, index) in fields1" :key="index">
<tr class="table-success">
<td style="width:100px;"><b>Type</b></td>
<td style="width:100px;">
<select x-model="field1.rType" class="custom-select" name="rType"
id="rType" required>
<option selected>---</option>
<option value="Type A">Type A</option>
<option value="Type B">Type B</option>
</select>
</td>
<td x-show="field1.rType==='Type A'" style="width:120px;">
<b>Type A Name</b>
</td>
<td x-show="field1.rType==='Type A'">
<input x-model="field1.typeA" type="text" class="form-control"
name="typeA" id="typeA">
</td>
<td x-show="field1.rType==='Type A'" style="width:150px;">
<b>Value4</b>
</td>
<td x-show="field1.rType==='Type A'">
<input x-model="field1.value4" type="text" class="form-control"
name="value4" id="value4">
</td>
<td x-show="field1.rType==='Type B'" style="width:120px;">
<b>From</b>
</td>
<td x-show="field1.rType==='Type B'">
<input x-model="field1.typeBDateFrom" type="Type B" class="form-control"
name="typeBDateFrom" id="typeBDateFrom">
</td>
<td x-show="field1.rType==='Type B'" style="width:150px;">
<b>To</b>
</td>
<td x-show="field1.rType==='Type B'">
<input x-model="field1.typeBDateTo" type="Type B" class="form-control"
name="typeBDateTo" id="typeBDateTo">
</td>
</tr>
</template>
</tbody>
</table>
</p>
<p>
<table class="table align-items-center table-sm table-bordered" style="text-align: center;">
<thead class="thead-dark">
<tr>
<th>No</th>
<th style="width:20px;">Value5</th>
<th style="width:80px;">Value6</th>
<th style="width:150px;">Value7</th>
<th style="width:120px;">Value8</th>
<th style="width:120px;">Value9</th>
<th style="width:130px;">Value11</th>
<th style="width:180px;">Value12</th>
<th style="width:180px;">Value1</th>
<th style="width:180px;">Value2</th>
<th style="width:180px;">Value3</th>
</tr>
</thead>
<tbody>
<template x-for="(field, index) in fields" :key="index">
<tr>
<td x-text="index + 1"></td>
<td x-text="field.value5"></td>
<td x-text="field.value6"></td>
<td>
<p x-text="field.value7"></p>
<p
x-show="field.value7==='RFR' | field.value7==='RFRHC'">
<span x-text="field.value8"></span><span>
℃</span>
</p>
</td>
<td x-text="field.value9"></td>
<td x-text="field.value10"></td>
<td x-text="field.value11"></td>
<td x-show="field.value12">
<p>
<label><b>Value13 </b><span x-text="field.value13"></span></label>
</p>
<p>
<label><b>Value14 </b><span x-text="field.value14"></span></label>
</p>
</td>
<td x-show="!field.value12">
<p>
</p>
</td>
<td class="table-success">
<input x-model="field.value1" type="number" class="form-control"
onchange="setDecimal" min="0" max="10000000" name="value1"
id="value1" step="0.001" value="0.000" required>
</td>
<td class="table-success">
<select x-model="field.value2" class="custom-select" name="value2"
id="value2" required>
<option selected>---</option>
<option value="USD">USD</option>
<option value="EUR">EUR</option>
</select>
</td>
<td class="table-success">
<input x-model="field.value3" type="text" class="form-control"
name="value3" id="value3">
</td>
</tr>
</template>
</tbody>
</table>
</p>
</template>
</tr>
<tr>
<td colspan="12" class="text-right"><button type="button" class="btn btn-info"
#click="listField()">+ Add Row</button></td>
</tr>
</table>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine#v2.8.2/dist/alpine.min.js" defer></script>

Displaying Validation Error Messages Beside The Input Box in Laravel

<form action="/regvalid", method="POST">
{{ csrf_field()}}
<h2>Personal Information</h2>
<hr>
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="fname" value="{{ old('fname') }}"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lname"></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<div class="radio">
<label><input type="radio" value="male" name="gender">Male</label>
<label><input type="radio" value="female" name="gender">Female<label>
</div>
</td>
</tr>
<tr>
<td>Mobile Phone:</td>
<td><input type="text" name="mp"></td>
</tr>
<tr>
<td>Tel No.:</td>
<td><input type="text" name="telno"></td>
</tr>
<tr>
<td>Birth Date:</td>
<td><input type="date" name="bd" ></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="add"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td>Website:</td>
<td><input type="text" name="website"></td>
</tr>
I have already their error rules in the controller;
$rules = [
'fname' => 'required|max:50',
'lname' => 'required|max:50',
'gender' => 'required',
'mp' => 'required|regex:/^([0-9\s-+()]*)$/|min:11',
'telno' => 'required|min:11|numeric',
'bd' => 'required|date',
'add' => 'required|max:100',
'email' => 'required|email',
'website' => 'required|url',
'username' => 'required|min:6|max:20',
'pass' => 'required|min:6|max:12',
'repass' => 'required|min:6|max:12',
];
and also the code that will show all;
#if(count($errors)>0)
#foreach ($errors->all() as $errors)
{{ $errors}}
#endforeach
#endif
I want to display the error messages on each one of the input boxes besides them. I don't know how.
For Displaying error message
Method 1:
<input type="text" name="firstname">
#if($errors->has('firstname'))
<div class="error">{{ $errors->first('firstname') }}</div>
#endif
Method 2:
#error('firstname')
<div class="error">{{ $message }}</div>
#enderror
These two ways of doing it

In laravel data is not inserting into database

I am new to laravel and doing project using laravel framework please help me to solve this problem.
data is not inserting into table.
routes.php
Route::post('report/save','TestingController#store');
TestingController
public function store(Request $request){
$userId = \Auth::user()->id;
$this->validate($request, [
'from_stk_loc' => 'required',
'testing_date' => 'required',
'casting_date' => 'required',
'debtor_no' => 'required',
'concrete_grade' => 'required',
'testing_age' => 'required',
]);
$test_details['client_id'] = $request->debtor_no;
$test_details['location'] = $request->from_stk_loc;
$test_details['casting_date'] = $request->casting_date;
$test_details['testing_date'] = $request->testing_date;
$test_details['concrete_grade'] = $request->concrete_grade;
$test_details['testing_age'] = $request->testing_age;
$test_details['report_date'] = date('Y-m-d');
$test_detailsId = DB::table('testing_report')->insert($test_details);
}
Model-> Testing.php
<?php
namespace App\Model;
use DB;
use Illuminate\Database\Eloquent\Model;
class Testing extends Model
{
protected $table = 'testing_report';
protected $fillable = [
'client_id',
'location',
'casting_date',
'testing_date',
'concrete_grade',
'testing_age',
];
}
Here is my form code, you can review this and find me a solution.
<div class="col-md-3">
<label for="exampleInputEmail1">{{ trans('message.testing.client') }}</label>
<select class="form-control select2" name="client_id" id="client_id">
<option value="all">Select client</option>
#foreach($customerData as $data)
<option value="{{$data->debtor_no}}">{{$data->name}}</option>
#endforeach
</select>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.from_location') }}</label>
<select class="form-control select2" name="from_stk_loc" id="from_stk_loc">
#foreach($locData as $data)
<option value="{{$data->loc_code}}" <?= ($data->inactive =="1" ? 'selected':'')?>>{{$data->location_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>{{ trans('message.table.casting_date') }}<span class="text-danger"> *</span></label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input class="form-control" id="datepicker" type="text" name="casting_date">
</div>
<!-- /.input group -->
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>{{ trans('message.table.testing_date') }}<span class="text-danger"> *</span></label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input class="form-control" id="datepicker1" type="text" name="testing_date">
</div>
<!-- /.input group -->
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.gradeofconcrete') }}</label>
<input type="text" placeholder="{{ trans('message.form.gradeofconcrete') }}" class="form-control valdation_check" name="concrete_grade" value="">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.testing_age') }}</label>
<input type="text" placeholder="{{ trans('message.form.testing_age') }}" class="form-control valdation_check" name="testing_age" value="">
</div>
</div>
<div class="row">
<div class="col-md-12">
<!-- /.box-header -->
<div class="box-body no-padding">
<div class="table-responsive">
<table class="table table-bordered" id="purchaseInvoice">
<tbody>
<tr class="tbl_header_color dynamicRows">
<th width="5%" class="text-center">{{ trans('message.table.sno') }}</th>
<th width="10%" class="text-center">{{ trans('message.table.wt') }}</th>
<th colspan="2" width="10%" class="text-center">{{ trans('message.table.load') }}</th>
<th colspan="2" width="10%" class="text-center">Comp Strength in M. Pa</th>
<th width="15%" class="text-center">Avg Strength</th>
<th width="15%" class="text-center">{{ trans('message.table.remark') }}</th>
<th width="5%" class="text-center">{{ trans('message.table.action') }}</th>
</tr>
<tr class="tbl_header_color dynamicRows">
<th width="5%" class="text-center"></th>
<th width="10%" class="text-center"></th>
<th width="10%" class="text-center">{{ trans('message.table.machine') }}</th>
<th width="10%" class="text-center">{{ trans('message.table.calibrated') }}</th>
<th width="10%" class="text-center">7 Day's</th>
<th width="10%" class="text-center">27 Day's</th>
<th width="10%" class="text-center"></th>
<th width="15%" class="text-center"></th>
<th width="5%" class="text-center"></th>
</tr>
<tr class="custom-item"><td class="add-row text-danger"><strong>Add Custom Item</strong></td><td colspan="8"></td></tr>
</tbody>
</table>
</div>
<br><br>
</div>
</div>
<!-- /.box-body -->
<div class="col-md-12">
{{ trans('message.form.cancel') }}
<button type="submit" class="btn btn-primary btn-flat pull-right" id="btnSubmit">{{ trans('message.form.submit') }}</button>
</div>
</div>
</form>
Problem:
When I click submit button nothing is happening at all.
Any help would be highly appreciated.
This is view
<div class="col-md-3">
<label for="exampleInputEmail1">{{ trans('message.testing.client') }}</label>
<select class="form-control select2" name="client_id" id="client_id">
<option value="all">Select client</option>
#foreach($customerData as $data)
<option value="{{$data->debtor_no}}">{{$data->name}}</option>
#endforeach
</select>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.from_location') }}</label>
<select class="form-control select2" name="from_stk_loc" id="from_stk_loc">
#foreach($locData as $data)
<option value="{{$data->loc_code}}" <?= ($data->inactive =="1" ? 'selected':'')?>>{{$data->location_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>{{ trans('message.table.casting_date') }}<span class="text-danger"> *</span></label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input class="form-control" id="datepicker" type="text" name="casting_date">
</div>
<!-- /.input group -->
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>{{ trans('message.table.testing_date') }}<span class="text-danger"> *</span></label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input class="form-control" id="datepicker1" type="text" name="testing_date">
</div>
<!-- /.input group -->
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.gradeofconcrete') }}</label>
<input type="text" placeholder="{{ trans('message.form.gradeofconcrete') }}" class="form-control valdation_check" name="concrete_grade" value="">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.testing_age') }}</label>
<input type="text" placeholder="{{ trans('message.form.testing_age') }}" class="form-control valdation_check" name="testing_age" value="">
</div>
</div>
<div class="row">
<div class="col-md-12">
<!-- /.box-header -->
<div class="box-body no-padding">
<div class="table-responsive">
<table class="table table-bordered" id="purchaseInvoice">
<tbody>
<tr class="tbl_header_color dynamicRows">
<th width="5%" class="text-center">{{ trans('message.table.sno') }}</th>
<th width="10%" class="text-center">{{ trans('message.table.wt') }}</th>
<th colspan="2" width="10%" class="text-center">{{ trans('message.table.load') }}</th>
<th colspan="2" width="10%" class="text-center">Comp Strength in M. Pa</th>
<th width="15%" class="text-center">Avg Strength</th>
<th width="15%" class="text-center">{{ trans('message.table.remark') }}</th>
<th width="5%" class="text-center">{{ trans('message.table.action') }}</th>
</tr>
<tr class="tbl_header_color dynamicRows">
<th width="5%" class="text-center"></th>
<th width="10%" class="text-center"></th>
<th width="10%" class="text-center">{{ trans('message.table.machine') }}</th>
<th width="10%" class="text-center">{{ trans('message.table.calibrated') }}</th>
<th width="10%" class="text-center">7 Day's</th>
<th width="10%" class="text-center">27 Day's</th>
<th width="10%" class="text-center"></th>
<th width="15%" class="text-center"></th>
<th width="5%" class="text-center"></th>
</tr>
<tr class="custom-item"><td class="add-row text-danger"><strong>Add Custom Item</strong></td><td colspan="8"></td></tr>
</tbody>
</table>
</div>
<br><br>
</div>
</div>
<!-- /.box-body -->
<div class="col-md-12">
{{ trans('message.form.cancel') }}
<button type="submit" class="btn btn-primary btn-flat pull-right" id="btnSubmit">{{ trans('message.form.submit') }}</button>
</div>
</div>
</form>
To store data I would give your html elements name as db field and use simple:
$testing = new Testing();
$testing->fill($request->all());
$testing->client_id = $request->your_client_id_from_source;
$testing->user_id = auth()->user()->id;
$testing->save();
So you will use directly laravel ORM and not raw db queries.
Your Model can only be used if your using Eloquent Queries. So based on your code your not using the model because your using Query Builder
I have 2 suggestions:
Check your date format "date('Y-m-d')" make sure that mysql allow this.
Change your $request->from_stk_loc to $request['from_stk_loc'] and the rest if the codes.
Try again, let me know if that helps.
First of all, I agree with everyone recommending Eloquent's ORM functionality over 'raw' DB queries.
As for a possible explanation: you are trying to insert into a non-fillable column: 'report_date', which might not be nullable in this table.
So try this in your model:
protected $fillable = [
'client_id',
'location',
'casting_date',
'testing_date',
'concrete_grade',
'testing_age',
'report_date'
];
Try this:
use App\Model\Testing;
public function store(Request $request){
$userId = \Auth::user()->id;
$validator = Validator::make($request->all(), [
'from_stk_loc' => 'required',
'testing_date' => 'required',
'casting_date' => 'required',
'debtor_no' => 'required',
'concrete_grade' => 'required',
'testing_age' => 'required',
]);
if( $validator->fails() )
{
return redirect()->back()->withInput()->withErrors($validator);
} else {
$testingObj = new Testing;
$testingObj->client_id = $request->client_id;
$testingObj->location = $request->location;
$testingObj->casting_date = $request->casting_date;
$testingObj->testing_date = $request->testing_date;
$testingObj->concrete_grade = $request->concrete_grade;
$testingObj->testing_age = $request->testing_age;
$testingObj->testing_age = $request->testing_age;
$testingObj->report_date = date('Y-m-d'); // try date('Y-m-d H:i:s') if datatype is datetime
$testingObj->save();
$test_detailsId = $testingObj->id;
}
Finally solved this problem by adding <input type="hidden" value="{{csrf_token()}}" name="_token" id="token"> in view page
please make sure in model fillable fields are respective field is mentioned ,correct with no extra space accurately. In your model you have not mentioned report_date field.
namespace App\Model;
use DB;
use Illuminate\Database\Eloquent\Model;
class Testing extends Model
{
protected $table = 'testing_report';
protected $fillable
= [
'client_id',
'location',
'casting_date',
'testing_date',
'concrete_grade',
'testing_age',
//add this to the end.
'report_date'
];

Resources