Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mhmmdva's avatar

how make filtering year in option

I want to create a filter by adjusting the year when selected using the options then the data for the selected year will appear in its entirety. and I have made a query like this in SQL, but my problem is that it is still a date '2016-11-15' I only want to display the year '2016'

SELECT * FROM research_group_statuses WHERE research_group_id = 1 AND status = 'basic' AND YEAR(publish) = 2016;

view.blade

 <div id="content-2{{ $group->id }}" class="research-collapse collapse" data-bs-parent="#faqlist">
     <div class="research-body">
         <div class="input-group my-2">
             <select class="select-option rounded" id="publish" name="publish" aria-label="filtering periode">
                 <option selected>Filtering Year </option>
                 @foreach ($filterOne->where('status', 'Basic') as $year)
                     <option value="1">{{ $year->publish }}</option>
                 @endforeach
             </select>
         </div>
         <table id="example" class="table table-striped" style="width:100%">
             <thead>
                 <tr>
                     <th>No</th>
                     <th style="width:400px">Judul Penelitian</th>
                     <th>Terbit</th>
                     <th style="width:200px">Status</th>
                 </tr>
             </thead>
             <tbody>
                 @forelse ($statusesOne->where('status', 'Basic') as $basic)
                     <tr>
                         <td>{{ $basic->research_group_id }}</td>
                         <td>{{ $basic->research_title }}</td>
                         <td>{{ $basic->publish }}</td>
                         <td>{{ $basic->status }}</td>
                     </tr>
                 @empty
                     <tr>
                         <td colspan="5" class="text-center">empty data!</td>
                     </tr>
                 @endforelse
             </tbody>
         </table>
     </div>
 </div>

Sorry, don't insult me ​​like the others if you don't look like a programmer in general, because I can only make them one by one or manually

client controller

class ClientController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $informations = Information::all();
        $logo = Logo::first();        
        
        $groupsOne = ResearchGroup::where('id', 1)->get();
        $statusesOne = ResearchGroupStatus::query()->whereIn('status', ['Basic', 'Applied', 'Development', 'Commercial'])->where('research_group_id', 1)->get();

        $groupsTwo = ResearchGroup::where('id', 2)->get();
        $statusesTwo = ResearchGroupStatus::query()->whereIn('status', ['Basic', 'Applied', 'Development', 'Commercial'])->where('research_group_id', 2)->get();

        $groupsThree = ResearchGroup::where('id', 3)->get();
        $statusesThree = ResearchGroupStatus::query()->whereIn('status', ['Basic', 'Applied', 'Development', 'Commercial'])->where('research_group_id', 3)->get();

        $groupsFour = ResearchGroup::where('id', 4)->get();
        $statusesFour = ResearchGroupStatus::query()->whereIn('status', ['Basic', 'Applied', 'Development', 'Commercial'])->where('research_group_id', 4)->get();

        $groupsFive = ResearchGroup::where('id', 5)->get();
        $statusesFive = ResearchGroupStatus::query()->whereIn('status', ['Basic', 'Applied', 'Development', 'Commercial'])->where('research_group_id', 5)->get();

        $groupsSix = ResearchGroup::where('id', 6)->get();
        $statusesSix = ResearchGroupStatus::query()->whereIn('status', ['Basic', 'Applied', 'Development', 'Commercial'])->where('research_group_id', 6)->get();

        $groupsSeven = ResearchGroup::where('id', 7)->get();
        $statusesSeven = ResearchGroupStatus::query()->whereIn('status', ['Basic', 'Applied', 'Development', 'Commercial'])->where('research_group_id', 7)->get();

        // filter year
        $filterOne = ResearchGroupStatus::query()->season(2016)->whereIn('status', ['Basic', 'Applied', 'Development', 'Commercial'])->where('research_group_id', 1)->distinct()->get();
        
        // method-1

        // $statusSevenBasics = ResearchGroupStatus::query()->basic()->where('research_group_id', 7)->get();
        // $statusSevenApplied = ResearchGroupStatus::query()->applied()->where('research_group_id', 7)->get();
        // $statusSevenDevelopment = ResearchGroupStatus::query()->development()->where('research_group_id', 7)->get();
        // $statusSevenCommercial = ResearchGroupStatus::query()->commercial()->where('research_group_id', 7)->get();

        return view('client.landing-page', compact(
            'informations', 'logo', 
                'groupsOne', 'statusesOne',
                'groupsTwo', 'statusesTwo',
                'groupsThree', 'statusesThree',
                'groupsFour', 'statusesFour',
                'groupsFive', 'statusesFive',
                'groupsSix', 'statusesSix',
                'groupsSeven', 'statusesSeven', 'filterOne'
        ));
    }
}

resarchGroupStatus model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ResearchGroupStatus extends Model
{
    use HasFactory;

    protected $guarded = ['id']; // do not add a foreign key, because it will ask for the default value

    protected $table = 'research_group_statuses';

    public function researchGroup()
    {
        return $this->belongsTo(ResearchGroup::class);
    }
    
    public function getRouteKeyName()
    {
        return 'slug';
    }

    public function status()
    {
        if ($this->status == 'Basic') {
            return 'basic research';
        } else if ($this->status == 'Applied') {
            return 'applied research';
        } else if ($this->status == 'Development') {
            return 'development research';
        } else if ($this->status == 'Commercial') {
            return 'commercial research';
        } else {
            return null;
        }
    }

    public function scopeBasic(Builder $query): Builder
    {
        return $query->where('status', 'Basic');
    }

    public function scopeDevelopment(Builder $query): Builder
    {
        return $query->where('status', 'Development');
    }

    public function scopeApplied(Builder $query): Builder
    {
        return $query->where('status', 'Applied');
    }

    public function scopeCommercial(Builder $query): Builder
    {
        return $query->where('status', 'Commercial');
    }

    public function scopeSeason($queries)
    {
        foreach ($queries as $query) {
            $query->orWhereYear('publish')->format('Y');
        }

        return $query;
    }

    public function scopeResearch(Builder $query): Builder {

        return $query->whereIn('status', ['Basic', 'Applied', 'Development', 'Commercial']);
        
    }
}
0 likes
2 replies
tykus's avatar

The value of each option should be the year; using your example (only so I don't refactor beyond your understanding) - I don't think it will produce distinct years!

<select class="select-option rounded" id="publish" name="publish" aria-label="filtering periode">
    <option selected>Filtering Year </option>
    @foreach ($filterOne->where('status', 'Basic') as $year)
        <option value="{{ Carbon\Carbon::parse($year->publish)->year }}">
            {{ Carbon\Carbon::parse($year->publish)->year }}
        </option>
    @endforeach
</select>
mhmmdva's avatar

@tykus data cannot be displayed in option

client controller

$filterOne = ResearchGroupStatus::query()
            ->whereIn('status', ['Basic', 'Applied', 'Development', 'Commercial'])
            ->where('research_group_id', 1)
            ->orderBy('publish')
            ->distinct()->get([ResearchGroupStatus::raw('YEAR(publish) as year', 'status')]);

Please or to participate in this conversation.