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.
Related
guest.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="author" content="Yucel Yilmaz">
<meta name="robots" content="index,follow">
<meta name="publisher" content="YĆ¼cel Yilmaz">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>#yield('title') | ecommerece</title>
</head>
<body>
</body>
</html>
cities-page-component.blade.php
<div>
#section('title', 'Laundry Near Me')
</div>
listing-page-component.blade.php
<div>
#section('title', 'Laundry Near Me')
</div>
how do I make Title dynamic in the product detail page from the data from the backend in laravel
like an I have a list of cities, under the cities, there is a bunch of laundries.
On the main page, we can search for cities so the list of cities is shown from the database. when the user clicks any of the cities the page will be directed to another page with a list of laundries so I want the title to be laundries in {{ city name }}
same way when the user clicks on any laundry the page is directed detail page of laundries so the title I want to be {{ laundry name }} in {{ city name }}
You can set #yield for title via template inheritance.
Layout
<html>
<head>
<title>#yield('title')</title>
</head>
<body>
...
Set $dynamicTitle in view :
#extends('layouts.app')
#section('title', $dynamicTitle)
#section('content')
...
#endsection
You are free to assign a value to the #section('title') using $dynamicTitle or $item->title for example. Depends on your needs.
You can store the City Title as session variable in cities selection controller when returning city record with
session()->put('cityName', city->city_title);
and use it in your laundries view as
<h4>City Name : {{ Session('cityName') }} </h4>
likewise save laundry Title when returning laundry record as
session()->put('laundryName', laundry->laundry_title);
and use it in your detail view like
<h4>City Name : {{ Session('cityName') }} Laundry : {{ Session('laundryName') }} </h4>
I have master layout of laravel blade template main.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>#yield('title','My Site')</title>
</head>
<body>
#yield('container','Content area')
{{ HTML::script('assets/js/jquery-1.9.0.js') }}
</body>
</html>
I extended it for another view index.blade.php
#extend('main')
I want to add javascript links to master layout(main.blade.php) if exists in child layout(index.blade.php)
added code in index.blade.php
$extraJs = array('plugins1.js','plugins2.js');
#section('extraJs')
{{ $extraJs }}
#stop
So, How can I add javascript to master layout if exists in child blade?
In php:
<?php
if(isset($extraJs)):
foreach($extraJs as $js):
?>
<script src="assets/js/<?php echo $js ?>" type="text/javascript" />
<?php endforeach; endif; ?>
You can just add a yield in your main.blade.php, like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>#yield('title','My Site')</title>
</head>
<body>
#yield('container','Content area')
{{ HTML::script('assets/js/jquery-1.9.0.js') }}
#yield('javascripts')
</body>
</html>
Then, in your child layout, you add a section for it, like you did. For example:
#section('javascripts')
#foreach($scripts as $script)
{{ HTML::script('ets/js/' . $script) }}
#endforeach
#stop
If you add another child view which doesn't need to use extra javascripts, Laravel won't complain because the section is optional.
I wouldn't really encourage this method but since you asked. It is definitely possible to pass something "up" in the view tree. You can just define a PHP variable in your view and it will be available anywhere in the layout.
index.blade.php
<?php $extraJs = array('plugins1.js','plugins2.js'); ?>
main.blade.php
#if(isset($extraJs))
#foreach($extraJs as $js)
<script src="{{ asset('assets/js/'.$js) }}" type="text/javascript" />
#endforeach
#endif
As you can see I also converted your code in the layout to blade and made use of the asset() function to generate absolute URLs
Note Instead of doing it this way there are other methods. For example View composers or defining the plugins in the controller.
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'.
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'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.