2017-07-18 32 views
-2

我有一個簡單的想法,我只想知道實現它的最佳方法是什麼?laravel中的計時消息5

我想讓我的網站的一部分,以在用戶登錄後歡迎用戶,並根據時間顯示歡迎注意到用戶。

例如。

早上好,xxx。

下午好,xxxx。

那樣。

PS:xxxx將成爲用戶名。

任何想法我可以做到這一點?

+0

的([取決於一天的時間運行代碼]可能的複製https://stackoverflow.com/questions/8652502/run-code-depending-on當天的時間) – linktoahref

+0

@linktoahref確定它的工作原理,但我如何得到涉及{{Auth :: user() - > name}}? – djhru

+0

在我的例子中,我說過早安xxxx這些'xxxx'應該是用戶名。 – djhru

回答

2

這應該解決這一問題

public function index() { 

    $greetings = ""; 

    /* This sets the $time variable to the current hour in the 24 hour clock format */ 
    $time = date("H"); 

    /* Set the $timezone variable to become the current timezone */ 
    $timezone = date("e"); 

    /* If the time is less than 1200 hours, show good morning */ 
    if ($time < "12") { 
     $greetings = "Good morning"; 
    } else 

    /* If the time is grater than or equal to 1200 hours, but less than 1700 hours, so good afternoon */ 
    if ($time >= "12" && $time < "17") { 
     $greetings = "Good afternoon"; 
    } else 

    /* Should the time be between or equal to 1700 and 1900 hours, show good evening */ 
    if ($time >= "17" && $time < "19") { 
     $greetings = "Good evening"; 
    } else 

    /* Finally, show good night if the time is greater than or equal to 1900 hours */ 
    if ($time >= "19") { 
     $greetings = "Good night"; 
    } 

    return view('home', compact('greetings')); 
} 

和刀片,你可以做

{{ $greetings }} {{ Auth::user()->name }} 
+0

完美的作品,但我不知道爲什麼它顯示出問候箱! HTTP:// imgur。com/a/TjFQT – djhru

+0

您需要檢查您想要顯示內容的位置並進行相應渲染 – linktoahref

+0

我已將{{$問候語}}放置在您看到的名稱相同的位置,但它顯示在''標籤在頂部!名字是在正確的地方,但問候上升! – djhru

-1

鑑於

@if(session('status')) 
    <div class="alert alert-success"> 
    {{ session('status') }} 
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
    </div> 
@endif 

在控制器

return View::make('home')->with('status','msg'); 
+0

是的,但是時機呢?我如何得到一天中的什麼時間以及哪一天應該顯示哪一條消息? – djhru

+0

其簡單的js真的 - var currentdate = new Date();toastr.success('Great Day',currentdate); –

+0

我對JS不太好,也沒有計劃使用烤麪包機/模型我只想打印我的用戶儀表板的文本頂部,它站在那裏。無論如何,你有任何編碼樣本可以幫助我嗎? – djhru

0

創建一個輔助函數來包裝http://carbon.nesbot.com/docs/#api-comparison 像:

<?php 

function say_hello(\Carbon\Carbon $carbon) { 
    if($carbon->hour > 12) { 
     return lang('good_morning'); 
    } elseif ($carbon->hour > 19) { 
     return lang('good_evening'); 
    } 
    return lang('good_afternoon'); 
} 
+0

不打印任何東西! – djhru

+0

做uoy有lang線嗎? – michail1982

+0

目前沒有。 – djhru