I'm trying to create an object in Laravel and when I set the date I get an error.
Code
<?php
$result = pg_query($this->conn, "SELECT * FROM cliente");
if (!$result) {
$this->message('Error to query cliente');
exit;
}
while ($row = pg_fetch_row($result)) {
if ($row[7] == null) {
$cliente->fecha_nacimiento = null;
} else {
$fechaString = (string)$row[7];
$fecha = DateTime::createFromFormat('Y-m-d', trim($fechaString));
$cliente->fecha_nacimiento = $fecha;
}
}
Error
[ErrorException]
DateTime::createFromFormat() expects parameter 2 to be string, object
given
If I use Carbon:
$fecha = Carbon::createFromFormat('Y-m-d', trim($fechaString));
Getting:
[InvalidArgumentException] The separation symbol
could not be found Unexpected data found. Trailing data
Thanks in advance!
Related
I am trying to update the setting but unfortunately, I am facing an error on how to fix this error? please help me thanks.
please see error https://flareapp.io/share/yPaYdGP4
Controller
public function settingupdate(Request $request)
{
$input = $request->except('_token','logo_image');
if ($request->logo_image) {
$setting = Settings::where('key','logo_image')->first();
$input['logo_image'] = $request->logo_image;
$input['logo_image'] = Storage::disk('cms')->putFile('', $request->file('logo_image'));
$request->file('logo_image');
}
foreach($input as $key => $value) {
$setting = Settings::where('key',$key)->first();
$setting->value = $value;
$setting->save();
}
return back()->with('success', 'Setting Successfully updated')- >with('path',$setting);
}
You are getting error because of below line. This line returning no record and you are setting value on it $setting->value = $value
$setting = Settings::where('key',$key)->first();
To resolve this you can check whether data exist or not
$setting = Settings::where('key',$key)->first();
if ($setting !== null) { // add this
$setting->value = $value;
$setting->save();
}
severity: Warning
Message: Creating default object from empty value
Filename: controllers/person.php
line Number: 133
function update($id)
{
$responce = new StdClass;
$this->_set_rules();
$person = $this->Person_model->get_by_id($id)->row();
error in this line i try to figure it ot
$this->form_data->id = $id;
$this->form_data->name = $person->name;
$this->form_data->gender = strtoupper($person->gender);
$this->form_data->dob = date('d-m-Y',strtotime($person->dob));
$data['title'] = 'Update person';
$data['message'] = '';
$data['action'] = site_url('person/updatePerson');
$data['link_back'] = anchor('person/index/','Back to list of persons',array('class'=>'back'));
// load view
$this->load->view('personEdit', $data);
}
Lowercase "s" in stdClass:
$response = new stdClass;
NOT:
$responce = new StdClass;
Find out more on PHP objects here:
http://php.net/manual/de/language.types.object.php
You need to create the data object as shown below.
$responce = new stdClass();
Hope this will solve your error.
public function get_priority($priority_id = "") {
if ($priority_id == "") {
$qry = $this->db->get("tm_priority");
} else {
$qry = $this->db->query("SELECT * FROM tm_priority WHERE nt_id = {$priority_id}");
echo $this->db->last_query();
}
print_r($qry->result());
}
In my above model function I echo the last_query() for debugging purpose and it's giving me the following result
SELECT * FROM tm_priority WHERE nt_id = 1
When I run this query directly into my mysqlyog, it's working fine.
Then what else could be the reason of the error below
Fatal error: Call to a member function result() on a non-object in /home/staging/erp/application/models/tasks/task_model.php on line 341
You should try to check for number of returned rows before outputting it :
if($qry->num_rows() > 0){
print_r($qry->result());
}
Is there a way to show the changed values after saving within the Joomla save method?
For example, when I edit a "maxuser" field and save it, I´d like to show the old and the new value.
I tried this by comparing "getVar" and "$post", but both values are the same.
function save()
{
...
$maxuser1 = JRequest::getVar('maxuser');
$maxuser2 = $post['maxuser'];
...
if($maxuser1 != $maxuser2) {
$msg = "Not the same ...";
}
...
}
It's better to override JTable, not the Model. Heres sample code:
public function store($updateNulls = false) {
$oldTable = JTable::getInstance(TABLE_NAME, INSTANCE_NAME);
$messages = array();
if ($oldTable->load($this->id)) {
// Now you can compare any values where $oldTable->param is old, and $this->param is new
// For example
if ($oldTable->title != $this->title) {
$messages[] = "Title has changed";
}
}
$result = parent::store($updateNulls);
if ((count($messages) > 0) && ($result === true)){
$message = implode("\n", $messages);
return $message;
} else {
return $result;
}
}
This will return message string if there are any, true if there are no messages and save succeeded and false if saving failed. So all you have to do is check returned value in model and set right redirect message.
In the controller you can use the postSaveHook which gives you access to the validated values.
I would like to print out a failed SQL statement in CodeIgniter. On my first try I used a try/catch block below that prints db->last_query(). However, as you can see from the log the SQL statement is not printed. What am I doing wrong?
public function get($limit = NULL, $offset = NULL, $sort = NULL, $search = NULL)
{
try {
if ($limit !== NULL) $limit = (int) $limit;
if ($offset !== NULL) $offset = (int) $offset;
if (is_array($sort)) {
foreach ($sort as $field => $order) {
$this->db->order_by($field, $order);
}
}
if (is_array($search)) {
foreach ($search as $field => $match) {
$this->db->where($field, $match);
}
}
$this->db->select($this->select_fields);
$query = $this->db->get($this->table_name, $limit, $offset);
// Set the results
$this->last_query = $this->db->last_query();
$this->num_rows = $query->num_rows();
$this->result_array = $query->result_array();
$this->db_result = $query;
$this->error_number = $this->db->_error_number();
$this->error_message = $this->db->_error_message();
} catch(Exception $e) {
log_message('error',$e->getMessage());
log_message('error',$this->db->last_query());
$this->error_number = 500;
}
}
The error I get is:
DEBUG - 2013-03-02 15:16:50 --> DB Transaction Failure
ERROR - 2013-03-02 15:16:50 --> Query error: Unknown column 'template' in 'where clause'
The $this->db->last_query() line returns the last query that was run.
But your SQL log file show us that you have a error in your SQL query. So, your query was not run. The last_query() line can not return anything.
Query error: Unknown column 'template' in 'where clause'
Do you have a template column in your table?
-Simple solution.
-if you are using firfox. than istall firbug and enable it,and in firebug console you can view any thing related your request,like
- Your http request
- post/get data
- responses etc