我想将de设置表中的数据从数据库传递到我的布局视图。
我如何完成它?
$item = Setting::find(1);
return view($this->controller.'/show')->with( 'item', $item);
解决方案:
public function boot() {
if( !isset( $_SESSION['adminTitle'] ) ){
$item = Setting::find(1);
$item = $item->toArray();
$_SESSION['adminTitle'] = $item['title'];
$_SESSION['adminEmail'] = $item['email'];
$_SESSION['adminLogo'] = $item['logo'];
}
}
答案 0 :(得分:1)
为什么不简单?:
// File app/Http/Controllers/ExampleController.php
//
class ExampleController extends Controller
{
public function show()
{
//
$setting = Setting::find(1);
return view('example', ['setting' => $setting]);
}
}
在Blade视图中:
<!-- resources/views/example.blade.php -->
{{ $setting->title }}
{{ $setting->logo }}
...
但是,如果您想在所有视图之间共享设置,可以添加此中间件:
// File app/Http/Middleware/ViewShareSettingMiddleware
//
class ViewShareSettingMiddleware
{
public function handle($request, Closure $next)
{
$setting = Setting::find(1);
view()->share('setting', $setting);
return $next($request);
}
}
答案 1 :(得分:0)
在以下位置创建视图:
\资源\视图\
示例:\resources\views\index.blade.php
$data['item'] = Setting::find(1);
return view('index')
->with( $data);
或
$item = Setting::find(1);
return view('index', compact('item');
查看:{{$item}}