在Laravel 6.5.1上更改ID列名称

时间:2019-11-24 19:14:10

标签: php laravel laravel-6

我在Laravel 6.5.1中有一个模型用户。

class User extends Authenticatable
{
    use Notifiable;

    public $table = 'usuario';
    public $primaryKey = 'cif_usu';
    public $incrementing = false;
    public $timestamp = false;

   //...
}

但是,当我尝试选择一个用户时,出现以下错误:

  

SQLSTATE [42703]:未定义的列:7错误:列“ id”不存在   第1行:从“ usuario”中选择*,其中“ id” = $ 1上限1 ^(SQL:select   *来自“ usuario”,其中“ id” = 24个限制1)

如何重命名id列?

编辑:

我更改了:

public $primaryKey = 'cif_usu';

至:

protected $primaryKey = 'cif_usu';

结果相同

3 个答案:

答案 0 :(得分:0)

$primaryKey属性的可见性应为protected。我认为没有流量的变化,您实际上并不会覆盖基本Model类中的主键。

如果不是这种情况,那么查看触发该查询的代码可能会很有用

答案 1 :(得分:0)

  

Eloquent还将假定每个表都有一个名为$primaryKey的主键列。您可以定义一个受保护的/** * The primary key associated with the table. * * @var string */ protected $primaryKey = 'key'; 属性来覆盖此约定:

public static void FloydWarshall(Graph g) {
        int V = g.getvCount();

        // to store the calculated distances
        float dist[][] = new float[V][V];

        // initialize with adjacency matrix weight values
        for (int i = 0; i < V; i++) {
            for (int j = 0; j < V; j++) {
                for(int k = 0; k<=g.edgeList.size(); k++){
                    if(g.edgeList.get(i).head.equals(i) && g.edgeList.get(j).tail.equals(j)){
                        int label = Integer.parseInt(g.edgeList.get(k).label);
                dist[i][j] = label;
                }}
            }
        }

        // loop through all vertices one by one
        for (int k = 0; k < V; k++) {
            // pick all as source
            for (int i = 0; i < V; i++) {
                // pick all as destination
                for (int j = 0; j < V; j++) {
                    // If k is on the shortest path from i to j
                    if (dist[i][k] + dist[k][j] < dist[i][j]) {
                        // update the value of dist[i][j]
                        dist[i][j] = dist[i][k] + dist[k][j];
                    }
                }
            }
        }

        // shortest path matrix
        for (int z = 0; z < V; z++) {
            for (int j = 0; j < V; j++) {
                // if value is infinity
                if (dist[z][j] == Float.MAX_VALUE)
                    System.out.print("INF ");
                else
                    System.out.print(dist[z][j] + "   ");
            }
            System.out.println();
        }

详细了解Laravel - Primary Keys

答案 2 :(得分:0)

将主键和表置于受保护的位置。

protected $table = 'usuario';

protected $primaryKey = 'cif_usu';

public $incrementing = false; //only if your primary key is an assignable field,not auto incremented

您可以将模型用作

DB::table('usuario')->where('cif_usu',$cif_usu)->first();

OR

DB::table('usuario')->find($cif_usu);