iT邦幫忙

0

Laravel 如何定義全域變數,每一個畫面都可以讀取該變數至資料庫抓的資料

Victor 2018-04-19 16:34:5710154 瀏覽

請教各位大大!

如何在 LARAVEL 定義一個變數,固定會去查詢資料表,並在每一個畫面可以讀取該變數,取得資料。

有點像 LARAVEL Auth::id(); 每一頁都可以使用都找的到 User ID

q00153 iT邦新手 3 級 ‧ 2018-04-19 16:49:41 檢舉
https://laravel-china.org/topics/3265/laravel-53-add-custom-facade-steps
Victor iT邦新手 2 級 ‧ 2018-04-19 18:01:24 檢舉
謝謝q大! 這好像也是一個好方法!
晚點來研究看看 ^^
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
最佳解答
Victor iT邦新手 2 級 ‧ 2018-04-19 17:48:22 檢舉

謝謝大大! 我研究出來了!想再請問大大一個問題

我用的方法是 App/AppServiceProvider 有成功讀取所有資料,
如果想設定全域變數,並且篩選 例如:ID 1的詳細資料該怎麼撰寫?

public function boot()
    {
        // Using view composer to set following variables globally
        view()->composer('post',function($view) {
            $view->with('post_info', POST::where('id',$id)); 
        });
    }

例如 http://localhost/post/1
想在有關 post 的畫面 都去讀 id = 1 找到 POST 詳細資訊

請問 $view->with('post_info', POST::where('id',$id));
$id 要怎麼取得?

要看你的Route怎麼設定才知道怎麼取值,參考以下…
https://stackoverflow.com/questions/27298426/how-to-pass-get-parameters-to-laravel-from-with-get-method

我要發表回答

立即登入回答