Laravel forms - array of same field - php

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:

Related

Laravel can't access object in blade when variable in where/find

I try to ech the object of a laravel many-to-many relation in blade:
When I try to find the created_at with $product->id=47 and echo it out like so:
{{ $inquiry->products->find($product->id)->created_at }}
I get the error: "Trying to get property 'created_at' of non-object..."
When I remove the $product->id and change it to "47" everything works find:
{{ $inquiry->products->find(47)->created_at }}
But I need to query with the variable $product->id.
Any ideas what's wrong here?
Here is a longer part of my code:
#foreach ($upgrades as $upgrade)
{{ $upgrade->id }} //Echos exactly as 47
//List all upgrades
<select>
#if($inquiry->products->where('id', $upgrade->id)->count() == 1)
<option> {{ $inquiry->products->find($upgrade->id)->pivot->duration }}
#endif
</select>
#endforeach

Call to undefined method error when I try to use links()

When I try to use $student->links() I see this error :
Facade\Ignition\Exceptions\ViewException
Call to undefined method App\Student::links()
I checked the controller, model etc but all of them seem OK... How can I fix this?
(I tried this code both on my Macbook and VPS -CentOS7- but same problem occurs)
That part of my view looks like this:
</tr>
#endforeach
</tbody>
</table>
{{ $student->links() }}
</div>
#endsection
Change
{{ $student->links() }}
to
{{ $students->links() }}
(use plural form).
You need to paginate in your backend code. $students = App\Student::paginate(15);
And then you can access the links()
<div class="container">
#foreach ($students as $student)
{{ $student->name }}
#endforeach
</div>
{{ $students->links() }}

Best way to output array from controller to view in laravel 5

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.

Directing through form

Im a beginner with laravel. Im following the laracasts tutorial and Im stuck at the part where you access another page with a form by:
{{ Form::open(['url' => 'created']) }}
for example.
Now that leads me to the right url but it gives me
Whoops, looks like something went wrong.
As soon as I type the link manually it works normally.
This is the code of the page where it directs to:
controller:
public function created()
{
return 'hello';
}
Routes:
Route::get('created', 'TestController#created');
View:
#extends('layout')
#section('content')
<h1> Test </h1>
#stop
This is the form of the 1st page:
#extends('layout')
#section('content')
<h1>Create New User</h1>
{{ Form::open(['url' => 'created']) }}
<div>
{{ Form::label('email', 'E-mail:')}}
{{ Form::text('email')}}
</div>
<div>
{{ Form::label('password', 'Password:')}}
{{ Form::password('password')}}
</div>
<div>
{{ Form::submit('Create')}}
</div>
{{ Form::close()}}
#stop
What is going wrong here?
Form open by default links to a post method so what you need is either a post route or a get method. Following should work:
{{ Form::open(['url' => 'created']) }}
// Insert your fields/codes here
{{ Form::close() }}
//Change route method to post
Route::post('created', 'TestController#created');
Please read the documentation here for more details.

Commenting in Laravel blade error "Something went wrong"

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'.

Categories