When i comment the inserting query, the return works.but with the query return is not working.it would be great if anyone can help immediatly
public function regCustomer()
{
$arr = [];
$cus_name = Input::get('cusname');
$cus_address = Input::get( 'address' );
$cus_phone = Input::get( 'phone' );
$cus_email = Input::get( 'email' );
try{
DB::table('Customers')->insert(
array(
'customer_name' => $cus_name,
'address' => $cus_address,
'phone' => $cus_phone,
'email' => $cus_email
)
);
return 1;
}catch(Exception $ex){
return '0';
}
}
replace the catch block parameter to match this:
catch (\Illuminate\Database\QueryException $e)
Related
In my Laravel-8, I have this code:
public function add(Request $request){
if( $request->ajax() ){
$rules = array(
'first_name.*' => 'required',
'country.*' => 'required'
);
$error = Validator::make($request->all(),$rules);
if($error->fails()){
return response()->json([
'error' => $error->errors()->all(),
]);
}
$first_name = $request->first_name;
$country = $request->country;
for( $count = 0; $count < count($first_name); $count++ ){
$data = array(
'first_name' => $first_name[$count],
'country' => $country[$count],
);
$insert_data[] = $data;
}
DynamicField::insert($insert_data);
return response()->json([
'success' => 'Data added Successfully',
]);
}
}
This successfully Inserts record.
How do I do the update to this same code?
I mean public function update ...
Please help me, i just messed up. I just checked the code again and again , manipulate code every time but database not updating.
controller
function updt_ctrl($id="",$userData=""){
$userData = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$res=$this->user_model->updt_row($id,$userData);
if($res){
$this->load->view('profile_view');
}
else{
echo "something is wrong";
}
}
Model
function updt_row($id,$userData){
//$this->db->set($userData);
$this->db->where('id',$id);
return $this->db->update('user',$userData);
}
try this.
CONTROLLER
function updt_ctrl(){
$this->load->helper('user_model');
$userData['fname'] = $this->input->post('fname');
$userData['lname'] = $this->input->post('lname');
$userData['email'] = $this->input->post('email');
$userData['password'] = $this->input->post('password');
$this->user_model->updt_row($id,$userData);
}
MODEL
function updt_row($id,$userData){
$this->db->where('id', $id);
$this->db->update('user',$userData);
//after done update, load this view
$this->load->view('profile_view');
}
If your embed a correct Form action on your view. this should be working 100%
Replace your contoller with this
controller
function updt_ctrl($id){
$userData = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$res=$this->user_model->updt_row($id,$userData);
if($res){
$this->load->view('profile_view');
}
else{
echo "something is wrong";
}
}
I want to import csv file with matlab/Excel at laravel 5.2,
the price should be integer, but when someone input price as string, i need to validate it.
This is my code:
Excel::filter( 'chunk' )->load( $path . $filename )->chunk( 1000, function( $results ) {
foreach ( $results as $import_data ) {
$validation = Validator::make( $import_data->toArray(), [
'code' => 'required|alpha',
'name' => 'alpha',
'number' => 'alpha',
'price' => 'numeric',
'pages' => 'numeric',
'delivery_id' => 'numeric'
] );
if( $validation->fails() ) {
return back();
}
if( $binder = Binder::where( 'code', $import_data->code )->first() ) {
$binder->update( [
'price' => $import_data->price,
'pages' => $import_data->pages
] );
} else {
Binder::create( [
'code' => $import_data->code,
'name' => $import_data->name,
'number' => $import_data->number,
'price' => $import_data->price,
'pages' => $import_data->pages,
'delivery_id' => $import_data->delivery_id
] );
}
}
});
After import csv, my return back() is not working at all, the controller still doing the foreach, how to enable my validation?
You're returning from the chunk() method, and then it goes on to call the next iteration of the same chunk() method, and so on. I would suggest wrapping the entire thing in a try-catch-block and throwing an exception instead.
Edit: like this:
try {
Excel::filter( 'chunk' )->load( $path . $filename )->chunk( 1000, function( $results ) {
foreach ( $results as $import_data ) {
// ...
if( $validation->fails() ) {
throw new Exception();
}
// ...
}
)};
} catch (Exception $e) {
return back();
}
i have to update a table and change the value inside it and the table have many column inside it. i tried to use query builder and here is my current code
the query is inside the public function ugetstd($id)
public function ugetstd($id) //update
{
$rules = array(
'firstname' => 'required|max:80|regex:/^[\p{L}\p{N} - ]+$/u',
'middlename' => 'required|max:50|regex:/^[\p{L}\p{N} -]+$/u', //more here
);
$messages = array(
'firstname.required' => 'First name is required',
'firstname.max' => 'First name can only contain 80 characters' //more here
);
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails())
{
return Redirect::to('view_students/' . $id)
->withErrors($validator)
->withInput(Input::except('password'));
}
else
{
DB::table('dbo_students')->where('StudentID', $id )
->update
(
array
(
'FirstName' => Input::get('firstname'),
'MiddleName' => Input::get('middlename'),
'LastName' => Input::get('lastname'),
'CurrentStatusID' => Input::get('scs'),
'Sex' => Input::get('sex'),
'ReligionID' => Input::get('rel'),
'EthnicityID' => Input::get('eth'),
'StreetAddress' => $enAdd,
'CityID' => Input::get('city'),
'YearLevelID' => Input::get('yearlevel'),
'Telephone' => Input::get('telephone'),
'Birthdate' => Input::get('date'),
'Birthplace' => Input::get('birthplace'),
'SchoolLastAttended' => Input::get('schoollastattended'),
'LastGradeCompleted' => Input::get('lastgradecompleted'),
'CurrentModuleLeft' => Input::get('currentmoduleleft'),
'CurrentModuleCriticalLevel' => Input::get('modulecriticallevel'),
'StudentDescription' => Input::get('description')
)
);
return Redirect::to('view_students/' . $id);
}
}
the thing is it does not execute the code. it does not update anything and it does not throw any error. any ideas what i am doing wrong? thanks
I am using codeigniter transaction. i wrote a function but it is not working.It was supposed to complete the transaction while submitting my form. Now its not saving with this code.with out transition code it is working. how can i fix this:
public function twotable_insertData() {
$this->db->trans_start();
$data = array(
'brand_name' => $this->input->post('f_name'),
'brand_user_name' => $this->input->post('l_name'),
);
$brand_id = $this->m_common->insert_row('brands', $data);
echo '$brand_id';
$data1 = array(
'brand_id' => $brand_id,
'stadium_id' => $this->input->post('stadium'),
'concession_stand_no' => $this->input->post('con_std_no'),
);
$this->m_common->insert_row('concession_stands', $data1);
redirect('backend/brand/view_brand');
if ($this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
} else {
echo $this->db->trans_complete();
}
}
I have updated your query...
$this->db->trans_start();
$data = array(
'brand_name' => $this->input->post('f_name'),
'brand_user_name' => $this->input->post('l_name')
);
$brand_id = $this->m_common->insert_row('brands', $data);
// echo '$brand_id';
$data1 = array(
'brand_id' => $brand_id,
'stadium_id' => $this->input->post('stadium'),
'concession_stand_no' => $this->input->post('con_std_no')
);
$this->m_common->insert_row('concession_stands', $data1);
$this->db->trans_complete();
if($this->db->trans_status() === FALSE){
// Check if transaction result successful
$this->db->trans_rollback();
$this->session->set_flashdata('failure', 'Transaction Fails.');
}else{
$this->db->trans_complete();
$this->session->set_flashdata('success', 'Transaction Success.');
}
redirect('backend/brand/view_brand');