如何使用Laravel按时间过滤数据? [英] How to filt data by time using Laravel?

查看:137
本文介绍了如何使用Laravel按时间过滤数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个查询,我只想获取当月创建的数据

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');
}

推荐答案

我不确定您的查询是否正确,可能应该删除两个select()之一.

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)

这篇关于如何使用Laravel按时间过滤数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