php - 如何在 laravel 中删除具有自定义 id 的记录,默认情况下仅使用 ID

标签 php mysql laravel-5 laravel-5.3

我的数据库结构是这样的

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;

这里的subscription_id是表主键。

我在这里使用 join 从 3 个表中获取数据。

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'));

    }

这里是销毁方法

  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');
       }
    }

删除操作看起来像内部 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>

但是当我要删除记录时它说

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)

这意味着默认情况下它只获取 id。我不想从 id 中删除。我想要来自 subscription_id 的信息。

我该如何解决这个问题,谢谢。

最佳答案

如果您在文档中阅读了相关内容,您会发现有一个带有Where()的firstOrFail()方法

https://laravel.com/docs/5.0/eloquent

所以在你的销毁方法中你需要改变

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

  $subscribe = Subscription::where('subscription_id', '=', $id)->firstOrFail();

关于php - 如何在 laravel 中删除具有自定义 id 的记录,默认情况下仅使用 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47049476/

相关文章:

php - 如果插入查询执行完成则更改表名称

php - Mysql查询没有返回正确的结果

c# - 在 c#、mysql 中使用 linq 查找最新插入日期的数据

mysql - 以一对多关系对 'many' 表进行 Elequent 查询

php - 用于 SEO 的 URL 重写和 MVC 框架

php - Laravel 5.4 Auth::User() 关系不起作用

php - 错误 0x1408F10B : "SSL3_GET_RECORD:wrong version number" with PayPal SDK

php - 有没有一个 PHP 框架被认为是 Ruby 的 Ruby On Rails?

php - Laravel 模型中的主键不起作用

php - Laravel 在查询 JSONB 列(转换为数组)时是否调用 json_decode 一次或多次?