Nayeem Hyder Riddhi
Nayeem Hyder Riddhi

Reputation: 611

Why Query Exception Updating Data in Laravel?

I am trying to updating data in laravel.view and delete part works fine but updating part is not working. it is giving query exception

QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'add_teachers.id' in 'where clause' (SQL: select * from add_teachers where add_teachers.id = 2 limit 1)

my web.php file is

<?php


Route::get('/', function () {
    return view('login');
});

Route::get('login',  function(){
    return view('login');
});

Route::post('login','logincontro@checklogin');
Route::get('logout','logincontro@logout');

Route::group(['middleware' => 'checklogin'], function () {

Route::get('dashboard','dashboard@home');

Route::get('add-student', function() {

       return view('add-student');
});

Route::get('add-teacher', function() {

       return view('add-teacher');
});

Route::get('view-student', function() {

       return view('view-student');
});

Route::get('view-teacher', function() {

       return view('view-teacher');
});



Route::get('view-student', 'addStudents@addstudentHome');

Route::post('/insert', 'addStudents@addstudentData');

Route::get('/update/{id}', 'addStudents@updatestudentData');

Route::post('/edit/{id}', 'addStudents@editstudentData');

Route::get('/delete/{id}', 'addStudents@deletestudentData');



Route::get('view-teacher', 'addTeachers@addteacherHome');

Route::post('/insert', 'addTeachers@addteacherData');

Route::get('/update1/{teacher_id}', 'addTeachers@updateteacherData');

Route::post('/edit/{id}', 'addTeachers@editteacherData');

Route::get('/delete/{id}', 'addTeachers@deleteteacherData');

    });

view-teacher.blade.php code is

 @if(count($addData)> 0)

  @foreach($addData -> all() as $singleData)
                <tr>

                <td>{{ $singleData -> teacher_id }}</td>

            <td> <img style="width:65px;" src="{{ URL::to('/') }}/images/{{ $singleData -> teacher_image }}" alt="image" /> </td>
            <td>{{ $singleData -> teacher_name }}</td>
            <td>{{ $singleData -> position }}</td>
            <td>{{ $singleData -> contact_num }}</td>
            <td>{{ $singleData -> teacher_email }}</td>
            <td> 
                <a href='{{ url("/update1/{$singleData -> teacher_id}")}}' class="label label-success" style="font-size:14px "> Update</a> 
                <a href='{{ url("/delete/{$singleData -> teacher_id}")}}' class="label label-danger" onClick="return doconfirm();" style="font-size:14px "> Delete</a>   
             </td>

             </tr>


        @endforeach
        @endif

Controller addTeachers.php is

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\addTeacher;

class addTeachers extends Controller
{

    public function addteacherHome()
    {
            $addData = addTeacher::all();

      return view('view-teacher', ['addData' => $addData]);

    }


    public function addteacherData(Request $request)

    {
            $this -> validate($request, 
             [ 
               'teacher_name' => 'required',
               'position'  => 'required',
               'contact_num' => 'required',
               'teacher_email'  => 'required',
               'teacher_image'  => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:5048'   ]);


         $image = $request -> file('teacher_image'); 

         $destinationPath = 'images/';

         $filename = $image -> getClientOriginalName(); 

         $image -> move($destinationPath, $filename);


         /*Storage::put('upload/image/'.$filename, file_get_contents($request -> file('image')-> getRealPath()));*/

         $datas = new addTeacher;

         $datas -> teacher_name = $request -> input('teacher_name');

         $datas -> position  = $request -> input('position');

         $datas -> contact_num = $request -> input('contact_num');

         $datas -> teacher_email = $request -> input('teacher_email');

         $datas -> teacher_image = $filename;

         $datas -> save();

         return redirect('/add-teacher')-> with('info', 'Data saved Successfully');




    }


    public function updateteacherData(Request $request, $teacher_id)

    {

        $updateData = addTeacher::find($teacher_id);

      return view('update-teacher', ['updateData' => $updateData] );


    }


    public function editteacherData(Request $request, $id)  {



$this -> validate($request, 
           [ 
               'teacher_name' => 'required',
               'position'  => 'required',
               'contact_num' => 'required',
               'teacher_email'  => 'required',
               'teacher_image'  => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:5048'   ]);


         $image = $request -> file('teacher_image'); 

         $destinationPath = 'images/';

         $filename = $image -> getClientOriginalName(); 

         $image -> move($destinationPath, $filename);

       $data = array(

         'teacher_name' => $request -> input('teacher_name'),

         'position' => $request -> input('position'),

         'contact_num' => $request -> input('contact_num'),

         'teacher_email' => $request -> input('teacher_email'),

         'teacher_image' => $filename
    );

    addTeacher::where('teacher_id', $id) -> update($data);

     return redirect('view-teacher') -> with('info', 'Data Updated successfully');

    }


    public function deleteteacherData($id)

    {

         addTeacher::where('teacher_id', $id) -> delete();

    return redirect('view-teacher') -> with('info', 'Data Deleted successfully');


    }
}

Table structure>>> enter image description here

Upvotes: 1

Views: 162

Answers (2)

Prateik Darji
Prateik Darji

Reputation: 2317

basically, Laravel takes id as the primary key when you are using find it looks the given id into the id field of the table so what you need to do is just open teacher model and write following line

class Teacher extends Model
{
    protected $primaryKey = 'teacher_id';

    // all the other methods
}

Or you can use where instead of using find method just like below

$data = Teacher::where('teacher_id',$teacher_id)->get();

This will also work same as find method without updating the model.

Upvotes: 1

Deepak Kumar T P
Deepak Kumar T P

Reputation: 1076

Laravel assumes id to be primary key. So you have used teacher_id as primary key.

Laravel Model::find() uses id as default primary key.

You can override the primary key to be your custom key like this.

In your addTeacher Model add this line.

protected $primaryKey='teacher_id';

Or else you can also do like this in your updateteacherData() method

replace $updateData = addTeacher::find($teacher_id);

with

$updateData = addTeacher::where('teacher_id',$teacher_id)->get()->first();

Upvotes: 4

Related Questions