I want to update three fields in a three column of my database. Each time i run this, the last field get updated into all fields. Someone please help. Thanks in advance.
public function updateResult(Request $request)
{
$id = $request->student_id;
if(count($request->mark) > 0)
{
foreach($request->mark as $item=>$v){
$data=array(
'mark'=>$request->mark[$item],
'grade'=>$request->grade[$item],
'student_id'=>$request->student_id[$item],
);
Result::where('student_id', $id)->update($data);
}
}
return Redirect()->back();
}
You have fixed id here $id = $request->student_id and everytime you update Result you are updating same row(s) again.
I think you should do this:
public function updateResult(Request $request)
{
// $id = $request->student_id;
// Note: The line above is no longer needed
if(count($request->mark) > 0){
foreach($request->mark as $item=>$v){
$data=array(
'mark'=>$request->mark[$item],
'grade'=>$request->grade[$item]
// Note: There is no need to include student_id here unless you actually intend to change the ID
);
Result::where('student_id', $request->student_id[$item])->update($data);
// Note: In the line above, I replaced $id with $request->student_id[$item] to update the specific student
}
}
return Redirect()->back();
}
Hope this helps you
Related
I'm trying to save new record, all i get is a white page
even dump and dd for $applicant ->save() returns nothing.
public function store(Request $request) {
try {
if (Auth::guest()) {
return redirect()->route('login');
}
$applicant = new Applicant();
$applicant->first_name = $request->get('first_name');
$applicant->middle_name = $request->get('middle_name');
$applicant->last_name = $request->get('last_name');
dump($applicant);
$applicant->save();
dd('Saved');
} catch (Exception $e) {
Log::error("Error during orders creation:" .$e>getTraceAsString());
}
}
I expected the record should be saved
Try this instead
public function store(Request $request){
$applicant = new Applicant;
$applicant->first_name = $request->input('first_name');
$applicant->save();
}
and don't forget to include use App\Applicant model in your Controller class; If it does not solve your problem. please double check your form action post in view section.
I think save function will not return anything.
for data you must use create method.
Used return after save.
$applicant->save();
return response()->json(['success' => 'Data Added successfully.']);
Use create in stead, write less code if you can us always better.
Try this:
Application::create($request->all());
Or change it the way you need by assigning every array key to its value
I'd One text_box(txtname) & Add button in View . I've to restrict user to add already existing(duplicate) data in a database . after clicking on Add button. what changes should I've to do in Model & controller.
I tried to copy+ paste my model & controller code but due to some restrictions of website i'm not getting able to display code. please kindly suggest me what should I've to do ?
try this i hope it will help you.
first of all get data by textAddItem
//controller save data function
//get txtAdditem
$txtAddItem = $this->modelname->method_name($this->input->post('textAddItem'));
if (count($txtAddItem) > 0) {
//already existed
}
else{
$data=array(
$name=>$this->input->post('textAddItem')
);
return $query=$this->db->insert('tbl_category',$data);
}
//modele
function method_name($textAddItem) {
return $this->db->where('cat_name', $textAddItem)
->get('tbl_category')->row_array();
}
First, check the cat_name in the DB before inserting in the controller
Controller
public function function_name(){
$this->load->model('CrudModel');
$data = array('cat_name' => $name);
$if_exists = $this->CrudModel->check_category($data);
if($if_exists > 0){
//Already Exists
}else{
//New insertion
}
}
Model
public function check_category($data){
return $this->db->get_where('tbl_cateogry', $data)->num_rows();
}
public function insertMenurecord()
{
$this->load->model('CrudModel');
$this->form_validation->set_rules('txtMenuItem', 'Menu','required|is_unique[tbl_menumaster.menu_name]');
if ($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('msg','    Inserted record already exist');
redirect('Welcome/displayMasterMenu');
}
else
{
$this->CrudModel->saveMenudata();
$this->session->set_flashdata('msg','You have successfully Inserted record');
redirect('Welcome/displayMasterMenu');
}
}
I'm trying to fill already existing models with UUID. And every time it says that UUID is not unique! What a bloody hell! )))
function unique_guid($model){
$guid = com_create_guid();
$table = $model->table;
$db_table = Db::table($table);
$req = $db_table->where("guid", strval($guid));
$instance = $req->first();
if(is_object($instance)){
$guid = unique_guid($model);
return;
}else{
try{
$model->guid = $guid;
$model->save();
sleep(2);
return $guid;
}catch(PDOException $Exception){
$guid = unique_guid($model);
}
}
}
It keeps circling in try/catch block and telling me that it is not unique,
i checked and there is no record with generated UUID.
Also - it brokes at third-fourth iteration, and if i add sleep(5) it works longer - 10 iteration and then brokes.
What in the world can it be?
Laravel 5.5, Postgres 9
Following the Laravel 6.0 official doc, I proceeded the following way:
I added the uuid-ossp extension at database creation. Then, my migrations were like:
use Illuminate\Database\Query\Expression;
...
public function up()
{
Schema::create('my_table', function (Blueprint $table) {
$table->uuid('guid')->primary()->default(new Expression('uuid_generate_v4()'));
...
});
}
I find it a lot more elegant like this.
I believe com_create_guid is not generating unique on every iteration, so please use some increment/prefix value to generated UUID, also if possible use uniqid instead of com_create_guid() uniqid generates id based on the current time in microseconds.
function unique_guid($model,$increment=0){
$guid = uniqid($increment,TRUE);
$table = $model->table;
$db_table = Db::table($table);
$req = $db_table->where("guid", strval($guid));
$instance = $req->first();
if(is_object($instance)){
$guid = unique_guid($model,++$increment);
return;
}else{
try{
$model->guid = $guid;
$model->save();
sleep(2);
return $guid;
}catch(PDOException $Exception){
$guid = unique_guid($model,++$increment);
}
}
}
I think you created function to generate UUID but there is some other better ways to generate UUID. You are using Laravel so you can search packages which are generating UUID.
There is a package I'm suggesting to use.
https://github.com/ramsey/uuid
These types of packages are useful and fully tested by community. So we can trust on them.
You can try like this.. it will check until the guid is really unique
public static function generateUniqueNumber($model = null, $column = null)
{
$num = null;
if ($model) {
$range = $model::all()->pluck('guid')->toArray();
while (in_array($num = com_create_guid(), $range)) {}
}
return $num;
}
I think it's some bug in Postgres 9.3. It inserting two rows at a time on some point of the script. I've eneded with just a migration like that
public function up()
{
DB::statement('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";');
DB::statement('ALTER TABLE feegleweb_octoshop_products ADD COLUMN guid uuid DEFAULT uuid_generate_v4() UNIQUE;');
}
It fill all my rows with unique uuid
how to extract single data of row id and not other from same column in laravel 5.3 and view it on browser. i have tried an example please see this and give me feedback as soon as possible. thanks in advance.
public function index($id)
{
$data = $this->issue->find($id);
if(is_null($data))
{
return abort(404);
}
$data = DB::table('issue')->select('id','return_date')->get();
return view('library.after',['issue'=>$data]);
}
and this is my view
#foreach($issue as $is)
{{'<ID="'.$is->id.'" label="'.$is->return_date.'">' }}<br>
#endforeach
if you want to get only one row then fetch one row from database.
public function index($id)
{
$data = $this->issue->find($id);
if(is_null($data))
{
return abort(404);
}
$data = DB::table('issue')->select('id','return_date')->first();
return view('library.after',['issue'=>$data]);
}
in view
{{'<ID="'.$issue->id.'" label="'.$issue->return_date.'">' }}<br>
I have one table users in this i store 1 for website and 2 for domain and i display this table my code is,
$this->load->library('grocery_CRUD');
$crud = new grocery_CRUD();
$crud->set_table('users');
$crud->callback_column('listing_type',array($this,'type_change'));
$output = $crud->render();
return $output;
and my callback_column function,
public function type_change($value, $row)
{
if($value == 1)
{
return 'Website';
}
if($value == 2)
{
return 'Domain Name';
}
}
so,for this search is not working for listing type if anyone have solution for this please help me , thank you.
You can change the theme use ex: datatables the search is client-side on visible data.
Attention: This solutions maybe not be good for a big number of row.