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
]);
Related
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;
this is my code
public function GetUsersWhoNeedPayments()
{
$All_DB_Email_Info = DB::table('users')->pluck('email');
foreach($All_DB_Email_Info as $ADEI){
$DisplayUserToReceivePaymentFrom = Auth::user()->where([
['paired_with', '=', $ADEI]
])->get();
var_dump($DisplayUserToReceivePaymentFrom->name);
}
}
So I created the variable $All_DB_Email_Info which selects the email column. my goal is to get the email saved in my DB...every user is paired to make payment to other users...so i am paired to make payment to johndoe#example.com,,i want the check the db for johndoe#example.com and then check for he's ->name, phonenumber->, ->age, etc..but when i reload my page the page says 'Exception in Colection.php line 1498: Property [name] does not exist on this collection instance.' Please help me out
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 : '-');
I have project with 3 application's sharing one codeigniter installation. It was working excellent until I changed name of mysql table, and changed query to reflect new changes to the database. Now it looks like my insert query is cached, and trying to insert value into old table (which doesn't exist anymore). This is what I've tried so far:
CI:
$this->db->cache_delete_all()
mysql:
RESET QUERY CACHE;
In config/database.php:
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
When db_debug configuration is set to TRUE it gives 500 error, when FALSE, I was able to catch error and it turn's out it's trying to insert into old table that doesn't exist anymore. I don't get how is this possible, I've even searched my complete project and can't find old table name anywhere in my project, also CI cache folders are empty. Any help would be appreciated.
EDIT: My insert code:
if($this->db->insert('booking_calendar', $data))
{
$booking_id = $this->db->insert_id();
$this->load->helper('string');
$order_id = strtoupper(random_string('alnum', 6)) . $booking_id;
$data = array(
'id' => $order_id,
'booking_calendar_id' => $booking_id,
'status' => 'PAYMENT_PENDING'
);
if($this->db->insert('bookings', $data))
{
$this->notifications->add($MYSELF['id'], 'Court successfully booked.');
return $order_id;
}
$this->notifications->add($MYSELF['id'], 'Court booking failed.', 'warning');
return false;
}
else
{
log_message('ERROR', 'Insert booking_calendar failed. ' . print_r($this->db->_error_message(), true));
}
And this is the output in my log:
ERROR - 2013-09-23 20:05:13 --> Insert booking_calendar failed. Table 'tennis.courts_calendar' doesn't exist
Notifications is custom class which just insert one row in "notifications" table.
I found answer to my problem, I had ON BEFORE INSERT/UPDATE triggers on my table, and when I changed it's name it turns out that inserting on the same table with new name was still triggering old table name instead of new one. So I just updated triggers and now it's working. Hope this will save time for someone else, because it took me a while even if it's looking trivial from this point of view now.