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

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

Related

Redering HTML in blade template from shortcut IF statement

Okay so I have the following (multiple times) in my blade template:
{{ Auth::user()->settings['font'] == null ? "<span class='fa fa-check'></span>" : false }}
However it doesnt render the span statement, it simply prints it out. I've read elsewhere that to render HTML in a blade template I need to use {!! <span></span> !!} but how can I do that within a shortcut IF statement?
The {{ $thing }} syntax escapes the content passed to it via the e() helper. You can use the following alternative syntax: {!! $thing !!}
This syntax works the same as the first one so you can use your ternary condition too. The following will print nothing if the condition is not met.
{!! Auth::user()->settings['font'] == null ? "<span class='fa fa-check'></span>" : '' !!}
Alternatively with #if:
#if(Auth::user()->settings['font'] == null)
<span class='fa fa-check'></span>
#endif

Laravel nl2br not getting foreach parameters properly

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 ?

laravel 5 fill form select with data

I'm trying to build a form to assign a reward to an item (I call it a "ticket"). I want a dropdown list with all of the tickets so that the person can then choose.
This is my controller
$tickets = Ticket::all();
return view('rewards.create',compact('tickets'));
And in my blade.php view
<div class="form-group">
{!! Form::label('ticket','reward for: ') !!}
{!! Form::select('id', $tickets, Input::old('id')) !!}
</div>
This works, but it shows all the fields of the object. I want it show two fields. To store the 'id' in the vallue and 'description' in the written field of the select box, but doing something like
{!! Form::select('id', $tickets->description, Input::old('id')) !!}
brings up an error.
Can anyone please help?
The options have to be passed as array: ['value' => 'text']. You can use lists() to build that array for you:
$tickets = Ticket::lists('description', 'id');
return view('rewards.create',compact('tickets'));
In your blade.php
<div class="form-group">
{!! Form::label('ticket','reward for: ') !!}
{!! Form::select('id', $tickets->id, Input::old('id')) !!}
{!! Form::select('description', $tickets->description, Input::old('description')) !!}
</div>

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

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