In Controller :
$data = [
'name' => 'Jony',
'age'. => 18
];
return View::make("user/detail", compact('data'));
In view/user/detail.blade.php
<!DOCTYPE html>
<html lang="en-EN">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Details</h1>
<div class="user-informatio">
//
I want to display view : user.blade.php
//
</div>
</body>
</html>
In view/user/user.blade.php
<div>{{ $data['name'] }}</div> <br/>
<div>{{ $data['age'] }}</div>
I want to pass $data from detail.blade.php to user.blade.php and display user.blade form inside detail.blade.
Please give me any ideas.Thanks
You can #include user.blade.php file in it.
<div class="user-informatio">
//
I want to display view : user.blade.php
//
#include('user.user', $data)
</div>
try this
return view('user/detail')->with('data', $data);
then in the view just use $data
Related
I started laravel with Laravel8 and from what I was seeing was that they were moving towards using component templating such as:
<-- layout.blade.php -->
<html>
<body>
{{ $slot }}
</body>
</html>
<-- view.blade.php -->
<x-layout>
<main>Content</main>
</x-layout>
As opposed to #yield and #extends type templating:
<--layout.blade.php -->
<html>
<body>
#yields('content')
</body
</html>
<-- view.blade.php -->
#extends('layout')
#section('content')
Content
#endsection
Now I'm working towards creating dynamic titles per page.
<-- views/components/layout.blade.php -->
<html>
<head>
<title>{{ isset($title) ? $title . ' | ' . config('app.name') : config('app.name') }}</title>
</head>
<body>
{{ $slot }}
</body>
</html>
<-- views/articles/index.blade.php -->
<x-layout>
<main>Content</main>
</x-layout>
Then an Article Controller
public function index()
{
$title = 'Articles';
return view('articles.index', [
'articles' => Article::latest('published_at')->filter(request(['search', 'category']))->get(),
'title' => $title,
]);
}
Using the newer "component templating" style, how do I pass the $title from the articles/index.blade.php view to the layout component? Is this a limitation of this approach?
I've seen a number or writeups on how to achieve this using the #yields #extends approach, but trying to not have to rework my entire site if there is the capabilities with the approach I've taken.
I figured out how to address this. You can pass data to a parent component in the same way you pass data to a child component.
<-- views/articles/index.blade.php -->
<x-layout :title="$title">
<main>Content</main>
</x-layout>
Allowed the parent to be updated from the child.
I am trying to retrieve data from my database and show it in my view but there I get an error.
Here is my controller
public function index()
{
$Page=Superior::all();
return view('Myview.Firstpage')->with('Task',$Page);
}
And this is where I assign in the view
<body>
<p>this is our first page </p>
{{ $Task }}
</body>
</html>
but this task is creating error and it says that the Task is an undefined variable my whole page looks like this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>This is our first page </title>
</head>
<body>
<p>this is our first page </p>
{{ $Task }}
</body>
</html>
Superior is the name of my model from which I want to retrieve my data.
My routes files in web.php is
<?php
Route::get('First',function(){
return view('Myview.Firstpage');
});
i am learning laravel
In the index method of your controller
public function index()
{
return view('Myview.Firstpage')->with('tasks',Superior::all());
}
Keep in mind that the all() method returns a collection which you want to loop through in your view.
In your view, you should have:
#foreach($tasks as $task)
{{ $task->title }}
#endforeach
You need to also update your route to make use of the controller:
Route::get('/', 'TaskController#index');
You could visit https://laravel.com/docs/5.8/collections#method-all to learn more about collections.
Hi please try to pass you variable to view like this:
$Tasks = Superior::all();
return view('Myview.Firstpage', compact('Tasks'));
And then use a loop in your view like suggested in above comments.
#foreach($Tasks as $task)
{{ $task->title }}
#endforeach
I'm learning laravel 5.2 and trying to make a cms project by following a video tutorial. I have created the files as follows-
app/View/Composers/InjectPages.php
namespace App\View\Composers;
use App\Page;
use Illuminate\View\View;
class InjectPages
{
protected $pages;
public function __construct(Page $pages)
{
$this->pages = $pages;
}
public function compose(View $view)
{
$pages = $this->pages->all()->toHierarchy();
$view->with('pages', $pages);
}
}
app/Providers/AppServiceProvider.php
public function boot()
{
$this->app['view']->composer('layouts.frontend', Composers\InjectPages::class);
}
resources/views/welcome.blade.php
#extends('layouts.frontend')
#section('title', 'Welcome')
#section('heading', 'This is a heading')
#section('content')
<h1>Hello World</h1>
#endsection
public/themes/views/layouts/frontend.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>#yield('title') — Tuts24.com</title>
<link rel="stylesheet" type="text/css" href="{{ theme('css/frontend.css') }}">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand">
<img src="{{ theme('images/logo.png') }}" alt="Tuts24.com">
</a>
</div>
<ul class="nav navbar-nav">
#include('partials.navigation')
</ul>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
#yield('content')
</div>
</div>
</div>
</body>
</html>
public/themes/views/partials/navigation.blade.php
#foreach($pages as $page)
<li class="{{ Request::is($page->uri_wildcard) ? 'active' : '' }} {{ count($page->children) ? ($page->isChild() ? 'dropdown-submenu' : 'dropdown') : '' }}">
<a href="{{ url($page->uri) }}">
{{ $page->title }}
#if(count($page->children))
<span class="caret {{ $page->child() ? 'right' : '' }}"></span>
#endif
</a>
#if(count($page->children))
<ul class="dropdown-menu">
#include('partials.navigation', ['pages' => $page->children])
</ul>
#endif
</li>
#endforeach
But always getting the following error.
ErrorException in Container.php line 734:
Class App\Providers\Composers\InjectPages does not exist (View: /path/to/project/resources/views/welcome.blade.php)
I'm not sure, is these informations are sufficient to find out the error for you. If needed more information please let me know.
Looking forward to your response. Thanks.
Updates-
After several tries as answer it seems to me the following file is also related to this error. That's why I'm adding this code also and sharing a dropbox link of my learning project.
app/View/ThemeViewFinder.php
<?php
namespace App\View;
use Illuminate\View\FileViewFinder;
class ThemeViewFinder extends FileViewFinder
{
protected $activeTheme;
protected $basePath;
public function setBasePath($path)
{
$this->basePath = $path;
}
public function setActiveTheme($theme)
{
$this->activeTheme = $theme;
array_unshift($this->paths, $this->basePath.'/'.$theme.'/views');
}
}
Dropbox link
https://www.dropbox.com/sh/v8n1qvix20hywmo/AABVcs8DqvEhI89lw98mbxbya?dl=0
Try this:
composer('layouts.frontend', '\App\View\Composers\InjectPages::class');
Also, try to run composer dumpauto command.
Check it out
composer('layouts.frontend', '\App\View\Composers\InjectPages');
or
composer('layouts.frontend', \App\View\Composers\InjectPages::class);
Try by adding below code into composer.json
composer.json
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files":[
"app/View/Composers/InjectPages.php"
]
},
Do : composer dump-autoload
in : app/Providers/AppServiceProvider.php
public function boot()
{
view()->composer(
'layouts.frontend', 'App\View\Composers\InjectPages'
);
}
This is my page in View.
<!DOCTYPE html>
<?php $this->load->view('partials/page_head');?>
<body>
<div class="wrapper">
<header>
<div class="logo">Logo</div>
</header>
<?php $this->load->view('partials/menu');?>
<div id="content">
<?php $this->load->view('partials/', $subview); ?>
</div>
<footer>© 2012 Codeigniter.tv</footer>
</div>
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
</body>
</html>
This is my menu page inside partials folder.
<menu>
<ul>
<li>Listing</li>
<li>Detail</li>
</ul>
</menu>
Below is the controller part.
<?php
class Example extends CI_Controller {
public $data = array('subview' => 'Oops, forgot to set a subview');
public function __construct(){
parent::__construct();
$this->load->helper('url');
}
public function listing() {
$this->data['subview'] = 'listing';
$this->load->view('layouts/layout', $this->data);
}
public function detail() {
$this->data['subview'] = 'detail';
$this->load->view('layouts/layout', $this->data);
}
}
I could not display the content in the file 'listing.php' which is in partials folder. When i run the project in localhost it saying not found. Can anyone help me to correct my problems.
modify views as bellow.
<!DOCTYPE html>
<?php $this->load->view('partials/page_head');?>
<body>
<div class="wrapper">
<header>
<div class="logo">Logo</div>
</header>
<?php $this->load->view('partials/menu');?>
<div id="content">
<?php $this->load->view('partials/'.$subview); ?>
</div>
<footer>© 2012 Codeigniter.tv</footer>
</div>
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>') </script>
</body>
</html>
modify your menu file as below
<menu>
<ul>
<li>Listing</li>
<li>Detail</li>
</ul>
</menu>
I'm new in CodeIgniter. I want to create Master Page or Layout with base style that will be contain Menu, footer and etc. I don't want to write repeating content in all pages and load it automatically for all pages. For example, I can create Master Page in asp.net or Layout in asp.net mvc. I'm sure I can do it in CodeIgniter.
lets assume you have an html page
<html>
<head>
<title> Hello World </title>
</head>
<body>
<div id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</div>
<div id="main-content">
<!-- this is the dynamic part -->
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
you could split it into
1- header
2- menu
3- main content
4- footer
you basically put
<html>
<head>
<title> Hello World </title>
</head>
<body>
in one view called "view_header"
then you put
<div id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</div>
<div id="main-content">
in a view called "view_menu"
and then you put
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
in a view called "view_footer"
then in your controller
$this->load->view('view_header');
$this->load->view('view_menu');
$this->load->view('YOUR_VIEW');
$this->load->view('view_footer');
The other solution, which I see is better: create a view called view_template.php
<html>
<head>
<title> Hello World </title>
</head>
<body>
<div id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</div>
<div id="main-content">
<?php $this->load->view($content); ?>
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
in the controller lets say you want to call a view called About
$data = array('content'=>'about');
$this->load->view('view_template',$data);
Dynamic Layout
You would create a new Controller with a public $template variable
Your extended Controller will then inherit the $template variable from the Master Controller.
MY_Controller
class MY_Controller extends CI_Controller
{
public $template=null;
public function __construct()
{
if(is_null($this->template)){
$this->template = 'layouts/default';
}
}
}
Admin_controller
class Admin_Controller extends MY_Controller
{
public function __construct()
{
//Still inherits from MY_Controller
//this time, any controller extending Admin_Controller
//will point to 'views/layouts/admin'
if(is_null($this->template)){
$this->template = 'layouts/admin';
}
}
}
-
class User extends MY_Controller
{
public function index()
{
//$this->template is inherited
//from MY_Controller
//which point to 'views/layouts/default'
//We can also load a view as data
//ie no master layout, INTO our master layout
//note we don't pass $this->template
//and set the third param as true
$dynamic_sidebar = $this->load->view('views/sidebar/dynamic', array(
'data' => 'some_data'
), true);
return $this->load->view($this->template, array(
'partial' => 'users/index' //partial view,
'dynamic_sidebar' => $dynamic_sidebar
));
}
}
Views/Layouts/default
<body>
//load the main view
//in our example we have also loaded
//a dynamic sidebar with this view
<?php $this->load->view($partial); ?>
<?php $this->load->view($dynamic_sidebar); ?>
//load a static view
//views/sidebar/static
<?php $this->load->view('sidebar/static'); ?>
</body>
Following the idea of a master page in Laravel, we can do this:
Controller code
$this->load->view('show');
View "show.php"
Set values for master page and then pass the variables to master page. Keep your master page codes inside ob_start() & ob_get_clean().
<?php $page_title = "My first page"; ?>
<?php ob_start(); ?>
Your header stylesheet links and scripts
<?php $page_header = ob_get_clean(); ?>
<?php ob_start(); ?>
<div>
<h1>This is a Header</h1>
<?php $this->load->view('Partials/list'); ?>
</div>
<?php $page_content = ob_get_clean(); ?>
<?php ob_start(); ?>
Your footer html or scripts
<?php $page_footer = ob_get_clean(); ?>
<?php $this->load->view('Layout/app',array(
'page_title' => $page_title,
'page_header' => $page_header,
'page_content' => $page_content,
'page_footer' => $page_footer
)); ?>
Partial view "Partials/list.php"
In case you don't want your code to be crowded. You can create some partial views to keep things simple.
<ul>
<li>LIst item 1</li>
<li>LIst item 2</li>
<li>LIst item 3</li>
</ul>
Master Page "Layout/app.php"
<html>
<head>
<title><?= v($page_title) ?></title>
<?= v($page_header) ?>
</head>
<body>
<?= v($page_content) ?>
<?= v($page_footer) ?>
</body>
</html>
<?php
function v(&$var)
{
return isset($var) ? $var : '';
}
So that will genarate code:
<html>
<head>
<title>My first page</title>
Your header stylesheet links and scripts
</head>
<body>
<div>
<h1>This is a Header</h1>
<ul>
<li>LIst item 1</li>
<li>LIst item 2</li>
<li>LIst item 3</li>
</ul>
</div>
Your footer html or scripts
</body>
</html>
What we probably do is separate view files for header, menu, footer, etc.. that is common for all pages. And include them inside each view. like
$this->view('header');
$this->view('menu');
//Some specific content
$this->view('footer');
If you need same functionality without copying the above to all views, you need to create a function in your controller as follows:
private function myviewfunction($current_view)
{
$this->load->view('header');
$this->load->view('menu');
$this->load->view($current_view);
$this->load->view('footer');
return NULL;
}
and call this function in all your pages (methods)
$this->myviewfunction('about'); //About is the specific view for the method