Laravel根据created_at datetime字段获取最新记录

时间:2016-05-23 04:23:46

标签: php laravel datetime laravel-5.1

在我的电子商务网络应用程序中,我正在尝试实施国际航运功能。这种关系就像:

一个国际航运区域拥有多个国家/地区属于一个shippig区域。

一个国际运输区域已经应用了许多运费和一个运费属于一个运输区。

现在我想要尝试的是获取按降序创建的所有运费。现在,我可以正确地获取它,没有任何问题。现在来了扭曲。如果表中有一个运费率具有相同的运费代码,则根据用户在前端选择的国家/地区,获取最新记录并忽略之前的记录。

我尝试过的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        /*
        Commented out because doing it this way creates 
        2 PlaceHolder variables named placeholder, everything else is as needed
        */
        //PlaceHolder placeholder = new PlaceHolder();

        //Populating a DataTable from database.
        DataTable dt = this.GetData();
        //Building an HTML string.
        StringBuilder html = new StringBuilder();
        //Table start.
        html.Append("<table border = '1'>");
        //Building the Header row.
        html.Append("<tr>");
        foreach (DataColumn column in dt.Columns)
        {
            html.Append("<th>");
            html.Append(column.ColumnName);
            html.Append("</th>");
        }
        html.Append("</tr>");
        //Building the Data rows.
        foreach (DataRow row in dt.Rows)
        {
            html.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                html.Append("<td>");
                html.Append(row[column.ColumnName]);
                html.Append("</td>");
            }
            html.Append("</tr>");
        }
        //Table end.
        html.Append("</table>");
        string strText = html.ToString();
        ////Append the HTML string to Placeholder.
        placeholder.Controls.Add(new Literal { Text = html.ToString() });
    }
}
private DataTable GetData()
{
    // Modified your method, since I don't have access to your db, so I created one manually
    // Here we create a DataTable with four columns.
    DataTable table = new DataTable();
    table.Columns.Add("Dosage", typeof(int));
    table.Columns.Add("Drug", typeof(string));
    table.Columns.Add("Patient", typeof(string));
    table.Columns.Add("Date", typeof(DateTime));

    // Here we add five DataRows.
    table.Rows.Add(25, "Indocin", "David", DateTime.Now);
    table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
    table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
    table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
    table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
    return table;
}

当用户选择我不想要的运送国家/地区时,上述代码会从该运费区域的费率表中提取所有记录。如果使用相同的代码找到多条记录,我只想获取最新的一条。

收集方法对我不起作用:

$shippingCountryId = session()->get('new_shipping_country'); $shippingCountryAmount = session()->get('new_shipping_amount'); if ($shippingCountryId !== null) { $shippingAll = ShippingCountry::find($shippingCountryId) ->zone->rates() ->latest()->get(); } else { $shippingAll = ShippingCountry::where('name', 'India')->first() ->zone->rates() ->latest()->get(); } unique()contains()search()

我知道我很确定我必须犯一些愚蠢的错误,但我无法找出错误和位置。

请帮助我解决这个问题。我试图在2天后解决这个问题,但是还没有成功。

非常感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:0)

latest方法仅供订购。如果您只想获取一个 - 最新记录,则必须将get(您将在此处获得有序集合)更改为first

$shippingCountryId = session()->get('new_shipping_country');
$shippingCountryAmount = session()->get('new_shipping_amount');

if ($shippingCountryId !== null) {
    $latestRate = ShippingCountry::find($shippingCountryId)
                                    ->zone->rates()
                                    ->latest()->first();
} else {
    $latestRate = ShippingCountry::where('name', 'India')->first()
                                    ->zone->rates()
                                    ->latest()->first();
}
相关问题