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>
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 want to select multiple categories in one input field, I have a code, but it is not working:
PostController:
foreach($request->category_id as $key => $value) {
Project::create([
'name' => $request->name,
'body' => $request->body,
'category_id' => $request->category_id[$key],
]);
}
Form:
<select name="category_id[]" id="category_id" class="selectpicker" data-style="btn btn-success btn-round" data-live-search="true" data-size="5" multiple>
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
If you want to have one Post in multiple categories, you need to change your Database also.
You cant have only one category_id in posts table.
You have to adapt Many To Many relationship (One Post can be in multiple categories, one Category can have multiple Posts).
More here: Laravel Docs
Than you can save it simple with one line (based on your code):
$post->categories()->sync($request->category_id);
Use this code in PostController
foreach($request->category_id as $value) {
Project::create([
'name' => $request->name,
'body' => $request->body,
'category_id' => $value,
]);
}
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
I have drop-down where i show my products attributes, and no matter if i select any option or not it will add all the options to my cart under selected product.
what i need is to just get selected options or leave it empty if nothing has selected.
here is my blade code:
<tbody>
#foreach($options as $optiontitle => $optioncollection)
<tr>
<td style="width: 150px;">{{ $optiontitle }}</td>
<td class="text-left">
<select name="attr" class="form-control">
<option value="">Select</option>
#foreach($optioncollection as $suboption)
<option value="{{$suboption->id}}">{{$suboption->title}} - {{ number_format($suboption->price, 0) }}</option>
#endforeach
</select>
</td>
</tr>
#endforeach
</tbody>
here is my controller method for attributes which i call them option
$options = $product->suboptions->mapToGroups(function ($item, $key) {
return [$item->option->title => $item];
});
and this is my cart method where thing happen and everything will be add to cart instead of selected only.
public function addingItem(Request $request, $id)
{
$product = Product::where('id', $id)->firstOrFail();
$customAttributes = [];
if(!empty($product->suboptions)){
foreach($product->suboptions as $subs) {
array_push($customAttributes, [
'attr' => [
'label' => $subs->title,
'price' => $subs->price,
]
]);
}
}
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price,
'quantity' => $request->input('quantity'),
'attributes' => $customAttributes,
));
Session::flash('success', 'This product added to your cart successfully.');
return redirect()->back();
}
UPDATE
What I need to get is options title and price (take a look at my order sample to see why and where I need those)
"[{\"id\":29,\"name\":\"effewf\",\"price\":24524,\"quantity\":1,\"attributes\":[{\"attr\":{\"label\":\"Gray\",\"price\":\"7000.00\"}},{\"attr\":{\"label\":\"Red\",\"price\":\"5000.00\"}},{\"attr\":{\"label\":\"15\\\"\",\"price\":\"500000.00\"}},{\"attr\":{\"label\":\"17\\\"\",\"price\":\"700000.00\"}},{\"attr\":{\"label\":\"22\\\"\",\"price\":\"900000.00\"}}],\"conditions\":[]}]"
As you see in my attributes part I have nested array such as :
\"attributes\":[{\"attr\":{\"label\":\"Gray\",\"price\":\"7000.00\"}},....
here is where I get that label and price in my function loop.
If I use loop such as:
foreach($request->attr as $subs) {
array_push($customAttributes, [
'attr' => [
'label' => $subs->title,
'price' => $subs->price,
]
]);
Instead of what I have I will get this error:
Trying to get property of non-object
on this line:
'attr' => [
'label' => $subs->title,
'price' => $subs->price,
PS:
If I use $customAttributes = $request->attr; with my own loop (at the top) I will get id of selected option only, which is no good to me.
Because you called your product's suboptions relationship instead of getting it from the request.
You are calling it with this $product->suboptions instead of getting it from the request.
$customAttributes = $request->attr; // try to dd($customAttributes) so you can see it
UPDATE:
You only need to pass the id to your options
<select name="attr[]" class="form-control" multiple>
<option value="">Select</option>
#foreach($optioncollection as $suboption)
<option value="{{$suboption->id}}">{{$suboption->title}} - {{ number_format($suboption->price, 0) }}</option>
#endforeach
</select>
and in your Controller, fetch them and validate
public function addingItem(Request $request, $id)
{
$product = Product::findOrFail($id);
$customAttributes = [];
if (!empty($request->attr)) {
foreach ($request->attr as $sub) {
// You should fetch the price from the database NOT from the user
// request as it will be very vulnerable to attacks
// find the suboption
$sub = Suboption::find($sub); // Here I assume you have the Model
// for your Product's suboptions
if (!empty($sub->id)) {
array_push($customAttributes, [
'attr' => [
'label' => $sub->title,
'price' => $sub->price,
]
]);
}
}
}
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price,
'quantity' => $request->input('quantity'),
'attributes' => $customAttributes,
));
Session::flash('success', 'This product added to your cart successfully.');
return redirect()->back();
}
Shouldn't you get the options from an order or The Request and not from the Prodect ?!
Because the product should obviously have all options.
...........
-Edit:1
Change your loop like this
foreach($request->attr as $subs)
and change your selct tag to look something like this
<select multiple>
-Edit2
After editing your question, I think you have two options:
use the id from each $subs to query your suboptions again from the DB
OR
Serialize the title and price in the front end html option tag so that you will end up with something like this:
<option value="{{$suboption->title.','.$suboption->price}}" .....> .....</option>
then inside your loop do like this:
foreach($request->attr as $subs) {
$title_and_price = explode(',', $subs);
array_push($customAttributes, [
'attr' => [
'label' => $title_and_price[0],
'price' => $title_and_price[1]
]
]);
-Edit:3
We should also add square brackets to the name in the select tag like this:
<select name="{{ $optiontitle }}[]" multiple>
Edit:4 I found another problem:
you should use name = "{{ $optiontitle }}[]" in the select because they canot all have the same name thus, you have to make the name attribute of the select tag dynamic as well
Try replacing Controller Function addingItem with below
public function addingItem(Request $request, $id)
{
$product = Product::where('id', $id)->firstOrFail();
$customAttributes = [];
if(!empty($product->suboptions)){
foreach($product->suboptions as $subs) {
array_push($customAttributes, [
'attr' => [
'label' => $subs->title,
'price' => $subs->price,
]
]);
}
}
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price,
'quantity' => $request->input('quantity'),
'attributes' => $request->input('attr'), // Attributes Array
));
Session::flash('success', 'This product added to your cart successfully.');
return redirect()->back();
}
I assume you need to insert multiple attributes by
#foreach($options as $optiontitle => $optioncollection)
replace the blade code,
<tbody>
#foreach($options as $optiontitle => $optioncollection)
<tr>
<td style="width: 150px;">{{ $optiontitle }}</td>
<td class="text-left">
<select name="attr" class="form-control">
<option value="">Select</option>
#foreach($optioncollection as $suboption)
<option value="{{$suboption->id}}">{{$suboption->title}} - {{ number_format($suboption->price, 0) }}</option>
#endforeach
</select>
</td>
</tr>
#endforeach
</tbody>
with below
<tbody>
#foreach($options as $optiontitle => $optioncollection)
<tr>
<td style="width: 150px;">{{ $optiontitle }}</td>
<td class="text-left">
<select name="attr[]" class="form-control">
<option value="">Select</option>
#foreach($optioncollection as $suboption)
<option value="{{$suboption->id}}">{{$suboption->title}} - {{ number_format($suboption->price, 0) }}</option>
#endforeach
</select>
</td>
</tr>
#endforeach
</tbody>
it will create an attribute array which will prevent the error,
Trying to get property of non-object
If you need further assistance, leave a comment.
When the User tries to add/store an sponsor along with sponsorship type in his CMS, I get this error SQLSTATE[23000]. Im wondering what could be the reason for this error. The row wasnt previously set to null, and I it shouldnt be because the sponsor must have a certain sponsorship type.
This is my view:
<div class="panel-body">
<div class="panel-body list-group list-group-contacts">
#foreach($event->sponsors as $sponsor)
{{ Form::open(array( 'route' => array( 'remove.sponsor.from.conference' , $event->id ) , 'method' => 'DELETE' )) }}
{{ Form::hidden('sponsor' , $sponsor->id) }}
<a href="#" class="list-group-item">
<span class="contacts-title">{{ $sponsor->name }}</span> <br>
<span class="contacts-title">{{ \Events\Models\Sponsor::$_SPONSORSHIP_TYPES[$sponsor->pivot->sponsorship_type][0] }}</span>
<div class="list-group-controls">
{{ Form::submit('X' , array( 'class' => 'btn btn-primary btn-rounded' )) }}
</div>
</a>
<br/>
{{ Form::close() }}
#endforeach
</div>
<h2>Add Sponsor</h2>
{{ Form::open(array( 'route' => array('add.sponsor.to.conference' , $event->id) , 'method' => 'POST')) }}
<select name="sponsor" class="form-control">
<option value="">Choose sponsor</option>
#foreach($sponsors as $sponsor)
<option value="{{$sponsor->id}}">{{ $sponsor->name }}</option>
#endforeach
</select>
<br>
<select name="sponsorship_types" class="form-control">
<option value="">Choose sponsorship type</option>
#foreach(\Events\Models\Sponsor::$_SPONSORSHIP_TYPES as $key => $value)
<option value="{{ $key }}">{{ $value[0] }}</option>
#endforeach
</select>
<br>
Controller function:
public function addSponsorToConference($event_id)
{
if (Input::get('sponsor') != "" && is_numeric(Input::get('sponsor')) )
{
$sponsor = Sponsor::find(Input::get('sponsor'));
$conference = Event::find($event_id);
$conference->sponsors()->save($sponsor, array('sponsorship_type' => Input::get('sponsorship_type')));
}
return Redirect::back();
}
public function removeSponsorFromConference($event_id)
{
$conference = Event::find($event_id);
$conference->sponsors()->detach(Input::get('sponsor'));
return Redirect::back();
}
Model:
class Sponsor extends BaseModel {
public static $_SPONSORSHIP_TYPES = array(
1 => ['Platinum sponsors', 400, 150],
2 => ['Gold sponsors', 300, 100],
3 => ['Silver sponsors', 200, 80],
4 => ['Bronze sponsors', 150, 70],
5 => ['Media partner', 60, 60],
);
protected $table = 'sponsors';
protected $fillable = ['name', 'logo', 'link', 'description'];
public function events()
{
return $this->belongsToMany('\Events\Models\Conference');
}
}
Event Model:
class Event extends BaseModel {
protected $table = 'events';
protected $fillable = ['title', 'slug', 'description', 'ticket_limit', 'location', 'gmap_location'];
public function sponsors()
{
return $this->belongsToMany('\Events\Models\Sponsor')->withPivot("sponsorship_type");
}
public function getSponsorsForShow()
{
$return = array();
foreach(Sponsor::$_SPONSORSHIP_TYPES as $key => $value){
$sponsors = $this->sponsors()->where("sponsorship_type", "=", $key)->get();
if(count($sponsors)) $return[] = array("height" => $value[2], "width" => $value[1], "sponsors" => $sponsors, "sponsorship_title"=>$value[0]);
}
return $return;
}
}
Looks like a simple fix.
Input::get('sponsorship_type')
This returns null which your database doesn't like. The only way this should return null is if there was no field named sponsorship_type.
So looking at your HTML, you have <select name="sponsorship_types" class="form-control">
I think you need to change the name of this element to sponsorship_type.