How to pull database table values into a template file? - php

How can I pull database values into a template? (footer.blade.php). I can pull data into home.blade.php just fine but since I'm just doing #include('templates.footer'), I'm not sure how to pull table values into footer.blade.php
This is my current setup:
HomeController.php
public function index() {
$general = DB::table('general')->get();
return view('home', ['general' => $general]);
}
home.blade.php
#extends('templates.master')
#section('content')
#foreach($general as $key => $data)
{{ $data -> content}}
#endforeach
#endsection
master.blade.php
<!doctype html>
<html lang="en">
<head>
</head>
<body>
#include('templates.header')
#yield('content')
#include('templates.footer')
</body>
footer.blade.php
<footer class="footer padding-medium">
This is the footer
</footer>
I am looking to do something like this:
footer.blade.php
<footer class="footer padding-medium">
#foreach($general as $key => $data)
{{ $data -> content}}
#endforeach
</footer>

You can use AppServiceProvider.php which is located in app/providers/AppServiceProvider.php using View composer.
Like
View::composer('templates.footer',function($view){
$view->with('results', DB::table('general')->get());
});
In footer
<footer class="footer padding-medium">
#foreach($results as $key => $data)
{{ $data -> content}}
#endforeach
</footer>
Do not forget import DB & View in the AppServiceProvider.php file

Related

how to send data from controller to include/header blade file but return another blade file in laravel

I want to include send my data from my controller to my header.blade.php file in view
but i want to return the other view in the laravel as mentioned below:-
public function handleProviderCallback()
{
$user = Socialite::driver('google')->stateless()->user();
$finduser = User::where('google_id', $user->id)->first();
if($finduser) {
Auth::login($finduser);
$google_avatar = $user->getAvatar();
view('pages.home',['google_avatar', $google_avatar]);
return redirect()->route('home',);
}
}
By extending a layout is the cleanest way.
From the docs:
<!-- Stored in resources/views/layouts/app.blade.php -->
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
The child:
#extends('layouts.app')
#section('title', 'Page Title')
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
<p>This is my body content.</p>
#endsection
By just returning the "child" view from the controller, the data will be available in the header as well. If you pass it in the #extends declaration

Correct view output Laravel

That's my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Spieler;
class SpielerController extends Controller
{
public function getSpieler(Request $request){
$spielID = $request->get('spielID');
$spielerOutput = Spieler::where('spielPlanID', '=', $spielID)->get();
return view('spieler')->with('alleSpieler', $spielerOutput);
}
}
here you can see my view which I will trigger
#extends('app')
#section('contentSpieler')
<h1>Spieler</h1>
#if(count($alleSpieler) > 0)
#foreach($alleSpieler as $teamSpieler)
{{ $teamSpieler->note }}
#endforeach
#endif
#endsection
And here is my main index/app page
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dateneingabe</title>
<link rel="stylesheet" href="/css/app.css">
<script src="{{ asset('/js/jquery-3.2.1.min.js') }}"></script>
</head>
<body>
<div class="container">
<h1>Spiele und Spieler AJAX - Drop Down</h1>
<div class="col-lg-4">
#yield('contentSpiel')
</div>
<div class="col-lg-4">
#yield('contentSpieler')
</div>
</div>
</body>
</html>
When my controller is going to trigger i get back this in my console
<script src="http://localhost:8000/js/jquery-3.2.1.min.js"></script>
</head>
<body>
<div class="container">
<h1>Spiele und Spieler AJAX - Drop Down</h1>
<div class="col-lg-4"></div>
<div class="col-lg-4">
<h1>Spieler</h1> 2.5 Blasdasd
</div>
</div>
</body>
</html>
But I want only that {{ $teamSpieler->note }} should appear in my index/app page in the part #yield('contentSpieler'). What is wrong with my code? The correct value of note 2.5 is in the html console output but at the moment I don't know why there is no output on my index/app page?
if your master view file location is view->index->app.blade.php then
your view file should be like this
#extends('index.app')
#section('contentSpieler')
<h1>Spieler</h1>
#if(count($alleSpieler) > 0)
#foreach($alleSpieler as $teamSpieler)
{{$teamSpieler->note}}
#endforeach
#endif
#endsection
here your master view file location should be correct to extend it in your view file

How to arrange display position of conten when using blade in Laravel?

