Question

I am trying to filter the "service" of a "technical" by year and month, for this, I get an ID to find the "technical". All this is done with a nested query . My problem is I dont know how filter by year and moth same time with laravel My code:

Controller

    public function doFilter(){
    $servicios = Tecnico::find(Input::get('id'))
                ->servicios //services
                ->where('timestamp', Input::get('ano').'-'.Input::get('mes'));// ano=year, mes=moth
    return View::make('detalletecnico', array('servicios' => $servicios, 'id' => Input::get('id')));            

    }

TechnicalModel

class Tecnico extends Eloquent{
protected $table = 'Tecnico';
protected $primaryKey = 'idTecnico';
protected $hidden = array('Contrasena');
protected $fillable = array(
                            'idTecnico',
                            'Nombre',
                            'Apellido',
                            'Telefono',
                            'Contrasena',
                            'Foto',
                            );
public function servicios(){
    return $this->belongsToMany('Servicio', 'Servicio_Tecnico', 'Tecnico_idTecnico', 'Servicio_idServicio');
}

}

ServiceModel

class Servicio extends Eloquent{
protected $table = 'Servicio';
protected $primaryKey = 'idServicio';
protected $fillable = array(
                            'Direccion',
                            'RutaPDF',
                            'Completado',
                            'timestamp'
                            );
public function materialUsado(){
    return $this->hasMany('Material_Usado', 'idMaterial_Usado');
}

public function tecnicos(){
    return $this->belongsToMany('Tecnico', 'Servicio_Tecnico', 'Servicio_idServicio', 'Tecnico_idTecnico');
}
}

Route

Route::get('tecnico/detalle/filter', array('uses' => 'TecnicoController@doFilter'));

Blade

            {{Form::open(array('url' => 'tecnico/detalle/filter'))}}
        {{Form::select('mes', array('Enero', 
                                        'Febrero', 
                                        'Marzo', 
                                        'Abril', 
                                        'Mayo', 
                                        'Junio',
                                        'Julio',
                                        'Agosto',
                                        'Septiembre',
                                        'Octubre',
                                        'Noviembre',
                                        'Diciembre'), array('class' => 'input-xlarge'))}}

        {{Form::select('ano', array(date('Y'),
                                    '2013', 
                                    '2012' 
                                    ), array('class' => 'input-xlarge'))}}
        {{ Form::hidden('id', $id) }}                       
        {{ Form::submit('Filtrar', array('class' => 'btn btn-primary')) }}  
        {{Form::close()}}
Was it helpful?

Solution

You might try to use raw wheres and the YEAR and MONTH functions from MySql:

$servicios = Tecnico::find(Input::get('id'))
            ->servicios()
            ->whereRaw('YEAR(timestamp) = ?', array(Input::get('ano')))
            ->whereRaw('MONTH(timestamp) = ?', array(Input::get('mes')))
            ->get();

OTHER TIPS

If I get you right, then this is what you want:

$servicios = Tecnico::find(Input::get('id'))
                ->servicios()
                ->where('timestamp', 'like', Input::get('ano').'-'.Input::get('mes'))
                ->get();

And of course months array in the select must have numbers as keys like so:

array('01' => 'Enero, '02' => 'Febrero' ...)

Filter using a date range:

public function doFilter()
{
    $inicio = \Carbon\Carbon::createFromDate(Input::get('ano'), Input::get('mes'), 1);

    $fim = $start->copy()->lastOfMonth()->hour(23)->minute(59)->second(59);

    $servicios = Tecnico::find(Input::get('id'))
                ->servicios()
                ->where('timestamp', '>=' $inicio)
                ->where('timestamp', '<=' $fim)
                ->get();

   return View::make('detalletecnico', array('servicios' => $servicios, 'id' => Input::get('id')));            
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top