skip to Main Content

we have a books table, and When I click on Harry Potter Part 1, it will show me the other parts below, such as Harry Potter Part 2 or Part 3, I try like query or group by

I tried these but to no result

 public function book(Request $request)
    {
        $book = books::find($request->id);
            return [
                'book' => $book,
                'more_books' => books::
                    ->whereNotIn('id', [$book->id])
                    ->where('title', 'like', '%' . $book->title. '%')
                    ->get()
            ];
    }

output :

{
    "book": {
        "id": 40,
        "price": 20,
        "title": "Harry Potter Part 1"
    },
    "more_books": []
}

2

Answers


  1. Chosen as BEST ANSWER
     public function book(Request $request)
        {
            $book = books::find($request->id);
            $title = substr($book->title, 0, -3);
    
                return [
                    'book' => $book,
                    'more_books' => books::
                        ->whereNotIn('id', [$book->id])
                        ->where('title', 'like', '%' . $title. '%')
                        ->get()
                ];
        }
        ```
    

  2. if this is mysql you would use the MATCH() function to return a relevance, and play around with the results you get from this (and add fullext index to the table). I think in laravel you’ll need to add a whereraw in the query builder. Look up the Match function. Other dbs will have the same or similar.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search