How to check if data exsists in the database so you can update it - laravel-5

This code above adds data to the database but it does not update if the data already exsists.
public function updateStudentPastoral(){
// Getting all post data
$data = Input::all();
$pCollection = new PastoralCollection();
$pCollection->fill($data);
$response = '';
if ($pCollection->save())
$response = 'success';
else
$response = 'error';
return (json_encode($response));
}
i want to check if the data already exists so i will update it in the database but if it does exist it will save in the database.

You can use the laravel firstOrnew method like so:
$user = User::firstOrNew(array('name' => Input::get('name')));
$user->foo = Input::get('foo');
$user->save();
This will check if there is a record matching name if there is it will be updated otherwise a new one will be createad.

Related

How can I save iframe content in Laravel?

I have a settings page in my admin panel where the user can paste their Google map iframe code (embed code). The setting options are saved in the database and displayed in various places on the front-end.
My problem is that when I save the settings, the map embed code isn't saved to the database. I guess it's a sanitization problem.
Here is my code from my controller:
public function updateGeneral(Request $request) {
$id = $request->id;
$data = array();
$data['site_title'] = $request->site_title;
$data['meta_description'] = $request->meta_description;
$data['site_lang'] = $request->site_lang;
$data['company_name'] = $request->company_name;
$data['address'] = $request->address;
$data['phone'] = $request->phone;
$data['map'] = $request->map;
$data['email'] = $request->email;
$data['facebook'] = $request->facebook;
$data['twitter'] = $request->twitter;
$data['youtube'] = $request->youtube;
$data['insta'] = $request->insta;
// Update table with new data
DB::table('settings')->where('id', $id)->update($data);
return redirect(route('admin.settings'))->with('successMsg', 'Settings have been updated successfully!');
}
So how can I make sure that the data is saved for:
$data['map'] = $request->map;
Sorry, just realized there was a typoo in form name field. But I also had to change the db field from varchar to text.

update data to database laragon using api

I want to update data to the database in ionic. how to make the data update. Here what I tried. I try using postmen to post the api and it appear success but the data does not change.
in api.php
public function update (Request $request)
{
$id = $request->id;
$medname = $request->medname;
$price = $request->price;
$stock = $request->stock;
$medno = $request->medno;
$ingredient = $request->ingredient;
$description = $request->description;
$addinfo = $request->addinfo;
AddMedicine:: where('medname',$medname)->update([
'id' =>$id,
'medname'=>$medname,
'price'=>$price,
'stock'=>$stock,
'medno'=>$medno,
'ingredient'=>$ingredient,
'description'=>$description,
'addinfo'=>$addinfo,
]);
$msg = "Data Updated";
$datamsg = response()->json([
'success' => $msg
]);
return $datamsg->content();
}
route
Route::put('/update','ApiController#update');
Are you sure use PUT request ? Because need to CSRF token please inspect
https://stackoverflow.com/questions/30756682/laravel-x-csrf-token-mismatch-with-postman

Laravel : How can i get old and new value by updateOrCreate

I want update or create in data base
but i want get the old value and updated value because i want to compare between these two value
for example
this item in table user
name = Alex and Order = 10
so now i want update this person by
name = Alex and Order = 8
Now After updating or creating if not exist
just for update i want get
Old order 10 | And new Order 8
I want compare between these order
i have tryin getChange() and getOriginal() but two the function give me just the new value.
Please Help
You can get the old value using getOriginal if you have the object already loaded.
For example :
$user = User::find(1);
$user->first_name = 'newname';
// Dumps `oldname`
dd($user->getOriginal('first_name'));
$user->save();
However in case of updateOrCreate, you just have the data. I am not sure about a way to do it using updateOrCreate but you can do simply do :
$user = User::where('name', 'Alex')->first();
$newOrder = 10;
if($user){
$oldOrder = $user->getOriginal('order');
$user->order = $newOrder;
$user->save();
}
Is the name unique in the table? Because if it is not you will have updates on multiple rows with the same data.
So the best approach is to use the unique column which is probably the ID.
User::updateOrCreate(
[ 'id' => $request->get('id') ], // if the $id is null, it will create new row
[ 'name' => $request->get('name'), 'order' => $request->get('order') ]
);
Solution
$model = Trend::where('name', $trend->name)->first();
if ($model) {
$model->old_order = $model->getOriginal('order');
$model->order = $key + 1;
$model->save();
} else {
Trend::where('order', $key + 1)->delete();
$new = new Trend();
$new->name = $trend->name;
$new->old_order = $key + 1;
$new->order = $key + 1;
$new->tweet_volume = $trend->tweet_volume;
$new->save();
}

laravel retrieve json and save into database

I am getting cinema title + times using API from Cinelist, I then want to save these values into a database.
At the moment, it does save but only 1 record, the last one. However, I want it to save each one.
Also each time it is run I want to update existing records instead of creating new ones unless there are more results.
So usually there are 5 records, each time I run the function I want to update the database with the new 5 records, however, if it's a different day and there are 6 records I want to update 5 records and insert 1 extra one so there is 6.
My code so far:
function odeon(){
$data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/10565');
$data = json_decode($data);
foreach($data->listings as $listing){
$title = $listing->title;
$time = implode(', ', $listing->times);
$id = + 1;
$films = Film::where('id', $id)->first();
if (!$films) {
$films = new Film();
}
$films->title = $title;
$films->times = $time;
$films->save();
}
}
You may use eloquent's updateOrCreate method to insert non-existent data and update existing data.
function odeon(){
$data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/10565');
$data = json_decode($data);
foreach($data->listings as $listing){
$title = $listing->title;
$time = implode(', ', $listing->times);
Films::updateOrCreate([
'title' => $title,
'$times' => $time
]);
}
}

Laravel: how to update records values coming as arrow

I am new to Laravel and I need to update records coming as array from a form.
$a = $request->id;
$b = $request->val;
Now I need to update the records
Details::find($b)->update(['detail'=>$a]);
The script above obviously does not work...
You can do it in more than one way, try this :
$details = Details::find($request->input($id));
$details->val = $request->input('val');
$details->save();
Or you can use this if the inputs has the same name as the model fields:
$details = Details::findOrFail($request->input($id));
$details->update($request->all());
You can update in the following ways. Assuming detail is your field name to update.
$id = $request->id;
$val = $request->val;
$detail = Details::findOrFail($id);
$detail->detail = $val;
$detail->save();
For the below to work. You need to set the $fillable propery in the model
// In Detail model
protected $fillable = ['detail'];
// Controller
Details::where('id', $id)->update([
'detail' => $val
]);
$a = $request->id;
$b = $request->val;
$detail = Detail::where('id', $a)->first();
$detail->update(['detail' => $b]);

Resources