Home > Back-end >  Get meta information from the database with Eloquent
Get meta information from the database with Eloquent

Time:10-27

I'm probably missing something simple here but how do I get the primary key for a table using Laravel/Eloquent? When I execute the following I'm getting an Incorrect Syntax error while it works when executing directly on the database.

$primarykey = DB::table('table')->whereRaw('SHOW KEYS FROM table WHERE Key_name = "PRIMARY"')->get();

CodePudding user response:

I tried it with following code and it worked.

$primarykey = DB::select('SHOW KEYS FROM users WHERE Key_name = "PRIMARY"');

I had used select of DB without get method because it returns array like this

[
     {#3558
        "Table": "users",
        "Non_unique": 0,
        "Key_name": "PRIMARY",
        "Seq_in_index": 1,
        "Column_name": "id",
        "Collation": "A",
        "Cardinality": 1,
        "Sub_part": null,
        "Packed": null,
        "Null": "",
        "Index_type": "BTREE",
        "Comment": "",
        "Index_comment": "",
     },
   ]

So please try and let me know if you find any issue in it. Thanks

  • Related