简体   繁体   中英

Select Year only from database php (Laravel)

I have a question, how to select year only from database in laravel? I have in my database:

2015-07-02
2015-07-04
2015-08-06

I just want to select year ('2015')

I want to build in Laravel, my code is:

   $visit_time = DB::table('tr_visit')->join ('tm_child','tr_visit.Child_ID','=','tm_child.Child_ID')
       ->where('tr_visit.Child_ID', 'LIKE', '%' . $childName . '%')
       ->groupBy(DB::raw('YEAR(tr_visit.Visit_Date)'))
       ->groupBy(DB::raw('MONTH(tr_visit.Visit_Date)'))
       ->select(DB::raw('YEAR(Visit_Date)'))->distinct()->get();

Please help me, thank you

you can use

->selectRaw('YEAR(Visit_Date)')

And using Eloquent

$visit_time = Tr_visit::join('tm_child', 'tr_visit.Child_ID', '=', 'tm_child.Child_ID')
->where('tr_visit.Child_ID', 'like', '%' . $childName . '%')
->groupBy(['date', 'month'])
->selectRaw('YEAR(tr_visit.Visit_Date) as date, MONTH(tr_visit.Visit_Date) as month')
->distinct()->get();

编辑

->select(DB::raw('YEAR(tr_visit.Visit_Date)')) 

gracias por su aporte realice el proceso para una lista de años ejecutados.

este es el codigo:

$anio_list = estandar312::where('id_empresa',$id)
        ->groupBy(['date'])
        ->selectRaw('YEAR(created_at) as date')
        ->distinct()->get();

luego en la vista realice lo siguiente recorriendo el key:

<select class="form-control" name="anioselect" id="anioselect">
                                <option selected value="">Seleccione Año.. 
                              </option>
                                @foreach ($anio_list as $key => $items)
                                    <option> {{ $items->date}}
                               </option>
                                @endforeach
                            </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM