Home > database >  Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select `house_
Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select `house_

Time:07-21

i have houses and their details. am able to display all the houses in a data table.I have a column in the datable whereby a user clicks and they are redirected to another page with all the house details. in the detail page for each house i want to show images of the house in a jquery datatable.am finding the images using using a relationship and also i have added a house_id in the images table. I have been able to integrate the data table using jquery very well but the problem is in the method at the controller where I want to get the images and display them in the details page.if the house doesn't have images the data table will just show the regular message for datatable "no data available".this is my method in the controller

   public function get_extraimages(Request $request,$id)
{
    $alternateimages=Alternaterental_image::select('id','image','status','house_id')->where(['house_id',$id])->get();    //am guesin this is there the bug comes from

    if($request->ajax()){
        $allimages = DataTables::of ($alternateimages)
        ->addColumn ('status',function($row){
            return 
            '<input  type="checkbox" checked data-toggle="toggle" data-id="'.$row->id.'" data-on="Active" data-off="Not Active" data-onstyle="success" data-offstyle="danger">';
        })
        ->addColumn ('delete',function($row){
            return 
                 '<a href="#" id="deletextraimage"  data-id="'.$row->id.'"><i ></i></a>';
        })
        ->rawColumns(['status','delete'])
        ->make(true);

        return $allimages;
    }

    return view('Admin.Rental_houses.edit_addimages',compact('alternateimages'));
}

here is my jquery code

   $(document).ready(function(){
        var roomimgid=$('#images_id').val();
        var url = '{{ route("get_extraimages", ":id") }}';
               url = url.replace(':id', roomimgid);

        var alternateimagestable = $('#rentalhseimages').DataTable({
        
        processing:true,
        serverside:true,
        reponsive:true,

        ajax:
        {
            url:url,
            type: 'get',
            dataType: 'json',
            data:{
                'id':roomimgid
            },
        },
        columns: [
          { data: 'id' },
          { data: 'image',
              render: function ( data, type, full, meta, row) {
                  return "<img src=\"/imagesforthewebsite/alternateimages/small/"   data   "\" height=\"80px\" height=\"80px\"/>"
              }
          }, 
          { data: 'status',name:'status',orderable:true,searchable:true },
          { data: 'delete',name:'delete',orderable:false,searchable:false },
        ],

        "fnDrawCallback": function( row ) {
          $('.rentalhousestatus')
          .prop( 'checked', row.status !== 1 )
          .bootstrapToggle();
        }
      });
    });

how can i find the images for that specific house and display them in the datatable.

CodePudding user response:

Do it this way:

$alternateimages=Alternaterental_image::where('house_id',$id)->select('id','image','status','house_id')->get(); 

Problem

The problem is, you are using closure in where(), which makes eloquent think that you want to check two columns (i.e., keys of array 0 and 1 in your case), named 0 and 1 equal to 'house_id' and [whatever_is_inside_$id] respectively. But in your case you wanted to check house_id == $id hence, closure not needed.

The Correct way to use closure

If you intend to check multiple columns you should proceed this way:

where(['house_id' => $id, 'user_id' => $uId])

in the above example the eloquent will check for columns named; house_id and user_id respectively.

  • Related