For the past couple of days I have been trying to get into Laravel 5 blade system. Yet for some absurd reason I cannot get things to work. Here's how things look now:
UserController -> index
public function index()
{
return view('app');
}
So obviously we are getting a view so I create a blade file in views called app.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
</html>
Alright so we are all set so when I load the page all I get is "Document" in the title.
Here's the rub: if I change the title tag to something else like so:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Application</title>
</head>
<body>
</body>
</html>
And reload the page one would expect to see the title change? Well guess what? Nope. That didn't happen. The original title "Document" is still there.
Someone want to tell me whats going on here and how to fix it?
Are you sure it's not just cached? Try this: go to your \storage\framework\views and delete everything except .gitignore file, and try again.
If laravel is caching your view, clearing the browser cache won't help you as the caching is done server-side.
If you are actually using laravel 5.1, you can type on your terminal php artisan view:clear
More info: laravel.com/docs/5.0/cache
Related
I have this relation on my product model.
public function productInventory(){
return $this->hasOne('App\Models\Ecommerce\Inventory', 'product_id')->where('is_deleted', 0);
}
Sometimes, i may not have inserted that specific product in my inventory table. So, this relation is still in play with view but will try to get the productInventory->price which will throw an error, cannot get price of null object. So, I've done this now which is just a simple thing to do. But the count is 1 or greater than 1, because there are other products. So how will I be able to return only null for that specific product which has no data in the inventory table?
public function productInventory(){
$has_inv = Inventory::where('product_id',$this->id)->where('is_deleted', 0)->count();
if($has_inv < 1){
return null;
}
return $this->hasOne('App\Models\Ecommerce\Inventory', 'product_id')->where('is_deleted', 0);
}
First, the method productInventory() must return the relationship. it does not matter that there is no connected Inventory for some records. This method must return the relationship, that's all... Confusing this return with the actual state of the DB ("for this specific product there is no inventory"), is a real no go, IMHO.
So, if your problem lies in the view, there you get an error when there is no inventory, a better solution would be:
Check in the view if there is an object available and only then check for the price attribute on that object.
You can check it with something like...
#if(is_null($productInventory))
{{$productInventory->price}}
#endif
BTW this is an EXAMPLE of code, not actual real code. But it gives you an idea of where to solve this problem.
Because, you are solving this in the wrong place. The relationship method is NOT the place, imho.
If you use this in a lot of views, you have to optimize your VIEW structure (by using includes!), not move with problem to a diferent place in your code.
Make an include for the view code you use a lot and include is with something like:
#include('product_incventory_price_data')
About includes in Blade
Let's use the example for your <head> code in your webpages. You can add this to all individual web pages (your view files). let's look at two different views (A en B)
file: view_a.blade.php
<html>
<head>
<meta charset="utf-8">
<title>{{$page->title}}</title>
<meta name="description" content="{{$page->description}}">
<meta name="keywords" content="{{$page->keywords}}">
</head>
<body>
...stuff for view A
</body>
</html>
file: view_b.blade.php
<html>
<head>
<meta charset="utf-8">
<title>{{$page->title}}</title>
<meta name="description" content="{{$page->description}}">
<meta name="keywords" content="{{$page->keywords}}">
</head>
<body>
...stuff for view B
</body>
</html>
A better solution: move all <head> code to a different file (in a folder like views/shared). And include that file in both A en B views.
shared/head.blade.php
<head>
<meta charset="utf-8">
<title>{{$page->title}}</title>
<meta name="description" content="{{$page->description}}">
<meta name="keywords" content="{{$page->keywords}}">
</head>
view_a
<html>
#include('shared/head')
<body>
...stuff for view A
</body>
</html>
view_b
<html>
#include('shared/head')
<body>
...stuff for view B
</body>
</html>
You can do this with all kinds of pieces of view code. Also your product price information could be move to one place and used in multiple other views with the #include()...
The data you pass to the views by your controller is passed also to the include files. Don't worry about that.... :-)
Laravel documentation about views & sub-views
I want to create dynamic url and content like wordpress platform but I am doing something wrong. Kindly check my code:
<?php
$pages = array("story1", "story2", "story3");
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>This is my page title.</title>
<link rel="canonical" href="http://www.xyz1.com/kids-english-<?php
echo $pages[1];?>.php"/>
<!-- here the url is creating but gives error 404 -->
</head>
<body>
<p>Hello, <?php echo $pages[1];?></p><br>
<?php echo $pages[2];?>
<!-- here the second url is creating but gives error 404 -->
</body>
</html>
Now I want to get the content from database and want to load somefile.php
Here starts my second problem. I create urls by foreach loop but how to load all the content to somefile.php and how will one somefile.php will handle all the different urls and different content created dynamically. I am confused here.
Your suggestions would be welcome.
Thank You.
Well.. applications like Wordpress, Shopware and so on are built on a MVC-Design scheme like every other good coded project.
You can go for frameworks like Symfony or build your own routing mechanism.
All in all it's a little bit more tricky as you did mentioned your thoughts.
I'm creating web application using laravel 5. Everypage has "//" on up-left corner. What is causing this?
The app.blade.php looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="jumbotron">
#yield("content")
<p>Above content generated by MVC</p>
</div>
</div>
</body>
</html>
Well it certainly isn't anything in the blade template that's doing it.
What is probably happening is that you've got somewhere in your code a line which says echo "//"; or something similar, or a rogue line of code before your <?php block starts -- maybe you were trying to comment out a block of code that includes a <?php block.
That line doesn't have to be in the template; it could be anywhere in the code; if it's run before the template is output, then you will get the kind of effect that you're reporting here.
As for where the line is and what it's doing there, that's something you'll have to work out for yourself. But you can start by searching your codebase for echo or print statements, and for //<?php.
I need a little help here...
When I try to render a simple template in blade, I get a bad rendered html.
I have master.blade.php and home.blade.php inside views.admin directory.
<!-- master.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Test Title</title>
</head>
<body>
Content
</body>
</html>
<!-- home.blade.php -->
#extends('admin.master')
The rendered HTML when I call return view("admin.home"); looks like this:
<html lang="en">
<head></head>
<body>
<meta charset="utf-8">
<title>Test Title</title>
Content
</body></html>
Any ideas of what is happening?
Thanks.
For me, this was caused by adding a ; to end of #extends('layouts.app')
I just spent about an hour trying to solve this, before deciding to make a new laravel app to paste new files over until it was fixed. It turns out my issue was because of a random character inserted into one of my config files (probably when trying to type in a url before realising my browser wasn't the active window). So if anyone can't seem to figure out why it isn't working after fixing their blade templates, check this.
Usually the case for the "" character appearing is that it has been introduced by using a non UTF-8 encoded text editor or was introduced in a different way. My suggestion would be to use something like Sublime Text or Notepad++ to create fresh versions of each of these three documents. This usually happens because of a copy/paste without realizing it or using a text editor that has "extra formatting" that you cannot see (e.g. Word). The character is ZERO WIDTH NO-BREAK SPACE (you can see more information here). So, it's not a Laravel Blade issue, it's that extra whitespace-type characters are being introduced by whichever text editor you're using.
I know the thread is old, but I faced this issue today and removing BOM helped me to fix it. One hint - if you are using PHPStorm, there is option to remove BOM, without need to copy the file and save it with another file editor. Just right click on the file and press "Remove BOM". Works like charm!
I know this is an old question, but for the sake of someone as me that had the same problem and came up to this page, I want to share what solved my problem.
As you may find in laravel's layout documents, it is stated that a master template should be used like this:
#extends('layouts.master')
#section('title', 'Page Title')
#section('sidebar')
##parent
<p>This is appended to the master sidebar.</p>
#stop
#section('content')
<p>This is my body content.</p>
#stop
My problem solved by adding #stop at the end of #section('content'). I hope this helps.
I'm using Laravel 4 locally with EasyPHP 14.1.
I have created a route :
Route::get('/test', function()
{
return View::make('test');
});
a layout (testlayout.blade.php) :
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<div class="container">
#yield('container')
</div>
</body>
</html>
and a view (test.blade.php) :
#extends("testlayout")
#section('container')
<h1>Hello!</h1>
#stop
It works fine and I get "Hello!"
But when a change my layout by adding an include like this :
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<div class="container">
#yield('container')
</div>
#include('testfooter')
</body>
</html>
with my footer (testfooter.blade.php) :
#section("testfooter")
<div class="footer">2013-2014</div>
#show
I have an error "syntax error, unexpected '/'".
The code generated by Laravel (found in app\storage\views) is wrong :
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<div class="container">
<?php echo $__env->yieldContent('container')
</div>
<?php echo $__env->make('testfooter', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>; ?>
</body>
</html>
Laravel forgets ;?> after yieldContent('container') and adds two ;?> after ... render().
I've tried many things but I really don't have any clue now.
UPDATE1
I have reinstalled a fresh Laravel and guest what, the problem is still the same. Without #include it's ok, with an #include I have the same error again and again.
UPDATE2 and PROBLEM FIXED (thanks to #majimboo)!
This is just unbelievable. All that story is not related to the code. This is a problem with the text editor : Notepad++ on Windows. For an unknown reason, and for somes files only, it switched from 'edit/EOL conversion/windows format' to 'edit/EOL conversion/Mac format'. And apparently, Laravel doesn't Mac format at all! So, if you use Notepad++ on Windows, be sure that you have : 'EOL conversion/windows format'
the problem is in the file that is getting included.
First to check if the problem is really that, remove everything inside testfooter.blade.php then open the view from the browser. You'll notice that there is no error anymore.
Change your testfooter.blade.php to:
<div class="footer">2013-2014</div>
remove the #section("testfooter")...#show.
Example:
<!-- app/views/header.blade.php -->
<h1>When does the Narwhal bacon?</h1>
<!-- app/views/footer.blade.php -->
<small>Information provided based on research as of 3rd May '13.</small>
You include it like:
<!-- app/views/example.blade.php -->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Narwhals</title>
</head>
<body>
#include('header')
<p>Why, the Narhwal surely bacons at midnight, my good sir!</p>
#include('footer')
</body>
</html>
Check this to learn more on blade.
The problem seems to be something else, I would just like to share this, If it can help you:
I'm using Notepad++ as text-editor and for some strange reason it had
decided to use "MAC format" as the End-Of-Line (EOL) format.
Apparently the Blade framework can't cope with that. Use the
conversion function (in notepad++ : Edit -> EOL Conversion) to convert
to Windows Format and it will work just fine...
Arjen
You have this:
#section("testfooter")
<div class="footer">2013-2014</div>
#show // <--
Change it to this:
#section("testfooter")
<div class="footer">2013-2014</div>
#stop // <--
In my case the issue was an unterminated string in some PHP in a totally different part of the file than Laravel was complaining about. I eliminated it by removing everything in the file and adding the lines in again one by one.
Usually Laravel gives a great hint to the problem. The problem might be in your CSS or JS include statements. First make sure you have turn on the debug mode of laravel. Here is how to do it.
Config/app -> debug = true at line 16
Refresh the page where the error is
I have intentionally leave out closing ')' of css file. The laravel FW showed where that error, and it will show you similar error.
Hope it help.