include controller in view laravel 5 - php

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

Related

How to pull database table values into a template file?

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

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

Can not view the rows of the authors table (Laravel)

Hello and thanks for the help! I'm new to laravel and i'm trying to learn it through a tutorial that i found in youtube! http://www.youtube.com/watch?v=lceNwQf-ufY
Everything has been worked correctly until now!
I'm trying to make a list of all the rows of my table authors
My Controller authors.php
<?php
class Authors extends BaseController{
public function get_index(){
return View::make('authors.index')
->with('title', 'Authors Title')
->with('authors', Author::all());
}
}
My Model author.php
<?php
class Author extends Eloquent{
protected $table = 'authors';
}
My View index.blade.php
#extends('authors.layout')
#section('content')
<h2>Authors Page</h2>
<h3>Welcome to the authors page</h3>
<ul>
#foreach($authors as $author)
<li>{{ $author->name }}</li>
#endforeach
</ul>
#stop
And my layout layout.blade.php
<!DOCTYPE html>
<html>
<head>
<title>{{ $title }}</title>
</head>
<body>
#yield('content')
</body>
</html>
Routes.php
<?php
Route::get('/', function()
{
return View::make('hello');
});
Route::get('users', function()
{
return View::make('users');
});
Route::get('authors', array('uses'=>'authors#index'));
You should change in your routes.php
Route::get('authors', array('uses'=>'authors#index'));
to
Route::get('authors', array('uses'=>'Authors#get_index'));
Using Authors#get_index the first part is the name of controller (in your case it's Authors and the second part is the name of method - in your case it's get_index and not index.

Laravel templating engine confusion

I'm really confused. that's about several hours that I trying to figure out the relationships of blade system.
However I yet haven't learned all of its points.
My Question is:
I have defined the master.blade.php which contains the following code:
<!doctype HTML>
<html>
<head>
#section("header")
#show
</head>
<body>
#yield("content")
</body>
<footer>
#section("footer")
</footer>
</html>
But I do not know how should I define header and content in the following controller:
class sample extends BaseController
{
protected $layout = "layout.master";
function show()
{
// the code which goes here
}
}
In constructor of BaseController:
$this->layout->header = View::make('header');
In master.blade.php:
{{ $header }}

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