简体   繁体   中英

QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customers.id' in 'where clause'

I want to Edit my data in laravel system, which has a customer_id as an increment key. But the error says,

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

Previous exceptions

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customers.id' in 'where clause' (42S22)

I don't understand why.

I've tried to add protected primarykey is customer_id in my model. but the result is the same like before.

This is my Controller

    {
        $customer = \App\Customer_Model::find($customer_id);
        $customer->delete();
        return redirect('/customers')->with('sukses', 'Users has been deleted!');
    }

This is my model


namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer_Model extends Model
{
    protected $table = 'customers';
    protected $primarykey = 'customer_id';
    protected $fillable = ['customer_id', 'customer_code', 'customer_name', 'email', 'phone', 'contact_person', 'address', 'user_id', 'created_by', 'updated_by', 'void'];
}```

This is my view
```<tbody>
                      @foreach($data_customer as $cust)
                        <tr>
                        <td>{{$cust->customer_code}}</td>
                        <td>{{$cust->customer_name}}</td>
                        <td>{{$cust->email}}</td>
                        <td>{{$cust->phone}}</td>
                        <td>{{$cust->contact_person}}</td>
                        <td>{{$cust->address}}</td>
                        <td><a href="/customers/{{$cust->customer_id}}/edit" class="btn btn-warning"><span class="icon-settings icons icon"></span></a><a href="/customers/{{$cust->customer_id}}/delete" class="btn btn-danger"><span class="icon-trash icons icon"></span></a></td>
                        </tr>
                        @endforeach
                      </tbody>```

Change $primarykey to $primaryKey . The variable is case sensitive and Laravel is looking for $primaryKey (with a capital 'K')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM