Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

krishna9720's avatar

How to Delete a Record with Custom id in laravel

MY DB 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
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Here subscription_id is the table primary key.

I am using join here to fetch data from 3 tables.

public function index()
    {
       $fetch = DB::table('subscription_manager')
            ->join('comic_info', 'comic_info.id', '=', 'subscription_manager.comic_id')
            ->join('users', 'users.id', '=', 'subscription_manager.user_id')
            ->get();
        for ($i = 0, $c = count($fetch); $i < $c; ++$i) {
        $fetch[$i] = (array) $fetch[$i];
        }
        return view('comic/subcriptions', compact('fetch'));

    }

and here the destroy method

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

And delete action is look like inside view.

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

But when i going to delete the record it says

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

That means its by default taking id only. I dont want delete from id. i want that from subscription_id.

How can i Fixed that issue thanks.

0 likes
3 replies
frezno's avatar
frezno
Best Answer
Level 36

@KRISHNA9720 In your SubscriptionManager Model class (i assume you created that Model) you have to add:

 protected $primaryKey = "subscription_id";

That overrides the default 'id' as primary key.

and very likely you have to override the default table naming convention as well:

protected $table = 'subscription_manager';
1 like
Snapey's avatar

please mark the answer as correct

Please or to participate in this conversation.