Laravel: How to Compare 2 Collection Attributes - php

I am sending data to my view as below;
I am displaying all contacts in a multi select form and trying to get "selected" printed in options listings. (Then I will use jquery to populate the select field with chosen plugin) In my other pages, nested foreach loop works(something like below), it works #if check is checking against a single value. But now, I have multiple values to check. Nested foreach loop fails (see below)
$contacts = ContactDirectory::where('customer_id', Auth::user()->customer_id)->get(); //Say 50 contacts
$rfito = rfito::where('rfi_id',$rfiid)->get(); // say 5 contacts
then
#foreach ($contacts as $contact)
#foreach ($rfito as $rfito)
<option value="{{$contact->id}}" #if($contact->id == $rfito->contact_id) selected #endif >
{{$contact->firstname}}
</option>
#endforeach
#endforeach

Related

Laravel Update multiple records with different value

I want to update the two records that I got by using the get() with different value.
I fetched the Auth user language with this line of code.
$languages = UserLanguage::where('user_ID', Auth::user()->id)->get();
this is the result of my diedump
In my view this is the foreach loop
<select name="language">
#foreach( $languages as $language )
<option value="{{ $language->id }}">{{ $language->language_name}}</option>
#endforeach
</select>
this is the view picture of foreach
I want to change those 2 records with English and Japanese with 1 button but I don't know how to update those records. please help

Prevent foreach from loop html tag multiple time in blade laravel

I'm trying to loop data using foreach and I want to skip the html tag after the first item being looped.
I've tried like the code bellow, but the html tag <p> still being looped multiple time. What I want is the <p> tags only looped once
#foreach ($store_icon as $key => $icon)
#if ($key < 1)
<p class="available-at">Also available at:</p>
#endif
#endforeach
Result example:
What I want is like this:
Also Available at
- Product 1
- Product 2
But using the code above it resulted like this:
Also Available at
- Product 1
Also Available at
- Product 2
Thanks
I am not sure why it is not working, It must work and working the same code for as well, you can try --
#foreach ($store_icon as $key => $icon)
#if ($key == 0)
<p class="available-at">Also available at:</p>
#endif
#endforeach
OR you can use like this
#if(!empty($store_icon))
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach
#endif
Whats problem? Just take it out from loop.
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach

Laravel/Blade Drop Down List - For more than one field

I'm trying to use blade to display a dropdown list from table data. The problem I have, is that I want to display the results of two fields in the table concatenated, not just one.
So I want something to render something like;
<select id="agreement_type" name="agreement_type">
<option value="1">Agreement Field 1 - Agreement Field 2</option>
<option value="2">Agreement Field 1 - Agreement Field 2</option>
<option value="4">Agreement Field 1 - Agreement Field 2</option>
</select>
My controller currently looks like this;
$agreements = Agreement::lists('agreement_type','id');
return View::make('client_agreements.create')
->with('agreements', $agreements);
My blade view currently looks like this;
<div class="form-group">
{{ Form::label('agreement_type', 'Agreement type') }}
{{ Form::select('agreement_type', $agreements) }}
</div>
I have tried to amend the view and controller in various ways to get the desired output. But can't get it to work.
I want to display agreement_type and level and have the id set as the value so I tried;
$agreements = Agreement::lists('agreement_type'.'level','id');
But this just displays level as the value and completely ignores id.
This is the simplest method. Just use a foreach loop to build the options array:
$agreements = Agreement::all();
$agreementOptions = array();
foreach($agreements as $agreement){
$agreementOptions[$agreement->id] = $agreement->agreement_type.' '.$agreement->level;
}
return View::make('client_agreements.create')
->with('agreements', $agreementOptions);
However you can also define an attribute accessor in your model. You can use that to create new properties that can be accessed normal, but you can run logic to generate them (like combining to attributes)
public function getSelectOptionAttribute(){
return $this->attributes['agreement_type'].' '.$this->attributes['level'];
}
And then you can use lists:
$agreements = Agreement::all()->lists('select_option', 'id');
(Laravel converts SelectOption (studly case) from the method name to select_option (snake case) for the attribute, so don't get confused by that)

Laravel update form - get correct selected state

Within my update profile form, I have a select list for title. Admittedly, the items should be derived from the database however for the moment, I would like this particular select list to do the following..
Show the selected item on initial page load. So, if the $user['title'] is a 'Mrs', this is displayed.
Should laravel validation bring up errors on other form fields, and the user has changed the title to 'Mr', the selected state should be on the item 'Mr'.
I have the following so far, but it isn't working correctly.
<select name="title" id="title">
<option value="Mr" #if(($user['title']) == 'Mr') selected #else #if((Input::old('title')) == 'Mr') selected #endif #endif>Mr</option>
<option value="Mrs" #if(($user['title']) == 'Mrs') selected #else #if((Input::old('title')) == 'Mrs') selected #endif #endif>Mrs</option>
....
</select>
You should pass the data for the select from your Controller and also uset the Form component to build the select in the View, for example:
In Controller method:
$users = User::lists('title', 'id');
return View::make('user')->with('users', $user);
In the View:
// By default, item with id 1 (the first item) will be selected
echo Form::select('title', $users, Input::old('title', 1), ['id'=>title]);
If you are using Blade then:
// By default, item with id 1 (the first item) will be selected
{{ Form::select('title', $users, Input::old('title', 1), ['id'=>title]) }}
Read the documentation properly. You are using Laravel with old school PHP coding.

PHP: How to organise queries and tables to generate HTML selection box

I am attempting to create a drop down selection box for various attributes of a product which the user would select on a product page, such as weight or flavour or colour. My database for the products is currently as follows:
products(id,productname,productprice)
productattributes(id,productid,attributename,value)
I'm using Laravel. So far in my controller I have:
public function show($id)
{
$product = DB::table('products')->where('slug', $id)->take(1)->get();
$prodid = DB::table('products')->where('slug', $id)->pluck('id');
$prodattr = DB::table('productattributes')->where('prodid', $prodid)->get();
return View::make('product/singleproduct', ['product' => $product, 'prodattr' => $prodattr]);
}
And in the view I have:
#foreach ($prodattr as $prodattribute)
{{ $attrname = $prodattribute->attributename }}
{{ $attrvalue = $prodattribute->value }}
#endforeach
This gives me the output:
Flavour Unflavoured
Flavour Strawberry
Weight 1kg
So I have the data I need, but my problem is how to get this into a select box for each attribute. I won't always know what the attribute names will be (colour may be added, for example) or how many there will be, so I don't want to hard code 'Flavour' etc in. I essentially want to generate HTML like this:
<select name="flavour">
<option value="Unflavoured">Unflavoured</option>
<option value="Strawberry">Strawberry</option>
</select>
<br>
<select name="weight">
<option value="1kg">1kg</option>
</select>
Does anyone have any ideas? I'm thinking a loop within the loop, or maybe my table layout is less than ideal? I thought about adding another query within the foreach loop, but this seems counterproductive as I think I have all the data I need. I also wondered if it would be possible to combine or merge the arrays somehow? Any help is much appreciated!
You could create a third table in order to seperate custom attributes and available values for each attributes.
Example of structure I would use:
products(id, name, price)
product_meta_name(id, name)
product_meta_value(id, product_meta_name_id, product_id, value)

Categories