php - 拉拉维尔 6.2 : I get the same Id from different data in index() method

标签 php mysql database eloquent laravel-6.2

我使用 Laravel,我需要从我的数据库中获取一些保留。

我使用查询生成器:

 public function getReservation($date)
    {
        $reservations = DB::table('reservations')
                        ->select('*')
                        ->join('agendas', 'reservations.agenda_id', '=', 'agendas.id')
                        ->whereDate('arrivee', $date)
                        ->get();

        return response($reservations);
    }

一切都很好,只是我对不同的资源有相同的 ID,但我不知道为什么?

回复:

[
    {
        "id": 1,
        "created_at": "2020-02-07 12:30:28",
        "updated_at": "2020-02-07 12:30:28",
        "arrivee": "2020-02-12 13:00:00",
        "nbre_client": 4,
        "num_table": null,
        "remarque": null,
        "venu": 0,
        "formule": 0,
        "agenda_id": 1,
        "user_id": 1,
        "nom_client": "Stark",
        "prenom_client": null,
        "num_phone_client": "0658180058",
        "email_client": null,
        "confirmResa": null,
        "nameAg": "London"
    },
    {
        "id": 1,
        "created_at": "2020-02-07 12:30:28",
        "updated_at": "2020-02-07 12:30:28",
        "arrivee": "2020-02-12 13:30:00",
        "nbre_client": 2,
        "num_table": null,
        "remarque": null,
        "venu": 0,
        "formule": 0,
        "agenda_id": 1,
        "user_id": 1,
        "nom_client": "Banner",
        "prenom_client": null,
        "num_phone_client": "0658180058",
        "email_client": null,
        "confirmResa": null,
        "nameAg": "London"
    }
]

我的数据库中的ID不同(分别是2和3)

有人有什么想法吗?

最佳答案

解决方案

重复的 ID 很可能是您的议程中的 ID,因为它们属于同一议程。

您可以将选择更改为:

->select('reservations.*')

Eloquent 之道

您也可以使用 Eloquent 完成您所拥有的任务。

首先,您需要确保您的 Reservation 模型与您的 Agenda 模型具有 BelongsTo 关系。

public function agenda()
{
    return $this->belongsTo(Agenda::class);
}

然后你可以这样查询:

Reservation::whereHas('agenda', function ($query) use ($date) {
    $query->whereDate('arrivee', $date);
})->get();

关于php - 拉拉维尔 6.2 : I get the same Id from different data in index() method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60171527/

相关文章:

php - 需要更快的 PHP/MySQL 搜索算法来进行高度复杂的计算

PHP 正则表达式不起作用 - 解析 {{tags}}

php - 限制对特定页面的访问

php - 查询增量列不起作用

javascript - Parse/AngularJS - 使用用户 ID 输入数据

php - 我不断在 hMailServer 和 XAMPP 的 PHPWebAdmin 页面中获取 "Fatal Error: Class COM not found"

java - 从 HashMap 中的数据库获取我的数据

mysql - #1062 - key '111' 的重复条目 'PRIMARY'

c# - 局域网中的公共(public)数据库

PHP 将行插入 MySQL - 哪种方法更合适?