Laravel:如何在Form中使用orderBy?

时间:2019-03-29 03:55:08

标签: laravel

我是laravel的新手,正在努力填充表单元素。

在数据库中,我想选择具有最新ID或最新created_by值的行,但orderBy方法似乎根本不起作用。

查看我的代码:

@php($deliveryNote = \App\DeliveryNote::where('order_id', $order->id)->orderBy('id', 'desc')->first())

因此,查询中是否存在orderBy无关紧要,它仍会打印ID最低的第一条记录。我也在其他线程中尝试过-> latest(),但这只会显示错误。

5 个答案:

答案 0 :(得分:1)

  • 切勿在刀片(视图)中使用雄辩的查询。完全错误,您应该遵循mvc结构。
  • 您应该从控制器发送此数据。

答案 1 :(得分:1)

切勿在刀片文件中使用雄辩的查询。这是完全错误的。

您应遵循mvc的结构并尝试Latest()函数。

$deliveryNote = \App\DeliveryNote::where('order_id', $order->id)->latest()->first();

答案 2 :(得分:0)

如果要检索表的最后行,请尝试此

$last_row = Model::orderBy('id', 'desc')->first();

这将使您可以按DESCENDING顺序排列数据库表,然后-> first()方法将获取您的第一个表(根据顺序相反)

答案 3 :(得分:0)

static Startup()
{
    PublicClientId = "Web";

    OAuthOptions = new OAuthAuthorizationServerOptions
    {
        TokenEndpointPath = new PathString("/Token"),
        AuthorizeEndpointPath = new PathString("/Account/Authorize"),
        Provider = new ApplicationOAuthProvider(PublicClientId),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
        AllowInsecureHttp = true
    };
}

public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }

public static string PublicClientId { get; private set; }

public void ConfigureAuth(IAppBuilder app)
{
    app.bunchofotherregistrationsfordefaultapp();

    // Enable the application to use bearer tokens to authenticate users
    app.UseOAuthBearerTokens(OAuthOptions);
}

答案 4 :(得分:-1)

@php
//codes here
@endphp

此laravel指令仅适用于纯PHP,因此我认为您不能在其中执行Eloquent关系。

尝试以下方法:

{{ $deliveryNote = \App\DeliveryNote::where('order_id', $order->id)->orderBy('id', 'desc')->first() }}