How to filt data by time using Laravel?

筅森魡賤 提交于 2020-05-17 07:00:06

问题


I have this query, I would like to obtain only the data created on the current month

public function index(Request $request)
{

    if($request->ajax())
    {
        $data = DB::table('tbl_lista_contactabilidad as a')
    ->select('a.id','a.rif','a.razon_social','a.postventaatcs_id','fecha_contacto','a.contactado','a.persona_contacto','a.correo_contacto','a.celular_contacto','a.numero_contacto','a.comentarios','a.auditado')
    ->leftjoin('tbl_equipo_postventaatcs as h','h.id','=','a.postventaatc_id')
    ->leftjoin('users as l','l.id','=','h.asesor_id')
    ->leftjoin('tbl_lista_respuestas as b','b.id','=','a.auditado')
    ->leftjoin('tbl_lista_respuestasc as c','c.id','=','a.contactado')
    ->where('l.idop', auth()->user()->idop)
    ->where('a.estatus','=',1)
    ->select(array('a.id','l.name as idop_asesor','l.apellido as ape_asesor','l.idop','a.rif','a.razon_social','a.estatus','fecha_contacto','a.contactado', 'a.persona_contacto','a.correo_contacto','a.celular_contacto','a.numero_contacto','a.comentarios','a.auditado','b.respuesta','c.respuestac'));

    }

    return view('contactabilidadasesor');
}

回答1:


I am not sure if your query is correct, probably one of the two select() should be removed.

Although, in order to fetch the data created on the current month add in your query the following

->whereYear('a.created_at', Carbon::now()->year)
->whereMonth('a.created_at', Carbon::now()->month)


来源:https://stackoverflow.com/questions/60642036/how-to-filt-data-by-time-using-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!