I have a folder in View:
resources -> view
- layouts
+ master.blade.php
- partials
+ footer.blade.php
+ header.blade.php
- news
+ show.blade.php
+ list.blade.php
- apartment
+ show.blade.php
My code in master.blade.php:
<head>
<script src="{{{ URL::asset('js/theme.min.js')}}}"></script>
</head>
<body>
<div class="main-container">
#include('partials.header')
#yield('content')
#include('partials.footer')
</div>
</body>
In news>show.blade.php:
#extends('layouts.master')
#section('content')
<div class="show-news">show all news at here</div>
#stop
In apartment>show.blade.php:
#extends('layouts.master')
#section('content')
<div class="show-apartment">show all apartment at here</div>
#stop
In HTML file have structure like:
header
content
- show slider image
- show list news
- show list apartment
footer
Because all file is define with section('content').
I don't know how to arrange display position of `section('content') correctly with my HTML file.
I think it should be like:
In news>show.blade.php: #section('news')
In apartment>show.blade.php: #section('apartment')
In master.blade.php:
<head>
<script src="{{{ URL::asset('js/theme.min.js')}}}"></script>
</head>
<body>
<div class="main-container">
#include('partials.header')
#yield('news')
#yield('apartment')
#include('partials.footer')
</div>
</body>
But I see on almost project don't using like this.
You can create another layout page that extends master page like this
master.blade.php
<head>
<script src="{{{ URL::asset('js/theme.min.js')}}}"></script>
</head>
<body>
<div class="main-container">
#include('partials.header')
#yield('content')
#include('partials.footer')
</div>
</body>
newlayout.blade.php
#extends('master')
#section('content')
#yield('news')
#yield('appartments')
#endsection
And you can use this layout for pages like you described, for another you can use master

include controller in view laravel 5

I'm new to laravel and I got stuck.
My problem is I want 2 sections (navigation, content) that has dynamic data
Here's some code
Main Blade
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Portfolio</title>
</head>
<body>
<div class="navigation">
#yield('menu')
</div>
<div class="content">
#yield('content')
</div>
</body>
</html>
portfolio blade
#extends('main')
#section('content')
#foreach($data as $portfolio)
<img src='{{ URL::asset("images/$portfolio->picture.jpg") }}'/>
#endforeach
#stop
and my navigation blade
#extends('main')
#section('menu')
#foreach($menuknoppen as $menuknop)
<a href='{{ URL::to("$menuknop->menu_url") }}'>{{$menuknop->menutitle}}</a>
#endforeach
#stop
the portfolio blade has a controller, but also the menu blade has a controller
Edit1:
the problem is the navigation isn't showing even if I add static text
Edit2:
My controllers
my portfolio controller
/**
* Show the profile for the given user.
*
* #param int $id
* #return Response
*/
public function index(){
//here comes a whole list with what i've done
$results = DB::table('projects')->get();
//return $results;
$data = array();
foreach ($results as $key => $result) {
$data[] = $result;
}
return view('portfolio.portfolio')->with('data', $data);
}
public function getProject($portfolio_url){
//this gets the project thats clicked
$results = DB::select('select * from projects where portfolio_url = ?', array($portfolio_url));
return view('portfolio.single')->with('data', $results['0']);
}
}
my navigation controller
class menuController extends Controller {
/**
* Show the profile for the given user.
*
* #param int $id
* #return Response
*/
// public function __construct($table){
// $results = DB::table($table)->get();
// return view('menu')->with('menuknoppen', $results);
// }
public function index(){
$results = DB::table('navigation')->get();
return view('menu')->with('menuknoppen', $results);
}
}
Your main blade should be:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Portfolio</title>
</head>
<body>
<div class="navigation">
#include('menu');
</div>
<div class="content">
#yield('content')
</div>
</body>
</html>
Your portfolio should be:
#extends('main')
#section('content')
#foreach($data as $portfolio)
<img src='{{ URL::asset("images/$portfolio->picture.jpg") }}'/>
#endforeach
#stop
Navigation field should be:
//Don't use extends here
#foreach($menuknoppen as $menuknop)
<a href='{{ URL::to("$menuknop->menu_url") }}'>{{$menuknop->menutitle}}</a>
#endforeach
Pass multiple data
public function index()
{
$data = //data code;
$results = // results code
return view(portfolio.portfolio, compact('data', 'results'));
}
You have mixed the concept of #yield , #include and #extend
#yield provides a place for you to replace, so when you call #extend in other view you can reuse the template in view which you extend and replace the part with #yield
#include means this part of code is always replaced by the view it defined
So when you are designing a webpage, you need to make sure what is "always called" (use #include) and what could be replaced (use #yield)
As an assisting explanation to aldrin27 working code, I hope this make your mind clearer on the blade template, it rocks! :D

Laravel Blade Layouts

This is the problem, its not reading #layout()
Inside the folder View/authors/index.blade.php
#layout('layouts.default')
#section('content')
Authors Home Page
#endsection
In the folder Controllers/authors.php
class Authors_Controller extends Base_Controller{
public $restful = true;
public function get_index(){
return View::make('authors.index')->with('title', 'Autores e Livros');
}
}
and inside the folder View/layouts/default.blade.php
basic html
<html>
<head> <title> {{ $title }} </title> </head>
<body>
{{ $content }}
</body>
</html>
Where's my error? why is it not reading?
First: Inside View/layouts/default.blade.php
<html>
<head> <title> {{ $title }} </title> </head>
<body>
#yield('content')
</body>
</html>
And second: Update
I'm like 99% sure that '#layout('layouts.default')' in your View/authors/index.blade.php is not on the first line. It has to be at the top of the page.
It seems in Laravel 4 you should use #extends('layouts.default') instead of #layout('layouts.default')

Categories