How to fix error in codeigniter4 and date_format? - codeigniter

I am making this query in codeigniter4 and I am stuck
public function consultasHoy(){
$data = $this->consultas->select('pacientes.*,consultas_hc.*')
->from('consultas_hc')
->join('pacientes','consultas_hc.identificacion = pacientes.identificacion')
->where(date_format('consultas_rc.reConsulta','%Y-%m-%d'),date_format(now(),'%Y-%m-%d'))
->orderBy('consultas_hc.reConsulta', 'ASC')
->get();
return json_encode($data);
}
and it returns this error
date_format() expects parameter 1 to be DateTimeInterface, string
given

If you are getting date straightforwardly from the database to your controller then you can print date format in view like below:
$YourDate = $row->start_date;
$newDate = date("d-m-Y", strtotime($YourDate));

Try replacing your where clause with this line of code:
$where="DATE_FORMAT('consultas_rc.reConsulta','%Y-%m-01')= DATE_FORMAT(CURDATE() ,'%Y-%m-01')";
->where($where);

this was the solution used, thanks for the help
public function consultasHoy(){
helper('date');
$hoy=(new \DateTime("now"))->format("Y-m-d");
$builder = $this->consultas->select('*');
$builder->join('pacientes', 'pacientes.identificacion=consultas_hc.identificacion');
$builder->where("DATE(reConsulta)", $hoy);
$query = $builder->get();
return json_encode($query->getResult());
}

Related

Laravel 5 date Format value

I would like to know why my code is not working.
I have my model Organigramme i want to calculate with a function the age between two dates:
public function getAncienneteAttribute()
{
$now = Carbon::now();
$date_dentree = Carbon::createFromFormat('d/m/Y', $this->date_dentree);
return $now->diffInYears($date_dentree);
}
But I'm faced this error: Data missing
Someone can help me please.
Your $this->date_dentree format is not matching the format you passed to Carbon::createFromFormat, it is probably the default format so try the code below:
public function getAncienneteAttribute()
{
$now = Carbon::now();
return $now->diffInYears(new Carbon($this->date_dentree));
}
Try this
$date_dentree = \Carbon\Carbon::createFromFormat('Y-m-d', $this->date_dentree);
$diffYears = \Carbon\Carbon::now()->diffInYears($date_dentree );
//\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $request->date)->format('Y-m-d')
$dbDate = \Carbon\Carbon::parse('2020-05-10');
$diffYears = \Carbon\Carbon::now()->diffInYears($dbDate);

Object could not be converted to string (laravel)

My question is, why does the following not work?
$questions = Question::where($queryatypes);
$questions->get();
I'm getting the following Error:
Object of class Illuminate\Database\Eloquent\Builder could not be
converted to string
Please check the answer
$questions = Question::where($queryatypes);
$questions = $questions->get();
I think you are trying this on your controller and return this Objects directly
use Illuminate\Http\Response;
public function controllerFunction(){
$queryatypes = .....
$questions = Question::where($queryatypes);
$questions->get();
return response($questions);
}

Codeigniter: Extract certain value from an array under controller

Good day!
This is pretty basic to some.
I have this code on my controller:
$data["sales_info"] = $this->The_model->get_sales_info(SELECT * FROM sales WHERE sales_id = 123);
Then get_sales_info() method under The_model stores the specific sales information into an array.
Now, I'm doing some debugging, so from the controller, I want to extract from the $data["sales_info"] and echo only the value of 'sales_date'.
How am I going to do this?
Thank you!
You can try this solution for your problem:
Change the model file to :
The_model.php
class The_model extends MY_Model {
function get_sales_info($sales_id){
$this->db->select("*.*");
if(!empty($sales_id)){
$this->db->where('sales_id', $sales_id);
$query = $this->db->get('sales');
return $query->row();
} else {
$query = $this->db->get('sales');
return $query->result();
}
}
}
and change the controller file to:
My_controller.php
$sales_id = 123;
$sales_info = $this->The_model->get_sales_info($sales_id);
echo $sales_info['sales_date'];exit; //how to get value from object
Thanks
This code may help you
$sales_info = $this->The_model->get_sales_info(SELECT * FROM sales WHERE sales_id = 123);
$data['sales_data'] = $sales_info['sales_date'];
$query = $this->db->get_where('sales', array('sales_id' => 123));
It should be like this, you cannot write the sql in to a method as best practice.

Input array loop on controller laravel 5

I have inputs array and i need to make a foreach but laravel $request->all() only return last one:
url:
http://localhost:8000/api/ofertas?filter_pais=1&filter_pais=2&filter_pais=3
controller:
public function filtroOfertas(Request $request){
return $request->all();
}
result:
{"filter_pais":"3"}
result should return 1, 2 and 3 and i need to make a foreach in filter_pais.
Any solution? Thanks
Use [] at the key of query string.
http://localhost:8000/api/ofertas?filter_pais[]=1&filter_pais[]=2&filter_pais[]=3
It will be parsed as array.
Repeated parameters make no sense and should be avoided without exception.
But looking at other solutions, there are several:
routes.php
Route::get('/api/ofertas/{r}', 'Controller#index');
Controller:
public function index($r)
{
$query = explode('&', $r);
$params = array();
foreach($query as $param)
{
list($name, $value) = explode('=', $param);
$params[urldecode($name)][] = urldecode($value);
}
// $params contains all parameters
}
Given that the URL has no question marks:
http://localhost:8000/api/ofertas/filter_pais=1&filter_pais=2&filter_pais=3

Laravel Eloquent belongsTo: "Trying to get property of non-object" when using without echo/die/var_dump

I've just started with Laravel and Eloquent.
In my "Receipt"-model, I try to get information about a relationship.
As long as I'm using var_dump, I get my information.
As soon as I'm just returning the value to use it in a blade view, I get my "Trying to get property of non-object".
<?php
class Receipt extends Eloquent {
public function businesspartner() {
return $this->belongsTo('Businesspartner', 'f_businesspartner_id');
}
public function brand() {
return $this->belongsTo('Brand', 'f_brand_id');
}
public function invoice() {
return $this->belongsTo('Invoice', 'f_invoice_id');
}
public function name() {
$name = '';
$bp = $this->businesspartner()->first();
$name = $bp->salutation; // line 21
$name .= ' ';
$name .= $bp->lastname_company;
return $name;
}
}
The error occurs in line 21.
When I now put a
var_dump($name);die();
before returning, it prints me my name.
What's going wrong here? :(
Regards,
Timo
SOLUTION
As #Jarek Tkaczyk wrote, I was calling the name() method in a loop.
When doing my debug outputs, I got data from my first row.
When getting the error, the code was already processing the second row, where the foreign key was null.
Wrapping the problematic part in if(count($this->businesspartner)) helped.
For more information about that, Jarek linked that post: https://stackoverflow.com/a/23911985/784588

Resources