1

I have been using my app with this query for over a year and it works great. Today I added a relationship to dso and I am getting this error: "Column not found: 1054 Unknown column 'dso_name' in 'where clause". This relationship works as expected for create and read. It just won't work in this search query. Any ideas?

 public function search_customer(Request $request){
    $search = $request->input('search_customer');
    $this->validate(request(),[
        'search_customer'=>'required|min:2'
    ]);
    $customers = customer::with('dso')
        ->where('customer_name', 'LIKE', '%'.$search.'%')
        ->orWhere('billing_street', 'LIKE', '%'.$search.'%')
        ->orWhere('billing_city', 'LIKE', '%'.$search.'%')
        ->orWhere('service_street', 'LIKE', '%'.$search.'%')
        ->orWhere('service_city', 'LIKE', '%'.$search.'%')
        ->orWhere('main_phone', 'LIKE', '%'.$search.'%')
        ->orWhere('email', 'LIKE', '%'.$search.'%')
        ->orWhere('cc_email', 'LIKE', '%'.$search.'%')
        ->orWhere('rNumber', 'LIKE', '%'.$search.'%')
        ->orWhere('registrant', 'LIKE', '%'.$search.'%')
        ->orWhere('RSO', 'LIKE', '%'.$search.'%')
        ->orWhere('service_state', 'LIKE', '%'.$search.'%')
        ->orWhere('billing_state', 'LIKE', '%'.$search.'%')
        ->orWhere('dso_name', 'LIKE', '%'.$search.'%')
       ->get();
    return response()->json($customers);
}
T.A.
  • 638
  • 1
  • 6
  • 15
  • Is this answers to your question https://stackoverflow.com/questions/29989908/laravel-where-on-relationship-object – Vahe Shak Aug 08 '20 at 23:51
  • @Vahe Shak - That solution seems to be a little different than what I am needing. That solutions returns only if there is a relationship. In my situation there is not always a dso for a customer. If there is no dso for a customer then I still need it to return something. – T.A. Aug 09 '20 at 00:10
  • Preppend the table name to the column. Ie `orWhere('customers.dso_name')` – porloscerros Ψ Aug 09 '20 at 01:24
  • @porloscerros. I tried that. I got this error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'dsos.dso_name' in where clause'. l also tried using the model rather than the table name: dos.dso_name. I got the same error. – T.A. Aug 09 '20 at 01:45
  • If the column is not in the customer model table, you can use `orWhereHas` like i think is what Vahe Shak suggested. https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-existence – porloscerros Ψ Aug 09 '20 at 01:52
  • Ie `orWhereHas('dso', function ($query) use ($search) { $query->where('dso_name', 'like', '%'.$search.'%'); })` – porloscerros Ψ Aug 09 '20 at 01:57
  • Try to use joins. I think it will be easier. https://laravel.com/docs/5.1/queries#joins – Vahe Shak Aug 10 '20 at 02:59

0 Answers0