0

I am new to laravel and building my first project using it. I want to query two tables (Cases and Detail) using orWhere() function. Please tell me the changes I need to make in my existing code.

Controller.php

$key = trim($request->get('q'));
$cases = Cases::query()
    ->where('cnic', "$key")
    ->orWhere('eventDate', "$key")            
    ->get();        

return view('adminHome',['user' => $cases ]);

The field eventDate exists in the Detail Model.

adminhome.blade

<form action="/search" method="GET" role="search">
      <input type="number" name="q" placeholder="Search CNIC...">
      <input type="date" name="q" placeholder="Search CNIC..." ">
      <input type="Submit" name="Search" value="Search">
      {{ csrf_field() }}
</form>
Waail
  • 5
  • 5
  • what do you mean by _I want to query two tables using orWhere()_, in your query you have one table (Model) only – STA Dec 30 '20 at 08:58
  • That is my question, how can I use with('detail) or something like that to achieve this? Sorry if my question was not clear enought. – Waail Dec 30 '20 at 09:00
  • Does this answer your question? [Query relationship Eloquent](https://stackoverflow.com/questions/20036269/query-relationship-eloquent) – Peppermintology Dec 30 '20 at 09:08
  • 1
    First, drop the `query()` in your Eloquent statement. No need for that. If you have a `details` table, you have to set the relationships right in your Models. In this case, it looks like a one-to-one relationship. Get that right and you simply add `with('details)` in your eloquent ORM statement. – Dimitri Mostrey Dec 30 '20 at 09:08

0 Answers0