Laravel nl2br not getting foreach parameters properly - php

So i am currently working with nl2br. I don't know whether this is a bug, or a wrong way to doing things from my side but i encounter this. First of all, here's the snippet :
#foreach($dataList as $data)
<div class="productDesc">
{!! nl2br(e($data->product->description)) !!}
</div>
#endforeach
When i run this, the output is only the description of the first product. I initially thought there's something wrong with my code but when i do a debug like this :
#foreach($dataList as $data)
{{ $data->id }}
<div class="productDesc">
{{ $data->id }}
{!! nl2br(e($data->product->description)) !!}
</div>
{{ $data->id }}
#endforeach
The result in the 2nd row is :
2
1 This is a description for 1st product
2
As you can see, it somehow ignore the forwhile loop parameter. Can someone inform whether there's a solution for this or am i just code it wrongly ?

Related

Pagination Link

What is the correct way to call the "Links" function after this "Foreach"?
I don't know how to handle the variable to put in function.
#inject('usuarios', 'App\User')
#foreach($usuarios->getIndicados() as $user)
#endforeach
<div class="row">
<div class="col-12 text-center">
{{ $usuarios->getIndicados()->links() }}
</div>
</div>
Maybe it's just an editing error, but in your output the -tags don't seem to be closed again. Also, there should be no space like < a> at the beginning of the tag. And < a hr_ef= ... is obviously wrong.
In order to style them, you can add a class attribute to the tags while building the string and do the style-stuff in css.
This is what laravel document provides. You need to add links in the collection.
<div class="container">
#foreach ($users as $user)
{{ $user->name }}
#endforeach
{{ $users->links() }}

In Laravel, how to display php value in name attribute?

