I am trying to wrap my head around Laravel.
I am commenting code out for later use.
My problem occurs, when I have a snippet of code commented out, but the code is not "true".
Example for what works for me:
<head>
<!-- {{ HTML::script('js/scrollTo.js'); }} -->
{{ HTML::style('css/style.css'); }}
</head>
<head>
<!-- Foo {{ HTML::script('js/scrollTo.js'); }} Bar -->
{{ HTML::style('css/style.css'); }}
</head>
What doesn't work for me:
<head>
<!-- {{ } HTML::script('js/scrollTo.js'); }} -->
{{ HTML::style('css/style.css'); }}
</head>
Whenever I put something within the blade tags, the system makes an error.
Why I would even do this, i am not sure. But what I don't understand is why the system makes an error when the tag is commentet out.
Use Blade Comments:
<head>
{{-- {{ } HTML::script('js/scrollTo.js'); }} --}}
{{ HTML::style('css/style.css'); }}
</head>
The Blade tags still get rendered, even within an HTML comment (Blade is not a HTML parser, so it has no clue what <!-- means). Your resulting view code is going to be:
<!-- <?php } HTML::script('js/scrollTo.js'); ?> -->
which is a parse error. You can use Blade comments:
{{-- {{ } HTML::script('js/scrollTo.js'); }} --}}
but the real question is why not fix the slightly invalid code in the first place?
I don't know why, but it doesn't work because it is php. So whenever you "comment it out" it is still ran and will still return an error when the php is 'bad'. If you do want to comment them out then you can do it this way:
{{ dd($non_existent_value) }} <- causes error
<!-- {{ dd($non_existent_value) }} --> <- also causes error
{{ ''# dd($non_existent_value) }} <- echo's '', aka nothing
<!-- {{ ''# dd($non_existent_value) }} --> <- also echo's '', aka nothing
If you take a look at the code after you get errors on this you will see that with the first examples it tries to do <?php echo $non_existent_value ?>, with the latter it will do <?php echo '' # $non_existent_value ?> and thus echo '', or nothing.
Blade also provides a way to comment out the entire thing, just append '--' after the opening '{{' and before the closing '}}'. Like so:
{{-- dd($someVar) --}}
Both methods above are correct, although the first does not comment out the <?php echo ?> part but only what comes after 'echo'.
Related
I currently have this in my view to output data from an array that comes from my controller:
<!DOCTYPE html>
<html>
#foreach ($details as $detail)
<head>
<meta charset="utf-8">
<title>{{ $detail->name }}</title>
</head>
<body>
<h1>in view</h1>
{{ $detail->name }}
<br>
{{ $detail->street }}
<br>
{{ $detail->city }}, {{ $detail->state }}. {{ $detail->zip }}
</body>
#endforeach
</html>
This is the function in my controller:
$details = DB::table('restaurants')->where('id', $restaurant_id)->get();
return view ('restaurant.detail')->with('details', $details);
My question is: is there a better way to do this? I tried using the blade syntax without the #foreach and didn't have any luck.
I don't want to output this multiple times, the only reason I have the foreach there is because it is the only way I could get it to output.
If this is how it is supposed to work, no worries, I am just not familiar enough yet with blade to know if there is a better way to output this.
Thank you!
You seem to be selecting something by id, so that's probably unique. When you do ->get() by default it will return a collection of results because it assumes there's always a chance that there's more than 1. When selecting by ID however you know if something exists by that id, there's only going to be 1 of it. You can change the code to:
$detail = DB::table('restaurants')->where('id', $restaurant_id)->first();
return view ('restaurant.detail')->with('detail', $detail);
<html>
<head>
<meta charset="utf-8">
<title>{{ $detail->name }}</title>
</head>
<body>
<h1>in view</h1>
{{ $detail->name }}
<br>
{{ $detail->street }}
<br>
{{ $detail->city }}, {{ $detail->state }}. {{ $detail->zip }}
</body>
</html>
I highly recommend you look into Eloquent
Example of an eloquent model:
class Restaurant extends Model {} //The table name for model "Restaurant" is assumed to be restaurants
Then you can find a single restaurant:
$detail = Restaurant::find($restaurant_id);
Why do you want multiple title tags in your page ? It doesn't make any sense.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><!-- What do you want to show here ? --></title>
</head>
<body>
<h1>in view</h1>
#foreach ($details as $detail)
{{ $detail->name }}
<br>
{{ $detail->street }}
<br>
{{ $detail->city }}, {{ $detail->state }}. {{ $detail->zip}}
#endforeach
</body>
</html>
You can achieve this by below methods,
1. Use first() to retrieve single record like below,
// Controller Code
$detail = DB::table('restaurants')->where('id', $restaurant_id)->first();
<!-- View Code -->
<title>{{ $detail->name }}</title>
2. Use find() to retrieve single record by its primary key like below. It works similar to first() but it retrieves data by its primary key.
// Controller Code
$detail = DB::table('restaurants')->find($restaurant_id);
<!-- View Code -->
<title>{{ $detail->name }}</title>
3. You can use get() as you did in your question but you need to access it as array with 0th index. I assumed that your query will retrieve only one record. It won't fail even if it retrieves multiple records but you will only first record in your view.
// Controller Code
$details = DB::table('restaurants')->where('id', $restaurant_id)->get();
<!-- View Code -->
<title>{{ $detail[0]->name }}</title>
There could be some more approaches but I used only these approaches so far.
I'm having problems with Laravel since i removed Entrust.
Everything was working fine until i tried installing Entrust. I removed it because it kept saying the following message the same as this question Laravel cache store does not support tagging
Laravel cache store does not support tagging
I removed everything to do with Entrust and removed any changes I had to make but since then whenever I try to go to another page after logging in, it redirects back to the login page. I log back in and it redirects to the dashboard as it should but as soon as i go to another page, it redirects me back to login again.
Any ideas how to fix this?
UPDATE
I think i've narrowed down to the problem.
I created a new project and started again. I created the Auth, created a couple test controllers and views.
I logged in as normal and I got redirected to the Home contoller. I then went to another page and it loaded fine as it should. The views are using the default layout in views/layouts/app.blade.php. As soon as I change it to a custom layout the problem occurs again but is okay if i change it back to app.blade.php
Here is my new default.blade.php but i don't see why this doesn't work anymore
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>#yield('title')</title>
<link rel="icon" type="image/png" href="public/css/favicon-32x32.png" sizes="32x32">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css">
{{ HTML::style('css/bootstrap.css') }}
#if (!empty($styles))
#foreach($styles as $style)
{{ HTML::style($style . '.css') }}
#endforeach
#endif
{{ HTML::style('css/app.css') }}
{{ HTML::style('css/media.css') }}
</head>
<body class="{{ $body_class or '' }}">
<div class="wrapper">
#include('layouts.header')
#yield('content')
<div class="push"></div>
</div>
<div class="footer navbar-primary">
<div class="row">
<div class="col-xs-12">
<footer>
<p>© Copyright #php echo date('Y'); #endphp Axminster Tools & Machinery</p>
</footer>
</div>
</div>
</div>
<script>
window.website_url = '{{ URL::to('/') }}';
</script>
{{ HTML::script('assets/src/jquery/dist/jquery.min.js') }}
{{ HTML::script('assets/src/jquery-ui/jquery-ui.min.js') }}
{{ HTML::script('js/bootstrap.min.js') }}
{{ HTML::script('js/validate.js') }}
#if(isset($jsVars))
<script>
#foreach($jsVars as $key => $val)
var {{ $key }} = {{ $val }};
#endforeach
</script>
#endif
<script>
$(function() {
$("#searchform").submit(function(e) {
$("#searchform").attr("action", "/search/term/" + encodeURI($("#keywords").val()));
});
});
</script>
#if (!empty($scripts))
#foreach($scripts as $script)
{{ HTML::script($script . '.js') }}
#endforeach
#endif
</body>
</html>
I eventually found the problem.
After doing a new installation i tested the Auth and it worked. As soon as I copied across some of my template files I found the problem occurred again. After looking at the template layout files, I found I was using this for the logout link
Change password
I did this within the first few days of learning Laravel and it was never a problem before...don't know why.
I changed it to this and it all works as expected
Logout
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
I'm trying to replicate this logic:
insert multiple fields using foreach loop
- using laravel 4 and blade.
I tried this:
<div>
{{ Form::select('linkType[]', array('Facebook', 'Twitter','Other')) }}
{{ Form::text('linkUrl[]') }}<br>
{{ Form::select('linkType[]', array('Facebook', 'Twitter','Other')) }}
{{ Form::text('linkUrl[]') }}
...
</div>
But that gives me this laravel error:
ErrorException (E_UNKNOWN)
htmlentities() expects parameter 1 to be string, array given (view:...)
Any idea how to fix that?
Thanks
Update
A few of these works: {{ Form::text('linkurl[]') }} So that is written as it should.
A few of this also works:
<select name="linktype[]"/>
<option value="facebook">Facebook</option>
<option value="twitter">Twitter</option>
</select>
So the problem is 99% sure in the:
{{ Form::select('linkType[]', array('Facebook', 'Twitter','Other')) }}
What I'm I doing wrong?
Works for me...
<!doctype html>
<html lang="en">
<body>
<div>
{{ Form::select('linkType[]', ['Facebook', 'Twitter','Other']) }}
</div>
</body>
</html>
generates:
I have a "master" layout that has a section: #yield('other-scripts')
I use this master template in another view (especie.blade.php):
#extends('layouts.master')
#include('layouts.editarEspecie')
#section('other-scripts')
{{ HTML::script('js/lightbox.js') }}
#stop
Inside layouts.editarEspecie, #section('other-scripts') is also overwritten:
#section('other-scripts')
{{ HTML::script('js/chosen.jquery.js') }}
{{ HTML::script('js/chosenScriptModal.js') }}
{{ HTML::script('js/scripts.js') }}
#stop
The problem is that #include('layouts.editarEspecie') goes first, so #section('other-scripts') never adds the part inside especie.blade.php.
What can I do so that both part are added and it ends like this?
{{ HTML::script('js/chosen.jquery.js') }}
{{ HTML::script('js/chosenScriptModal.js') }}
{{ HTML::script('js/scripts.js') }}
{{ HTML::script('js/lightbox.js') }}
P.S.: I do this because lightbox.js is always necessary, but chosen.jquery.js, chosenScriptModal.js, scripts.js may not always be needed.
Inside layouts.editarEspecie dont have a "section" - just have this:
{{ HTML::script('js/chosen.jquery.js') }}
{{ HTML::script('js/chosenScriptModal.js') }}
{{ HTML::script('js/scripts.js') }}
Then those 3 lines will always be included.
The thing is, I don´t want those 3 lines to always go inside
layouts.editarEspecie, just when certain conditions are met (not shown
here)
ummmmm.. i know its like the fastest thing ever but you can always PHP if it
<?php
if (condition)
{
?>
{{ script here}}
<?php
}
else
{}
?>
Sorry for lazy explanation but i am sure you get the hint
I'm trying to carry out a simple acceptance test for learning purpose.
It's a simple authentication scenario : user enters /admin, If not logged it, He gets redirected to /login to fill the form.
When i run the test i get this error :
1) Couldn't login with a password protected area in LoginCest.loginUserWithProperCredentials
Guy couldn't fill field "username","rafael": Field matching id|name|label|value or css or xpath selector does not exist
Scenario Steps:
5. I fill field "username","rafael" <==== RED
4. I see current url equals "/login"
3. I am on page "/admin"
2. So that I Perform administrative tasks
1. As a Site Owner
Now here's my view :
//create.blade.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
{{ Form::open() }}
<div>
{{ Form::Label('username', 'Username') }}
{{ Form::Text('username', '') }}
</div>
<div>
{{ Form::label('password', 'Password') }}
{{ Form::password('password', '') }}
</div>
<div>
{{ Form::submit('Login') }}
</div>
{{ Form::close() }}
</body>
</html>
And here's the test :
class LoginCest
{
public function loginUserWithProperCredentials(WebGuy $I){
$I->am("Site Owner");
$I->wantTo("Login with a password protected area");
$I->lookForwardTo("Perform administrative tasks");
$I->amOnPage('/admin');
$I->seeCurrentUrlEquals('/login');
$I->fillField('username', 'rafael');
$I->fillField("password", "123456");
$I->click("Login");
$I->seeCurrentUrlEquals("/admin");
$I->see("Admin area", "h1");
}
}
Any help would be appreciated.
You may copy XPath of username from html, and write:
$I->fillField('//*[#id="addPosDialog"]/div/button','Username');
'//*[#id="addPosDialog"]/div/button' - paste your xpath there.
You may use xpath, name, id and other locators.
When I have problems with fillField i am usually doing something like this.
HTML code in this case is more important than view code.
If my advice will not solve the problem, need to see HTML code.
I had the issue where the html syntax was not correct. Then the phpbrowser works not reliable. Even so it does not look like it in your example.