Delete Records with Custom primary key in Laravel 5.5 | Laravel eloquent 5.5

Delete Records with Custom primary key in Laravel 5.5 | Laravel eloquent 5.5

Delete Records with Custom primary key in laravel 5.5 | Laravel eloquent 5.5

In Many cases if you are using Table joins between many tables so here you may need to use custom primary key id in the tables like for example if you are going to make a table like subscription_manager then maybe you can set its primary id as subscription_id.

By default in laravel use id for store the data, update the data, and destroy the data. if you made a controller by default these function will generate inside your controller file.

So Here we go, I am going to tell you in this quick tutorial. How we can use custom id for deletion or destroy the data.

Suppose your table structure is like this:-

CREATE TABLE `subscription_manager` (
`subscription_id` int(10) NOT NULL,
`user_id` int(10) NOT NULL,
`comic_id` int(10) NOT NULL,
`admin_id` int(10) NOT NULL,
`date_of_subscription` date NOT NULL,
`mini` tinyint(1) DEFAULT '0',
`one_shots` tinyint(1) NOT NULL DEFAULT '0',
`updated_at` date NOT NULL,
`created_at` date NOT NULL

Here you can assume subscription_id is a primary key.

And For example here is your destroy method.

   public function destroy($id)
      $subscribe = Subscription::find($id);
      if ($subscribe != null) 
      return redirect('subscriptions')->with('message','Subscription Successfully Deleted');
        return redirect('subscriptions')->with('message','Unable to Delete');

And Inside your View Delete operation look like this with custom primary key subscription_id.

  <form action="{{ action('SubscriptionController@destroy', $comic['subscription_id'])}}" method="post">
            <input name="_method" type="hidden" value="DELETE">
            <button class="btn btn-danger" type="submit">Delete</button>

But when if you are going to delete the record it gives following error.

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

That means by default laravel takes id as a primary key for performing the operation.

Solutions for the Problem:-

You can use custom id using laravel eloquent or Directly define primary key inside your Model file.

#1. Declare primary key as you custom table primary key inside the model file


for example like this.

protected $primarykey ="subscription_id";

protected $table="subscription_manager";


#2. for the second possible solution that’s working perfectly that you can use by Laravel eloquent

So in your destroy method you need to change

$subscribe = Subscription::find($id);


$subscribe = Subscription::where(‘subscription_id’, ‘=’, $id)->delete();



So now with this quick tutorial, you know how to override your default primary key and destroy data, perform the operation in laravel with your own custom id.

Krishna Gupta

Krishna Ballabh Gupta is a Gold Medalist Full Stack Developer & Algorithm Designer and Founder of TechnologyShouters. He generally Blogs about Python, JavaScript, jQuery, AngularJS, Database, PHP, AJAX and something that interesting.

Related Posts

Create Account

Log In Your Account

1Subscribe to Our Live Updates.