Laravel Crud Application Editing a record - php

I have a problem with my Laravel crud application for Registrations.
There are these tables: Registration, ExtraRegistration (with a registration_id, extra_id and extraoptions_id),
Extra and ExtraOptions (with the Extra_id).
In the RegistrationController when i add a Registration it makes a new record in the ExtraRegistration with the extraoptions_id and the extra_id. the extra_id is the name of the option and the extraoptions_id is the id of the option you selected.
But now, when you click on edit a record, it shows all the information. the problem is that when you change the extraoption, it makes another record, and not change the select.
And when you have edited something and you look at it again, it still shows the option before you edited it.
RegistrationController
$options = Extra::where("exa_form_id", $distance->asd_form_id)->get();
foreach($options as $option){
$input_name = "option_" . $option->exa_id;
$input_option = $request->$input_name;
if(!is_null($input_option)){
$input_name_extra = "extraoptions_" . $option->exa_id;
$input_option_extra = $request->$input_name_extra;
$registrationextra = new ExtraRegistration();
$registrationextra->iea_registration_id = $registration->isg_id;
$registrationextra->iea_extra_id = $input_option;
$registrationextra->iea_extraoption_id = $input_option_extra;
$registrationextra->iea_price = $option->exa_price;
$registrationextra->save();
}
}
$registration->isg_options = $input_option;
$registration->isg_option_extra_id = $input_option_extra;
Edit:
I want a check before it makes a new ExtraRegistration. that if the price that is in the registration already exists in that registration_id(in ExtraRegistration) it doesnt make another record in the ExtraRegistration.
I want to use an if to fix the problem, but not sure where to begin..
Thanks in advance!

It will probably be easier for you if you just use firstOrCreate method that Laravel provides out of the box.
You can check it here:
https://laravel.com/docs/5.8/eloquent#other-creation-methods
That way, you will be able to retrieve the record that you are looking for, if there is any and then perform all the changes on it. If that record doesn't exist, Laravel will create the new record for you which you'll be able to update.
I hope this helps!

You are creating new object of ExtraRegistration() so its inserting/creating new record every time.
Just changes this line
$registrationextra = ExtraRegistration::find($id);//any id to fetch record

Related

Codeigniter unknown column in where clause?

