My app.blade.php file is my main file that will #yield all my other blade template files. I noticed that the Auth class is available to this file. I cant figure out how to add one of my custom classes, Projects so it is available to app.blade.php.
app.blade.php contains a drop down menu where I need to list all my projects, but if I try something like
Projects::where('user_id','=',Auth::user()->id)->get()
I get an error saying the Projects class is not found. How can I add this to my master template just like how Auth is available? Or, do I need to go about this differently?
You do it by injecting the class into your template.
#inject('project', 'App\Project')
{{ $project->where('user_id','=',Auth::user()->id)->get() }}
Just make sure your namespace is correct.
http://laravel.com/docs/5.1/blade#service-injection
Related
I am building a website and I'm creating an admin and user login form but I'm facing some problems because when I try to type for example #extends or any other function with # it doest change color or do anything (sorry for not being to clear). So basically it doesn't act as a function but just as a simple text that later shows up at the display on the website. If anyone can help me i would be very happy because I have been trying for hours and its important for me to finish this website
Image of code in my editor where the color is not changing
Image of code that isn't working correctly
adminLoginController
This is what i get on localhost
This is what i did trying to add layout
The lack of color highlighting is "syntax highlighting". Your editor would need support for Blade to be able to highlight the Blade syntax correctly.
The Blade directive #extends needs to take an argument/expression:
#extends('layouts.app')
"When defining a child view, use the Blade #extends directive to specify which layout the child view should "inherit".
Laravel 6.x Docs - Blade - Extending a Layout #extends()
When it comes to rendering these templates, just make sure the file ends with .blade.php if you want it to be a Blade template.
"Blade view files use the .blade.php file extension and are typically stored in the resources/views directory."
Laravel 6.x Docs - Blade - Introduction
Update:
You do not have a layouts directory in resources/views. You need to extend an existing layout. By the looks of your structure you mean to extend auth.layouts.app:
#extends('auth.layouts.app')
Also you are not defining a #section('content') to be #yielded in the layout.
#extends('what.ever.layout.you.want.to.use')
#section('content)
<!-- your content here -->
#endsection
Please make sure your blade file filenames are in the format of:
my_custom_file.blade.php
And not just:
my_custom_file.php
If you just end your files in .php then Laravel will not process it as a Blade template. The #extends directive is a Blade template feature and will need to be pre-processed into regular PHP by Laravel for it to work. It does this automatically when you use the return view('my_custom_view') syntax. This line would look for a file in resources/views/my_custom_view.blade.php first.
From your file structure layouts.blade.php is located in your views directory, so what you need to do is:
#extends('layouts')
To extend the layouts.blade.php
I have a partial view with a script I want to include in all my views, I dont't wanna #include this partial in all my layouts and blade views but instead add it to all rendered views, my partial looks like this:
<script type="text/javascript">window.$app = {!! json_encode(app(App\Helpers\Javascript::class)->app) !!};</script>
How can I do this?
You could:
include it in your main(s) layout template (the one(s) handling <html> and <body> tags)
you could use a Laravel View Composer to add the JSON data to the required views. Those views would so include the JSONed data, that you'd echo in the right place to be handled by your JS.
you could check the Php-Vars-To-Js package that allows you to put some data from PHP in a custom JS namespace (eg window.yourNameSpace). And then use it from a View Composer or the specific controller's methods concerned by your views.
I'm wanting to create a website with laravel framework. I had made layout but now, have some zone i don't know how to set content for it. Ex: 2 zone of me are left-menu and cart (please view picture). My left-menu will get content from table: categories and cart will get content from package cart [Cart::content()].
It's on layout and of course, all page will have it. But i don't know how to give content of categories and cart() for it. Please help me
I think that you should to use View Composer.
https://laravel.com/docs/5.6/views#view-composers
Use Blade templates, as found here: https://laravel.com/docs/5.6/blade
Wherever in your page you want to print content, use the {{ $mycontent }} construct. You can also use confitionals and loop structures like #if and #foreach to loop through collections.
Then, in your controllers, you can just call the view and pass it content from your database or wherever you get it by doing something like:
return response()->view(“myView”, [“mycontent” => $content], $httpStatus);
You may opt for afterMiddleware if you want it on every page. Create a section on the master blade page (usually app.blade.php) and fill it in the middleware just like you would in any other controller. You can create a middleware by running php artisan create:middleware Cart. A file will be created at app/Http/Middleware/Cart.php.
Register the middleware in the app/Http/kernel.php file.
You may have to add a Auth::check() condition to avoid errors.
A started to work with Phalcon framework, included Blade templating. It's already works, but unfortunately i didn't find the right way to include css and JS assets in master.blade.php.
If I add the assets like $this->assets->addCss("css/bootstrap.min.css"); in the controller I can not include it in the master template file.
For example, my indexAction looks like this:
public function indexAction(){
$this->assets->addCss("css/bootstrap.min.css");
$this->assets->addJs("js/bootstrap.min.js");
return $this->blade->make('index.index');
}
Thanks for any help!
Well - you should add blade as actual template engine into phalcon view.
Your class should extends Engine implements EngineInterface. If you will do it it could be nice to add it to incubator repository.
https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Mvc/View/Engine check out implementation of other engines for more how are they made. Then you could just do {{ assets.outputJss() }}, example from volt/twig, not sure how exactly it should look like in blade, never used it.
Also what's wrong with volt? It's faster than blade and have many features.
I have a layout in which I want to add classes to the body depending on which view is being displayed, i.e.:
<body class="layout-default page-index">
I can do this in Twig quite easily (OctoberCMS uses Twig) but I can't see a way to do it with Laravel's Blade templates (which I prefer anyway).
I'd rather not have to pass a variable to every View::make with the view name as this seems redundant.
Good question, very smart way to work with css.
You would use this typically by adding classes to the body tag, or the main container div.
within your routes or filters file:
View::composer('*', function($view){
View::share('view_name', $view->getName());
});
Within your view:
<?php echo str_replace('.','-',$view_name);?>
<?php echo str_replace('.','-',Route::currentRouteName());?>
These should get you everything you need.