I have a form where I am adding some data to db, but I want to avoid duplicate records if user clicks multiple times on the button, I can disable the button using JS but I want to have some checking on server side as well.
Currently on form I am setting a session variable with random number and sending it to controller using textbox (hidden) and then in controller I check if session variable is equal to textbox then add to db - but still the data adds multiple time in db, would appreciate if someone could help. Thanks.
Controller:
if ($request->token == session('test')){
session()->forget('test');
sleep(20); (this i added in order to test)
TableName::create([
'code' => 'test',
'name' => 'testing',
]);
return "done";
} else {
return "stopped";
}
Blade:
{{session(['test'=> rand()])}}
<input type="text" value="{{session('test')}}" name="token">
There are two methods in Laravel firstOrCreate or firstOrNew.
Refer https://laravel.com/docs/5.8/eloquent
The firstOrNew method, like firstOrCreate will attempt to locate a record in the database matching the given attributes. However, if a model is not found, a new model instance will be returned
// Retrieve flight by name, or create it with the name, delayed, and arrival_time attributes...
$flight = App\Flight::firstOrCreate(
['name' => 'Flight 10'],
['delayed' => 1, 'arrival_time' => '11:30']
);
You can check with not exist in MYSQL, check Below
INSERT INTO table_listnames (name, address, tele)
SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE name = 'Rupert'
) LIMIT 1;
Related
I want to get data from CF7 checkboxes and then save them to the same column in the MySQL database.
For example form is like that:
First and Last name: First Last
What do you like:
X cars,
X bikes,
X planes
Where did you hear about us? (Dropdown)
FB
IG
Google
X is checked box.
Script I use for saving values excluding the multiple checkbox:
$firstLastName = $data["first-last"];
$marketing_source = $data["marketing_source"];
$mydb->insert(
'TableName',
array(
'firstLastName' => $firstLastName,
'marketing_source' => $marketing_source[0],
),
array(
'%s','%s',
)
);
So I want to add a line that will also save multiple selections when user selects more fields in checkbox.
'like' => $like[0].", ".$like[1].", ".$like[2].", ".$like[3],
this code when you want to put values to MySQL db.
I need to check if the value in the Table is existed skip it otherwise save it in the table below is the code.
else if(!empty($checkActivity))
{
//dd($checkActivity);
foreach($activitydetails->acb as $ac){
$acd=new ActivityFinance();
$acd->project_id=$project_id;
$acd->account_code=$ac->account_code;
$acd->activity_budget=$ac->activity_budget;
$acd->exact_title=$ac->exact_title;
$acd->created_by=Auth::user()->id;
$acd->save();
}
}
if we dd($activity->acb);
and if we dd the $checkActivity it shows the following data
in simple words, there is two arrays, one came from the database and the other from View how to check if the view sent array is in Database?
Thanks
i have use the laravel function updateOrCreate() it will check if the entry exist it will ignore or create new , if already in database table then it will update not ID below are the codes
ProjectFinance::updateOrCreate([
'project_id'=>$project_id,
],[
'finance_budget'=>$finance->finance_budget,
'finance_currency'=>$finance->finance_currency,
'created_by'=>Auth::user()->id
]);
$upsertActvity=$activitydetails->acb;
foreach($upsertActvity as $up)
{
ActivityFinance::updateOrCreate([
'id'=>$up->id,
'project_id'=>$project_id,
],[
'account_code'=>$up->account_code,
'activity_budget'=>$up->activity_budget,
'exact_title'=>$up->exact_title,
'created_by'=>Auth::user()->id
]);
I have a page to get some data from a user, this data will save in first_forms table, but I have other fields in the table for next page and they are null.
this is first_forms table :
In this table, project_name, customer_name, buyer, response, model will be saved in the first step in the first page choose.blade.php
This is store function in the controller:
FirstForm::create([
'model' => $request['model'],
'project_name' => $request['project_name'],
'customer_name' => $request['customer_name'],
'buyer' => $request['buyer'],
'response' => $request['response'],
]);
return view('Form2.firstForm');
As you can see other fields in first_forms table will be null. After this user redirect to next page firstForm. Now in this page users have to complete other fields like: airflow, tcp, compress_number, etc. I want when user click on submit in this page, new fields in this page save in the previous row in first_forms table who complete in the previous page, how can I do this?
I think I have to get the id of who completes he previous page and update row with new fields in a new page, but I don't know how I can get the id and update row. Primary key is id.
This is first_forms table when user complete first page :
This is the data of who completed the first page. On the next page, those fields are null, have to be complete in this row.
You can simply check that
$first = FirstForm::create([
'model' => $request['model'],
'project_name' => $request['project_name'],
'customer_name' => $request['customer_name'],
'buyer' => $request['buyer'],
'response' => $request['response'],
]);
//Below line will re-retrieve the record along with primary key which is `id`
$first = $first->fresh();
return view('Form2.firstForm',compact('first'));
In view you can access inserted data like $first->id Now place your id in a hidden field of the nextform and update your record according to that id. You can even show the previously inserted record in readonly fields of the nextform
In your nextform view
<input type="hidden" name="update_id" value="{{ $first->id ?? '' }}" />
When you submit the nextform with all other fields, you will get the hidden id as well. So again in your controller method you will do something like
FirstForm::where('id',request('update_id'))->update([
//here goes your other fields of the nextform
]);
This is one of my first applications out of tutorials so I don't know how to express my issue well.
Well I have these 2 tables:
User ( id, code )
Hours ( id, user_id, created)
I want to know how I can add an entry to the Hours table using the user_code.
I tried to grab the data of the User table with the code value and then findBy and pass for the patchEntity but it did not work.
I don't have a whole lot of information to work with, but I'll give it a go.
I want to know how I can add an entry to the Hours table using the
user_code
You mention using patchEntity, so that's updating information that's already there. Assuming user_code is the 'code' column you're talking about there, first find the user by his code:
$users_tbl = TableRegistry::get('Users');
// find the user
$user = $users_tbl->findByCode($user_code)->first();
if ($user) {
// replace '$this->request->data() with whatever patch data you wanted
$users_tbl->patchEntity($user, $this->request->data(), [
'associated' => ['Hours']
]
if ($users_tbl->save($user)) {
// success!
} else {
// error!
}
} else {
// error!
}
It will also depend on how you have the data you passed in (where my '$this->request->data() is, or whatever your array might be) - it needs to match the right column names and be in the correct format listed here.
However, this is updating the data. Just adding the data, you can load the hours table and add a new entry with the user_id acquired from the user search:
$hours_tbl = TableRegistry::get('Hours');
$hours = $hours_tbl->newEntity([
'user_id' => $user->id // $user populated from same method earlier
]);
/* assumed 'id' was autoincrementing and 'created' was populated
through Timestamp behavior */
if ($hours_tbl->save($hours)) {
// yay!
} else {
// boo
}
In my form, I created the value by populating the dropbox from values from a table.
<?php echo $form->dropDownList($model,'status', CHtml::listData(Statusprospect::model()->findAll(), 'id', 'status'),array('prompt' => 'Select')); ?>
When I view the record it has a 1, as it should for status. How do I make it display the value when the record is viewed, instead of the 1.
The view file code that currently displays the field is this:
<?php echo CHtml::encode($data->status); ?>
The Model does have the relationship defined:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'status0' => array(self::BELONGS_TO, 'Statusprospect', 'status'),
);
}
How would I accomplish showing the value instead of the number?
Right now this should work $data->status0->status.
Take care that $data->status0->status might not be set if $data->status can be null so make a check beforehand if that is the case. You can use
CHtml::encode(isset($data->status0->status) ? $data->status0->status : '-');