【问题标题】:Laravel get title from table on basis of pivot tableLaravel 根据数据透视表从表中获取标题
【发布时间】:2020-06-24 20:03:00
【问题描述】:

我的数据库名称中有三个表,sms_content、sms_content_categories 和 categories。

表 sms_content_categories 包含“categories_id”和“sms_content_id”。而类别包含“id”、“title”。我想根据“categories_id”从类别标题中获取“标题”。

基本上我在数据表中使用它并且我得到了错误。 SQLSTAT[HY00]: 一般错误: 2031 (Sql: select 'title from categories where 'id'=?)

这是我的代码:

$categoryID = SmsContentCategories::where('sms_content_id', $smsContentID)->pluck('categories_id')->toArray();
     $categoryName = Category::where('id', $categoryID)->pluck('title')->toArray(); 

我想从类别中获取标题。

【问题讨论】:

  • dd($categoryID) 以确保它包含应有的内容
  • 我已经更新了我的问题

标签: php laravel


【解决方案1】:

我想根据“categories_id”从类别标题中获取“标题”。

未经测试,但这应该可以工作:


$categoryIds = SmsContentCategories::whereIn('sms_content_id', $smsContentID)->pluck('categories_id');

$categoryTitles = Category::whereIn('id', $categoryId)->pluck('title');

【讨论】:

  • 谢谢您的回复。现在我收到 dataTables ajax 错误。比如“DataTables 警告:table id=DataTables_Table_0 - Ajax 错误。有关此错误的更多信息,请参阅datatables.net/tn/7
  • @FaisalKhan 如果您在修补程序会话中尝试第一行,您会得到什么响应? php artisan tinker
猜你喜欢
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多