I am having following html code in blade file:
#foreach ($engagements as $engagement)
{!! Form::checkbox('engagements[]', $engagement->id, in_array($engagement->id, $user->privileges->pluck(engagement_id)->toArray()) ? true : false) !!} {{ $engagement->name }}
{!! Form::select('roles[]', $userRoles, $user->privileges->where(engagement_id, $engagement->id)->first()[role]) !!}
#endforeach`
I want to pass $engagement->id in roles[] as roles[{{#engegement_id}}]
However, instead of displaying value of {{#engegement_id}}, it is showing it as roles [{{#engegement_id}}] in html view source.
What am I doing wrong here?
Try with this,
#foreach ($engagements as $engagement)
#php echo $roleWithId = roles[$engagement->id] #endphp
{!! Form::checkbox('engagements[]', $engagement->id, in_array($engagement->id, $user->privileges->pluck(engagement_id)->toArray()) ? true : false) !!} {{ $engagement->name }}
{!! Form::select($roleWithId , $userRoles, $user->privileges->where(engagement_id, $engagement->id)->first()[role]) !!}
#endforeach
Make string which you want to pass and store it in variable and pass that variable in Form select.
Hope this helps :)
We can not do like this, it is giving as error saying 'Undefined variable roles' for #php echo $roleWithId = roles[$engagement->id] #endphp. However, I tried it in select itself as
{!! Form::select('roles[#php echo $engagement->id #endphp]', $userRoles, $user->privileges->where(\App\Privilege::COLUMN_ENGAGEMENT_ID, $engagement-> {\App\Engagement::COLUMN_ID})->first()[\App\Privilege::COLUMN_ROLE]) !!}
However, it is showing it as
<select name="roles[<?php echo $engagement->id ?>]"> in view source

Update Database with Laravel

I am trying to update information in my database with Laravel. Not sure what I am doing wrong but I can't seem to find where the problem is. Here is the code for my edit page (This is the page where I would edit information taken from my DB).
{{ Form::open(['url'=>'portfolio/update']) }}
<div>
{{ Form::label('portfolio_title', 'Portfolio Title:') }}
{{ Form::text('portfolio_title',$work->portfolio_title) }}
{{ $errors->first('portfolio_title','<span class="error">:message</span>') }}
</div>
<div>
{{ Form::label('portfolio_description', 'Portfolio Description') }}<br>
{{ Form::textarea('portfolio_description', $work->portfolio_description, ['size' => '50x5']) }}
{{ $errors->first('portfolio_description','<span class="error">:message</span>') }}
</div>
<div>
{{ Form::label('portfolio_content', 'Portfolio Content') }}<br>
{{ Form::textarea('portfolio_content', $work->portfolio_content, ['size' => '50x5']) }}
{{ $errors->first('portfolio_content','<span class="error">:message</span>') }}
</div>
{{ Form::hidden('id',$work->id) }}
<div>
{{ Form::submit('Update Work') }}
</div>
{{ Form::close() }}
I have a controller called PortfolioController that will save info to database and what not.
public function edit($work_title){
$work = Portfolio::wherePortfolio_title($work_title)->first();
return View::make('portfolio/edit', ['work' => $work]);
}
public function update(){
$id = Input::get('id');
$input = Input::except('id');
if( !$this->portfolio->fill($input)->isValid()){
return Redirect::back()->withInput()->withErrors($this->portfolio->errors);
}
$work = Portfolio::find($id);
$work->portfolio_title = Input::get('id');
$work->save();
}
Here is my route that I am working with:
Route::resource('portfolio','PortfolioController');
Route::post('portfolio/update','PortfolioController#update');
I am able to get the form populated with the correct information but when i change something like the title and click update, the page reloads but does not save in the DB. Sometimes I will get an MethodNotAllowedHttpException error. This has been pretty frustrating for me so any help will be greatly appreciated.
Why don't you just actually use your resource route?
First, remove the portfolio/update route. You don't need it.
Then change your Form::open to this:
{{ Form::open(['route' => ['portfolio.update', $work->portfolio_title], 'method' => 'put']) }}
This way you target the update method in your RESTful controller.
Finally change that to use the portfolio_title as identifier and you should be able to remove the hidden id field from your form.
public function update($work_title){}
Please take a look at:
RESTful controllers
Opening a form

Blade templating (Laravel) overwrite issues

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

How to tackle variables that may not be set when using blade template engine to build forms

Ok, so a long title, but apologies, he only way to describe it without getting the wrong kind of answer.
So, the scenario....
I am creating a site which has a search form of sorts in the header, and therefore on every page. I would like it to retain its previous variables when being used for user convenience, for my convenience I have built the form into the default layout, to save recreating many instances of it.
default.blade.php (Heres the form, with unnecessary markup removed)
{{ Form::open(array('url' => '/search')) }}
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
{{ Form::submit('Go') }}
{{ Form::close() }}
The $model & $colour are variables I am capturing during the post. The problem is, I am getting unset variable errors from Blade on any pages where the user hasn't posted to, so I am literally having to preset them in almost every route or controller across my entire site, just to prevent the errors.
In essence, the system works fine as long as the user is posting, if someone visits the site using a direct link its basically useless.
Obviously I can not have the search form be set to the previously searched results, but that would be bad practice from a usability point of view.
Am I missing something here, surely there has to be a simple solution to this. Thanks for any help.
Create a view composer for your highly used variables:
View::composer(['store.index', 'products.*'], function($view)
{
$model = Input::get('model') ?: 'modelX';
$colour = Input::get('colour') ?: 'colourY';
$view->with('model', $model);
$view->with('colour', $colour);
});
Laravel will send those variables to your views automatically, every time someone hit one of them.
You can put that in your routes file, filters file or, like, me, create a app/composers.php and load by adding
require app_path().'/composers.php';
To your app/start/global.php.
You can check whether variables are set or not with PHP isset():
{{ Form::open(array('url' => '/search')) }}
#if (isset($model) && isset($colour))
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
#else
{{ Form::select('model', Photo::getAvailableModels(true)) }}
{{ Form::select('colour', Photo::getAvailableColours(true)) }}
#endif
{{ Form::submit('Go') }}
{{ Form::close() }}
Or, if your form fields are optional and you need to check separately:
{{ Form::open(array('url' => '/search')) }}
#if (isset($model))
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
#else
{{ Form::select('model', Photo::getAvailableModels(true)) }}
#endif
#if (isset($colour))
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
#else
{{ Form::select('colour', Photo::getAvailableColours(true)) }}
#endif
{{ Form::submit('Go') }}
{{ Form::close() }}

Categories