Reputation: 109
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">
<a href="{{url('/sales/list')}}" class="btn btn-info btn-flat">{{ trans('message.form.cancel') }}</a>
<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">
<a href="{{url('/sales/list')}}" class="btn btn-info btn-flat">{{ trans('message.form.cancel') }}</a>
<button type="submit" class="btn btn-primary btn-flat pull-right" id="btnSubmit">{{ trans('message.form.submit') }}</button>
</div>
</div>
</form>
Upvotes: 1
Views: 7962
Reputation: 1
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'
];
Upvotes: 0
Reputation: 109
Finally solved this problem by adding <input type="hidden" value="{{csrf_token()}}" name="_token" id="token">
in view page
Upvotes: 1
Reputation: 545
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;
}
Upvotes: 0
Reputation: 26
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'
];
Upvotes: 0
Reputation: 1585
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:
Try again, let me know if that helps.
Upvotes: 0
Reputation: 379
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.
Upvotes: 1