Reputation: 2400
I want to pass data from de setting table from the database to my layout view.
How do I get it done?
$item = Setting::find(1);
return view($this->controller.'/show')->with( 'item', $item);
Solution:
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'];
}
}
Upvotes: 1
Views: 64
Reputation: 16580
Why not simply this?:
// File app/Http/Controllers/ExampleController.php
//
class ExampleController extends Controller
{
public function show()
{
//
$setting = Setting::find(1);
return view('example', ['setting' => $setting]);
}
}
Within the Blade view:
<!-- resources/views/example.blade.php -->
{{ $setting->title }}
{{ $setting->logo }}
...
But, if you want to share settings between all your views, you can add this middleware:
// File app/Http/Middleware/ViewShareSettingMiddleware
//
class ViewShareSettingMiddleware
{
public function handle($request, Closure $next)
{
$setting = Setting::find(1);
view()->share('setting', $setting);
return $next($request);
}
}
Upvotes: 1
Reputation: 1901
Create your view in:
\resources\views\
Example: \resources\views\index.blade.php
$data['item'] = Setting::find(1);
return view('index')
->with( $data);
Or
$item = Setting::find(1);
return view('index', compact('item');
View: {{$item}}
Upvotes: 0