I have nested foreach in my blade in laravel but all records compact twice in my select option here is my code:
#foreach($user as $user)
#foreach($user_renter as $last_user_renter)
#if($user->id == $last_user_renter->user_id)
<option value="{{ $user->id }}" selected>
{{ $user->name }} {{ $user->family }} - {{ $user->email }}
</option>
#else
<option value="{{ $user->id }}">
{{ $user->name }} {{ $user->family }} - {{ $user->email }}
</option>
#endif
#endforeach
#endforeach
now is there any way to compact them once?
Try to change the user_renter as array as given bellow
$user_renter = array(
array(
'name' => 'flash',
'id' => 1
),
array(
'name' => 'zoom',
'id' => 2
),
array(
'name' => 'snart',
'id' => 3
)
);
And
#foreach($users as $user)
<?php $key = array_search($user->id, array_column($user_renter, 'id')); ?>
<option value="{{ $user->id }}" #if(is_int($key)) selected #endif>
{{ $user->name }} {{ $user->family }} - {{ $user->email }}
</option>
#endforeach
Related
I have a quiz app and when I press add question button the error Undefined variable: question $question is undefined. Make the variable optional in the blade template. Replace {{ $question }} with {{ $question ?? '' }} occurs. The error appear for line 13 and 17:
<div class="form-group">
{!! Form::label('title', 'Title'); !!}
{{ Form::text('title', null, ['class' => 'form-control input-editor', 'placeholder' => 'Title']) }}
</div>
<div class="form-group">
{!! Form::label('category_id', 'Category'); !!}
<select class="form-control" id="category_id" name="category_id">
<option selected="selected" value="">---Pick a Category---</option>
#foreach($categories as $key => $cat)
#if(!empty($child_categories[$key]))
<optgroup label="{{ $cat }}">
#foreach($child_categories[$key] as $ckey => $ccat)
<option value="{{ $ckey }}" {{ $question->category_id == $ckey ? 'selected' : '' }}>{{ $ccat }}</option>
#endforeach
</optgroup>
#else
<option value="{{ $key }}" {{ $question->category_id == $key ? 'selected' : '' }}>{{ $cat }}</option>
#endif
#endforeach
</select>
</div>
<div class="form-group">
{!! Form::label('question_type', 'Question Type'); !!}
{!! Form::select('question_type', ['regular' => 'Regular Question', 'photo' => 'Photo Question'], null, ['id' => 'question_type', 'class' => 'form-control']); !!}
</div>
#if($btntitle == 'Update Question')
<div class="alert alert-success question_image" style="{{ isset($question) && $question->question_type == 'photo' ? 'display: block;' : 'display: none;' }}">
Upload image if you want to ovewrite existing one(if any), otherwise leave that field blank
</div>
#endif
I would like to integrate a list of icons in a drop-down list of a back office (laravel nova), I found something to help me on this link https://novapackages.com/packages/bernhardh/nova-icon-select
I followed all the procedure, but I block at the level of the display of my icons, I currently have this code in my models:
public static function getIconsOptions(): array
{
NovaIconSelect::make("Icon")
->setIconProvider(IconProvider::make()->setOptions([
[
'label' => 'aaaa',
'value' => '<i class="fa-brands fa-apple-pay"></i>',
],
[
'label' => 'aaaa',
'value' => 'my-icons-2',
],
[
'label' => 'aaaa',
'value' => 'my-icons-3',
],
]));
$options = [];
foreach(self::ICON_NAMES as $key => $icon)
$options[$key] = trans('appearance.'.$icon);
return $options;
}
and this code in my blade
<select class="js-visual-field w-full form-control form-input form-input-bordered" data-field-name="icon_select_{{ $i }}">
<option value="">{{ trans('nova-visual-composer::templates.'.$templateName.'.no_icon') }}</option>
#foreach(\App\Models\Appearance::getIconsOptions() as $key => $txt)
<option value="{{ $key }}">{{ $txt }}</option>
#endforeach
</select>
To render HTML in blade, you need to use {!! $txt !!} instead of {{ $txt }}. Otherwise, the string will be auto escaped. So:
<select class="js-visual-field w-full form-control form-input form-input-bordered" data-field-name="icon_select_{{ $i }}">
<option value="">{{ trans('nova-visual-composer::templates.'.$templateName.'.no_icon') }}</option>
#foreach(\App\Models\Appearance::getIconsOptions() as $key => $txt)
<option value="{{ $key }}">{!! $txt !!}</option>
#endforeach
</select>
I try to get children's in their parents view with ability of editing them,
Codes
controller
public function edit($id)
{
$option = Option::findOrFail($id);
return view('admin.options.edit', compact('option'));
}
blade
// getting parent info in edit blade
{{ Form::model($option, array('route' => array('options.update', $option->id), 'method' => 'PUT')) }}
{{ Form::label('title', 'Name') }}
{{ Form::text('title', null, array('class' => 'form-control')) }}
{{ Form::submit('Update', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
// Geting this option subs (children's) in my second tab
#foreach($option->suboptions as $sub)
{{ Form::model($sub, array('route' => array('suboptions.update', $sub->id), 'method' => 'PUT')) }}
{{ Form::label('title', 'Name') }}
{{ Form::text('title', null, array('class' => 'form-control')) }}
{{ Form::label('price', 'Price') }}
{{ Form::text('price', null, array('class' => 'form-control')) }}
{{ Form::submit('Update', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
#endforeach
Issue
As my sub's form is inside the loop I get update button for each row
(children) how can I only have 1 button?
UPDATE
check this video to see what I mean.
UPDATE 2
More explain!
my page is include 2 different forms (one of them edits my option
eg. size that's fine we no have problem with that)
my second form which is my issue will return subs of my option, eg.
option = size, subs = 12 inch, 15 inch etc.) in order to edit this subs currently i have 1 button for each of them (as you see in video i shared), what i want is to have only 1 button for all of my second form.
You can just open and close the form outside of the loop:
// Geting this option subs (children's) in my second tab
{{ Form::open(array('route' => ['suboptions.updateMany'], 'method' => 'PUT')) }}
#foreach($option->suboptions as $sub)
{{ Form::label('title', 'Name') }}
{{ Form::text('title[]', null, ['class' => 'form-control']) }}
{{ Form::label('price', 'Price') }}
{{ Form::text('price[]', null, ['class' => 'form-control']) }}
#endforeach
{{ Form::submit('Update', ['class' => 'btn btn-success']) }}
{{ Form::close() }}
You would then implement the updateMany controller method to loop over the submitted data (validate it) and submit it.
SOLVED
I've made controller below and I can update my data as I wanted:
public function subsupdate(Request $req)
{
for ($i=0; $i<count($req->id); $i++) {
DB::table('suboptions')
->where('id',$req->id[$i])
->update([
'title' => $req->title[$i],
'price' => $req->price[$i],
]);
}
return redirect()->back()->with('success', 'Suboptions updated successfully.');
}
Terms table:
term_id
name
slug
Term_taxonomy table:
term_taxonomy_id
term_id
description
My Term model:
public function TermTaxonomy(){
return $this->hasOne('TermTaxonomy');
}
My TermTaxonomy model:
public function Term(){
return $this->belongsTo('Term');
}
my Controller
public function edit($id)
{
$categories = Term::with(['TermTaxonomy' => function($q){
$q->select('term_id', 'description');
}])->get(['term_id', 'name', 'slug']);
$category = Term::with(['TermTaxonomy' => function($q){
$q->select('term_id', 'description');
}])->find($id, ['term_id', 'name', 'slug']);
return View::make('qhymchildz.backend.posts.categories', compact('category', 'categories'));
}
My View
#if (isset($category))
{{ Form::model($category, ['route' => ['admin_posts_categories_update', $category->term_id], 'method' => 'PATCH']) }}
#else
{{ Form::open(['route' => 'admin_posts_categories_store'])}}
#endif
{{ Form::label('name', 'Name') }}
<span data-tooltip aria-haspopup="true" class="has-tip radius" title="Category name for your posts.">
{{ Form::text('name', '', ['placeholder' => 'Category name here']) }}
#if ($errors->has('name')) <small class="error"> {{ $errors->first('name') }} </small> #endif
</span>
{{ Form::label('slug', 'Slug') }}
<span data-tooltip aria-haspopup="true" class="has-tip radius" title="Slug or URL for your category.">
{{ Form::text('slug', '', ['placeholder' => 'Slug here']) }}
#if ($errors->has('slug')) <small class="error"> {{ $errors->first('slug') }} </small> #endif
</span>
{{ Form::label('description', 'Description') }}
{{ Form::textarea('description', '', ['placeholder' => 'Category description here', 'size' => '50x5']) }}
#if (!isset($category))
{{ Form::submit('Add New Category', ['class' => 'radius button']) }}
#else
{{ Form::submit('Update', ['class' => 'radius button']) }}
#endif
{{ Form::close() }}
and all input text is not populated, already try many way from googling but nothing work, then how to populate data in my input text and text area ? i use it for edit function.
Thanks, i am new in laravel. any help will be appreaciated.
First, the second parameter has to be null so it will actually use the value from your model:
{{ Form::text('name', null, ['placeholder' => 'Category name here']) }}
To use a property from a related model you can use this:
{{ Form::textarea('TermTaxonomy[description]', null, ['placeholder' => 'Category description here', 'size' => '50x5']) }}
Note that there's no need to do an #if(isset($category)) at the beginning. The Form::model method will handle non-existent models on it's own. This is sufficient:
{{ Form::model($category, ['route' => ['admin_posts_categories_update', $category->term_id], 'method' => 'PATCH']) }}
In my case was not rendering the model value on the Textarea.
So instead of null i give the value like this:
{{ Form::textarea('TermTaxonomy[description]', $category->TermTaxonomy['description'], ['placeholder' => 'Category description here', 'size' => '50x5']) }}
Hope this also helps someone.
I also had the same problem, but I did it like this:
{!! Form::text('firstname', $user->profile->firstname, ['class' => 'form-control']) !!}
In my case:
User belongsTo('App\Profile'),
Profile hasOne('App\profile')
Just get an idea. Hope it helps!
this below code is my profile managment form for users and i want to fill input tags with $profile variable .
{{ $profile }} can echo all fields of user table. but i can not fill input tags with this variable.
Result: {"id":"1","username":"mahdi","name":"Mahdi","family":"Pishguy","email":"name#gmail.com"}
My Form:
{{ Form::open(array('route' => 'admin.profile.update', $profile->id , 'method' => 'PUT','class'=>'navbar-form navbar-right', 'role' =>'search')) }}
<div class="form-group rtl">
<div>Your Username: <b> {{ $profile->username }} </b></div><br />
{{ Form::label('name' , 'name: ') }}
{{ Form::text('name', Input::old('name'), array('id'=>'email', 'class' => 'form-control')) }}
{{ Form::label('family' , 'family: ') }}
{{ Form::text('family', Input::old('family'), array('placeholder'=>'sss', 'id'=>'email', 'class' => 'form-control')) }}
<p>
{{ Form::submit('Submit', array('id'=>'submit','class'=>'btn btn-default' , 'style'=>'float:left')) }}
</p>
</div>
{{ Form::close() }}
Thats easy, see the Form Model Binding in the docs
You code should be something like this:
{{ Form::model($profile, array('route' => 'admin.profile.update', $profile->id , 'method' => 'PUT','class'=>'navbar-form navbar-right', 'role' =>'search')) }}
<div class="form-group rtl">
<div>Your Username: <b> {{ $profile->username }} </b></div><br />
{{ Form::label('name' , 'name: ') }}
{{ Form::text('name', Input::old('name'), array('id'=>'email', 'class' => 'form-control')) }}
{{ Form::label('family' , 'family: ') }}
{{ Form::text('family', Input::old('family'), array('placeholder'=>'sss', 'id'=>'email', 'class' => 'form-control')) }}
<p>
{{ Form::submit('Submit', array('id'=>'submit','class'=>'btn btn-default' , 'style'=>'float:left')) }}
</p>
</div>
{{ Form::close() }}
NOTE: You can remove the Input::old() stuf because this is handled by the Form::model()