【问题标题】:How to use composite primary key in laravel 8?如何在 laravel 8 中使用复合主键?
【发布时间】:2021-02-28 12:31:34
【问题描述】:

我没有使用迁移文件,因为我在数据库中有预先存在的表,我想知道如何强制 laravel 使用我的复合主键。如果有任何方法可以覆盖。 复合主键为(Tel + number_str) 这是我在模型中的功能:

public function saveData(){
  $response = new ResponseModel(); 
                //Primary key(Tel + number_str )
                $reponse->Tel = '0123456789';//---> the first key
                $reponse->number_str = '1'; //---> the second key
    
                $reponse->view = '0';  
                $reponse->channal = '0';
                $reponse->save();
}

当我执行时出现此错误:

Yajra\Pdo\Oci8\Exceptions\Oci8Exception
Error Code : 904 
Error Message : ORA-00904: "ID": invalid identifier Position : 98 Statement : insert into "RESPONSE" ("TEL", "number_str", "view", "channal ") values (:p0, :p1, :p2, :p3) returning "ID" into :p4 Bindings : [0123456789,1,0,0,0]

【问题讨论】:

  • 这是什么,laravel-5laravel-8? "insert into "REPONSE"" 是错字还是REPONSE 正确?
  • 这是响应,对不起。
  • 是否有任何方法可以在 laravel 8 中覆盖以允许我使用复合键?

标签: laravel web laravel-5 eloquent laravel-8


【解决方案1】:

Laravel 附带 Eloquent ORM,而 Eloquent 不支持复合键。如果您打算重写 Eloquent 方法以支持它们,则需要重写 很多 核心方法以保持一切正常运行。

现在,Eloquent 并不是与数据库交互的唯一选择。您仍然可以使用查询生成器来执行您的操作。但是当然没有 ORM 的好处。

DB::table('response')->insert([
    'tel' => '0123456789', 
    'number_str' => 1,
    'view' => 0,
    'channel' => 0,
]);

DB::table('response')
  ->where('tel', '0123456789')
  ->where('number_str', 1)
  ->get();

【讨论】:

  • 是的,谢谢,我认为使用查询生成器是个好主意。比重写很多核心方法要好。
【解决方案2】:

Laravel 8,接受以下格式

DB::table('response')
  ->where('tel' ,'=', '0123456789')
  ->where('number_str', '=' ,1)
  ->get();

【讨论】:

    猜你喜欢
    • 2021-07-03
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    相关资源
    最近更新 更多