I am trying to create an update function in Codeigniter using a dropdown menu. Unfortunately, when I click submit I get an error that there is an "Unknown column 'stages.id' in my where clause. I've used the stages.id in other parts of my code to update other fields, and am not sure why this is happening here. Any assistance or thoughts on why this might be happening would be greatly appreciated!
function update_stage_name($id){
$stage_name=array(
'stage_name'=>$this->input->post('stage_name')
);
$this->db->where('stages.id', $id);
$this->db->update('stage_names', $stage_name);
}
This is what the database query looks like according to the error:
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stages`.`id` = '264'
I have several tables, and stages is sort of the main one. Stages each only have one stage_name but a stage_name can belong to multiple stages. Stage_name does not have a foreign key from stages, but stages has a stage_name_id.
EDIT: So it seems the issue was actually that I wanted to update the stages table, and not the stage_names table. But the problem is that the stages table has a stage_name_id but the input from my form is for stage_name. How do I get to the id in the same method? I'm still new to active record and codeigniter and i'm not sure how to proceed.
As the error says you dont have a column that can be referenced using stages.id. Mainly because its the wrong table..
Try..
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stage_names`.`id` = '264'
//Note changed the table name in the where
If you need to use the stages.id and (as you say in your comment) you dont have an FK then you will need to create one as the database isnt smart enough to guess it. So once you have a field in stages_names called stage_id (or similar) you can do..
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stage_id` = '264'
Try this:
function update_stage_name($id) {
$stage_name=array(
'stage_name'=> $this->input->post('stage_name')
);
$this->db->where('stage_names.id', (int)$id);
$this->db->update('stage_names', $stage_name);
}

PHP multi-step form with neo4j

I am making multi step form for submission of a company information in PHP with neo4j graph database. In first step there is a submission of basic information and in second step , some advanced information for a user to fill.
Then problem is that when I am creating a company node in the first step of the form, it is created successfully but in the next step I am unable to get company id to store Step 2 information of this company. The step 2 form resides in another file.
I am using AJAX form submit method.
I basically need the company name or id that is generated in the first step, in the second step form to store step 2 information of the company.
Adding some code could only be helpful for us.
However, if you what you want to achieve is close to the lastInsertId in PDO/Mysql for e.g., you can achieve it with neo4j too with the RETURN statement that will return you the created node and you can get the id from it, pass it to the second step of your form and retrieve the node with the passed id.
The following code is an example using the PHP Client from Neoxygen https://github.com/neoxygen/neo4j-neoclient :
// Creating your company node
$q = 'CREATE (c:Company {name:"My Awesome Company"}) RETURN c';
$result = $client->sendCypherQuery($q);
$id = $result->getSingleNode()->getId();
Pass now the id to the next step of your form, and load the company from the id :
$id = $_SERVER['POST']['id'];
$q = 'MATCH (c:Company) WHERE id(c) = {company_id} RETURN c';
$params = array('company_id' => $id);
$result = $client->sendCypherQuery($q, $params);
$company = $result->getSingleNode();
// Want to get some info of the node ?
$companyName = $company->getProperty('name');
Hope it helped.
Chris

$this->Agreements->save() - create only one record, why?

I need to read all records from table agreements, make changes in filed payments, and update all records, save to table. So, my problem is, that save() only create empty record. Do not update exists record. I show you how:
Reading from table:
$agreements = $this->Agreement->find('all');
$payments = $this->Payment->find('all');
Manipulation on fields (part of)(example):
$id=0;
foreach ($agreements as $agreement):
for ($i=$first_agreement; $i<=$last_agreement; $i++){
if ( $agreement['Agreement']['agreement_number']==$i){
$agreements[$id]['Agreement']['payment']=$payd[$i];
}
}
$id++;
endforeach;
Writting to table:
$this->Agreement->save();
A echo debug($agreements) shows correct array, i have tryed also :
$this->Agreement->save($agreements);
or
$this->Agreement->save($this->request->data);
Can you help/explain me how to write all record?
Cake 2.5.2 PHP : 5.4.4-14
Model::save() only saves a single record. If you want to save multiple records you need to use Model::saveAll() or Model::saveMany().

Setting cookie in codeigniter with one name and several values

I want to set a cookie in Codeigniter whenever the user clicks on "add to my favorites". But I'm confused. Because I have to add several items with one name at the same time. You know this is not possible and the CI overrides the previous values. Look at this:
$this->input->set_cookie(array("name"=>'fav', 'value'=>2500, 'expire'=>100000));
$this->input->set_cookie(array("name"=>'fav', 'value'=>3500, 'expire'=>100000));
$this->input->set_cookie(array("name"=>'fav', 'value'=>4500, 'expire'=>100000));
And when I try to get fav value using this function:
printer($this->input->cookie("fav"));
I get this result:
4500
How should I set a cookie for user when they ad an item to their favorite list so that in the moment of retrieving them I know what to retrieve. I cannot use database because this implementation is for the users who are not registered members.
You can use $this->session->set_userdata for storing values.
$fav = $this->session->userdata("my_favs");// get existing list
$fav[] = $new_fav; // append new items to list
$this->session->set_userdata(array("my_favs"=>$fav)); // update session with existing one.
To print all items
$fav = $this->session->userdata("my_favs")
foreach($fav as $fitems)
echo $fitems."<br/>";
I think you should use print_r() instead of printer().
more than that, you should use:
$this->input->get_cookie("fav");
Have a look here for more information: Cookie helper

Possible to manually set ID of new rows with RedBean PHP?

I'm using RedBean PHP to dump some data from a web scrape into a database, and I need to retain the legacy IDs, preferably as the primary key field. Is it possible to do this with RedBean?
When I try to set the id as so:
$bean->id = 56;
The row doesn't get inserted - the query that ends up being created instead becomes an "UPDATE WHERE id=56", which does nothing since the record doesn't exist yet.
I solved this by inserting a placeholder with an SQL exec first:
$id = 60000;
$name = 'bob';
R::exec('insert into person(id) values('.$id.')');
$person = R::dispense('person');
$person->id = $id;
$person->name = $name;
R::store($person);
Forcing a specific value on an auto-increment primary index is just asking for race-condition problems. I'd suggest you create a new column legacy_id instead where you save the alternative id.
I have started an redbean github project for this, first version supports to have custom id fields.
You have to tell redbean what the name of the id field is (if not "id")
R::getRedBean()->idFieldMap= array('adres'=>'id_adres','contact'=>'contact_id'); // settings for current database
Future improvements: support custom Foreign keys.
https://github.com/freelanceniek/redbeanx

Categories