月度归档:2019年08月

Laravel Union操作后排序错误的问题 Fix the wrong order after using union method in Laravel query

解决方法,在两个子查询中分别加上limit即可。可以使用较大的数确保所有记录返回。

The solution is to use the limit method on the two subqueries. You can use a larger number to ensure that all records are returned.

$orders1 = Order::where('id', '=', $user->id)
    ->where('status', '!=', 0)
    ->orderBy('id', 'asc')
    ->limit(1e8);

$orders2 = Order::where('id', '=', $user->id)
    ->where('status', '=', 0)
    ->orderBy('created_at', 'desc')
    ->limit(1e8)
    ->unionAll($orders1)
    ->get();

 

Laravel 5.5在浏览器中预览Notification渲染 Previewing Laravel Notification In Browser

对于Mail,我们可以通过下面的代码在浏览器中预览Mailables

For Mail, we can preview Mailables in the browser with the following code.

Route::get('/mailable', function () {
    $invoice = App\Invoice::find(1);
    return new App\Mail\InvoicePaid($invoice);
});

然而Notification类中toMail返回的MailMessage实例,Laravel文档中并没有给出直接方法在浏览器中预览,我们可以采取下面的方法。

However, the MailMessage instance returned by toMail method in the Notification class does not give a direct method to preview in the browser in the Laravel document. We can use the following code.

Route::get('/mailable', function () {
    $message = (new \App\Notifications\FooNotification()->toMail("bar");
    return app()->make(\Illuminate\Mail\Markdown::class)->render($message->markdown, $message->data());
});

在Laravel5.8及以上,MailMessage实现了Renderable接口(PR in Github),所以可以直接return MailMessage实例作为响应了。

In Laravel 5.8 and above, MailMessage implements the Renderable interface (PR in Github), so you can directly return the MailMessage instance as a response.