The data is shown normal, it can be updated and everything, but when saving a record it is not saved and it shows me this error.
public function store(Request $request, Cuenta $cuenta){
$cuenta->id_cliente = $request->select_cliente;
$cuenta->id_plan = $request->select_plan;
$cuenta->id_plan_cuotas = $request->select_cuota;
$cuenta->tipo_cuota = $request->select_tipo_cuota;
$cuenta->id_agente = $request->select_agente;
$cuenta->save();
$data = DB::select("call change_status_create_cuotas(?, ?, ?)",array($request->select_cuota, $request->select_tipo_cuota ,$request->select_cliente));
return redirect()->route('cuentas.index');
}
This is code to save the data, in the localhost it works correctly, but in the hosting it does not leave anything.
Related
For example, I have my codes.
$news = new News();
$news->title = 'hello world';
$new->user = $user_id,
$news->urlcc = DB::raw('crc32("'.$args['newsShortUrlInput'].'")');
$news->save();
$news->refresh();
Here with attribute $news->urlcc comes from user input after using mysql function crc32();
For the SQL injection issue, above codes not safe.
So, my question is how to bind the parameters in DB::raw() with Laravel model something like below.
$news->urlcc = DB::raw('crc32(:newsShortUrlInput)', ['newsShortUrlInput' => $args['newsShortUrlInput]);
Thanks,
I found one solution, not sure it is right or perfect solution.
In News model class to rewrite setAttribute, see below.
public function setUrlcrcAttribute($shortUrl)
{
$this->attributes['urlcrc'] = $this->getConnection()->select('select crc32(?) as urlcrc', [$shortUrl])[0]->urlcrc;
}
In your service class to create a new model like below.
$news = new News();
$news->title = 'hello world';
$new->user = $user_id,
$news->urlcrc = $args['newsShortUrlInput']; // Laravel model will try to build the real attribute urlcrc
$news->save();
$news->refresh();
It works to me, but not sure this is perfect solution or not.
Working with REST API's in Laravel. Can't send raw json request in body using GET method, it returns null values in my server environment. But in local server works fine. Refer the attached images. And my controller code was,
public function slots(Request $request)
{ //return $request->all();
$consult_rule = TRUE;
date_default_timezone_set('Asia/kolkata');
$current_time = date('H:i:s');
$curr_date = date('Y-m-d');
// $appoint_date = DateTime::createFromFormat('d/m/Y', $request->appoint_date)->format('Y-m-d');
// $appoint_date_format = strtotime($appoint_date);
// $appoint_day = date('l', $appoint_date_format);
$date = $request->appoint_date;
$doctor_id = $request->doctor_id;
$appoint_for = $request->appoint_for;
return $this->sendResponse(['doctor_id' => $doctor_id,'appoint_date' => $date,'appoint_for' => $appoint_for], 'Data retrieved successfully.');
}
And the images,
For Local server works fine
Localhost response
For Server works fine with Key Parameters
Server response with key parameters
For Server return null values with raw JSON parameters
response with raw JSON parameters
Since it is a GET route, it expects parameters in URL.
For POST request, you need to define a separate Route and function which will take parameters in body.
EDIT: you can't send parameters in body when using GET HTTP method.
Refer:
https://laravel.com/docs/7.x/routing#required-parameters
I've checked the Q&A about this and can't find anything, so thought i'd ask.
I have a very simple Laravel controller returning all results from a table as below via the 'Name model'. There is then also a further call to my controller, via the model to count the rows and all works and sends to the result set fine...
// All results from my 'Name' model:
$results = $this->name->getAllResults(); // All works fine.
// I then use my controller again, count the rows via the model and add them to $results:
$results['count'] = $this->countNames(); // Again fine
BUT, when i try to add a string to the $results array before i pass it off to th view, as in:
$results['test'] = 'Test'; // This fails in the view
$results['test'] = 124; // But this passes in the view and renders.
It only seems to allow me to add an INT to my result set array. as $results['test'] = 124 also fails.
I then finally, have this sending to my view via:
return view('names', compact('results')); // And that works fine.
Can anyone see what it is I am missing and why integer added to $results works and not a string?. Many thanks in advance.
You are updating collection data. The following line will give collection of models.
$results = $this->name->getAllResults(); // This may give collection of the model
And below, you are updating the collection object.
$results['count'] = $this->countNames();
You can do the following to safely send data to view, without modifying any.
$results = $this->name->getAllResults();
$count = $this->countNames();
$test = 'Test';
$test2 = 124;
return view('names', compact('results','count','test','test2'));
I have the following issue when I try to update the laravel table I send my data via ajax everything is good. But the update returns an error.
the following function receives the data and updates the table.
public function saveCalendar(Request $request) {
$calendar = $request->calendar;
$apartment_id = $request->apartment_id;
apartments::where('Apartment_ID', $apartment_id)->update(array('calendar' => $calendar));
$confirmation = 'Календара е запазен успешно !';
return $confirmation;
}
I also tried this query:
apartments::where('Apartment_ID', $apartment_id)->update('calendar' => $calendar);
Any idea what am I doing wrong.
Error is because update function accepts array. Correct syntax is
apartments::where('Apartment_ID', $apartment_id)->update(['calendar' => $calendar]);
Also the correct syntax for fetching resquest inputs are
$calendar = $request->input('calendar');
$apartment_id = $request->input('apartment_id');
Problem
I'm using Symfony2 with AJAX to insert a new record in my 'locations' table.
I can easily pass the data I want from the form to my Controller that handles that
AJAX request. I checked with Firebug and all the GET values are as I wanted.
Just to be clear the controller is not in the location controller but in the debug controller, don't know if that matters. (just for sandbox-testing atm).
Because of the relationships in the database the location entity has a district column which needs a district-entity object.
Problem is now this code is returning the succes response fine if I leave out the $em->flush(). If I add the $em->flush() to perform the insert, I get an 500 Internal error in my console of my browser.
Here's the code for my controller:
$request = $this->container->get('request');
if($request->isXmlHttpRequest()){
$street = $request->query->get('street');
$zip = $request->query->get('zip');
$lat = $request->query->get('lat');
$name = $request->query->get('name');
$number = $request->query->get('number');
// insert into object
$entity = new Location();
$entity->setStreet($street);
$entity->setZip($zip);
$entity->setLat($lat);
$entity->setName($name);
$entity->setNumber($number);
$em = $this->getDoctrine()->getManager();
$district = new District();
// get the district hard coded for testing
$district = $em->getRepository("SnowFrontBundle:District")->find(7);
$entity->setDistrict($district);
//$entity->setDistrict(null);
$em->persist($entity);
$em->flush();
//prepare the response, e.g.
$response = array("code" => 100, "success" => true);
return new Response(json_encode($response));
}
Question
What could be wrong with the $em->flush() part? How can I see what the real error is? (instead of 500 internal error). As well should I be using a createFormBuilder?