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

Lokedi's avatar

Api get don`t work correctly

Hi, I made an api that will take my pages from the database according to its name, but any year parameter I would give it, it will take according to its name and the year will ignore it Do you know what this problem is about?

public function viewPageByYear(Request $request, $slug, $year) 
    {
        $data = DB::table('pages')->where('meta_title', '=', $slug)
                ->orWhere('meta_title_en', '=', $slug)->where('year', '=', $year)->get();

        $media = DB::table('files')->where('model_id', '=', $data->id)
                ->where('model_type', '=', 'App\Pages')->first();

        return response()->json([$data, $media]);
    }
//api.php
Route::get('/pages/{slug}/{year}',          [AjaxController::class, 'viewPageByYear']);

So this is the function, the year is always ignored and is taken only according to the meta_title api/pages/something/2021 - show api/pages/something/2022 - show ( but this don`t exist in database)

0 likes
2 replies
bugsysha's avatar

You probably want to wrap ->where('meta_title', '=', $slug)->orWhere('meta_title_en', '=', $slug) together. Because orWhere is affecting the rest of the query. Which version of Laravel are you using.

$data = DB::table('pages')
  ->where(function ($query) use ($slug) {
    $query->where('meta_title', $slug)->orWhere('meta_title_en', $slug);
  })
  ->where('year', $year)
  ->get();

Please double-check for any typos.

1 like
bugsysha's avatar

@danylok if that worked consider marking discussion as solved by picking it as the best answer. Thanks.

Please or to participate in this conversation.