pkouadio's avatar

dataTables erreor Column not found: 1054 Unknown column

Hi, I have a problem with the dataTables search. I get this error every time I use the dataTables search field:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sections.nom_region' in 'where clause' (SQL: select count(*) as aggregate from sections left join regions on sections.id_region = regions.id left join dioceses on sections.id_diocese = dioceses.id where (LOWER(sections.nom_section) LIKE %k% or LOWER(sections.nom_region) LIKE %k% or LOWER(sections.nom_diocese) LIKE %k%))

Here is my request to display the data:

$instances = DB::table('sections') ->leftJoin('regions', 'sections.id_region', '=', 'regions.id') ->leftJoin('dioceses', 'sections.id_diocese', '=', 'dioceses.id') ->select('sections.nom_section', 'dioceses.nom_diocese', 'regions.nom_region') ->addSelect(DB::raw('COALESCE(regions.nom_region, "") as nom_region')) ->paginate(30);

And here the one for data collection:

$instances = DB::table('sections') ->leftJoin('regions', 'sections.id_region', '=', 'regions.id') ->leftJoin('dioceses', 'sections.id_diocese', '=', 'dioceses.id') ->select('sections.nom_section', 'dioceses.nom_diocese', 'regions.nom_region') ->addSelect(DB::raw('COALESCE(regions.nom_region, "") as nom_region'));

Thank you for your help.

0 likes
11 replies
LaryAI's avatar
Level 58

The error message indicates that the column sections.nom_region is not found in the database. Double-check that the column exists and is spelled correctly.

If the column exists, it's possible that the search query is not properly formatted. Try modifying the search query to explicitly reference the table name for the nom_region column:

$instances = DB::table('sections')
    ->leftJoin('regions', 'sections.id_region', '=', 'regions.id')
    ->leftJoin('dioceses', 'sections.id_diocese', '=', 'dioceses.id')
    ->select('sections.nom_section', 'dioceses.nom_diocese', 'regions.nom_region')
    ->addSelect(DB::raw('COALESCE(regions.nom_region, "") as nom_region'))
    ->where(function($query) use ($search) {
        $query->where('sections.nom_section', 'LIKE', "%$search%")
              ->orWhere('regions.nom_region', 'LIKE', "%$search%")
              ->orWhere('dioceses.nom_diocese', 'LIKE', "%$search%");
    })
    ->paginate(30);

This modified query adds a where clause to search for the nom_region column explicitly. The $search variable should contain the search term entered by the user.

Note that this solution assumes that the nom_region column exists in the regions table. If it exists in a different table, modify the query accordingly.

tykus's avatar

I don't see the constraints in the Query Builder; where do they come from?

where (
  LOWER(sections.nom_section) LIKE %k% 
  or LOWER(sections.nom_region) LIKE %k% 
  or LOWER(sections.nom_diocese) LIKE %k%)
)
pkouadio's avatar

@tykus I didn't know that you had to write the search function explicitly. I correct and come back.

tykus's avatar

@pkouadio no, you misunderstand me... I was saying that I cannot see in the PHP code where you add the constraints to the Query Builder - i.e. the is no where in the query:

$instances = DB::table('sections')
    ->leftJoin('regions', 'sections.id_region', '=', 'regions.id')
    ->leftJoin('dioceses', 'sections.id_diocese', '=', 'dioceses.id') 
    ->select('sections.nom_section', 'dioceses.nom_diocese', 'regions.nom_region')
    ->addSelect(DB::raw('COALESCE(regions.nom_region, "") as nom_region')) 
    ->paginate(30);
pkouadio's avatar

@tykus After modification I have the following function but I still have the same error.

public  function  data(Request  $request)
{
$search = $request->input('search.value');
$instances = DB::table('sections')
->leftJoin('regions', 'sections.id_region', '=', 'regions.id')
->leftJoin('dioceses', 'sections.id_diocese', '=', 'dioceses.id')
->select('sections.nom_section', 'dioceses.nom_diocese', 'regions.nom_region')
->addSelect(DB::raw('COALESCE(regions.nom_region, "") as nom_region'))
->where(function ($query) use ($search) {
$query->where('sections.nom_section', 'LIKE', "%$search%")
->orWhere('regions.nom_region', 'LIKE', "%$search%")
->orWhere('dioceses.nom_diocese', 'LIKE', "%$search%");
});
return  Datatables::of($instances)->make(true);
}
pkouadio's avatar

@tykus I see this query is automatically generated by dataTables to perform a search in the returned fields to build the table. The problem is that I have a join between three tables but dataTables does not seem to know the difference between them.

tykus's avatar

@pkouadio right; this is why I asked to see how the datatable is set up in the view. Do you qualify the nom_region as regions.nom_region?

pkouadio's avatar

@tykus there is

$(document).ready(function() { $('#instances-table').DataTable({ "processing": true, "serverSide": true, "ajax": "{{ route('instances.data') }}", "columns": [ {"data": "nom_section"}, {"data": "nom_region"}, {"data": "nom_diocese"}]}); });

tykus's avatar
tykus
Best Answer
Level 104

@pkouadio try to fully qualify the column, like this:

{ data: 'nom_region', name: 'regions.nom_region' },
1 like

Please or to participate in this conversation.