Laravel Update multiple records with different value - php

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

Related

Laravel, can't build query for array in multiple <select>

I made a filter to find businesses
There are several cities to choose from
Cities store on city_id column
The filter looks like this
<select id="city" multiple name="city[]">
#foreach($cities as $city)
<option value="{{ $city->name }}">{{ $city->name }}</option>
#endforeach
</select>
All request
dd($request->all())
shows me this
I build query for franchise or business
like this
if ($request->has('fb')) {
$businessesQuery->where('fb', $request->fb);
}
I try to build query like this but it's not works
if ($request->has('city[]')) {
$typeArray = explode(",", $request->city[]);
$businessesQuery->whereIn('city_id', $typeArray);
}
Help me solve this issue, I would be very grateful!
Your html must use city_id as a value and not the city name. Because you're trying to search using the id not name.
<option value="{{ $city->id }}">{{ $city->name }}</option>
explode function takes a string as in input and returns an array. In your case your city[] request value is already an array visible from the dump. You should use it directly. Something like this.
if ($request->has('city')) {
$businessesQuery->whereIn('city_id', $request->input('city');
}

Laravel: How to Compare 2 Collection Attributes

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

Display other table data from drop down list when edit

Is that possible to get data from other table?
Let say in create table, I get all the info from genre table and display all data in drop down list
<select name="mov_genre" id="mov_genre" class="form-control input-sm">
<option value="">类型</option>
#foreach(App\Genre::all() as $gData)
<option value="{{$gData->gen_title}}">{{$gData->gen_title}}</option>
#endforeach
</select>
In edit page, I want to display it as well but how to display it as the genre I selected when create?
In my opinion, you should use $gData->id as value of each option. Because normally you store the id as foreign key and not something like the title.
But to your question itself: You can simply add an if statement that checks within the foreach loop if the current item is the selected one. In my example I assume you have a variable $movie which is the entry you want to edit.
<select name="mov_genre" id="mov_genre" class="form-control input-sm">
<option value="">-- SELECT GENRE --</option>
#foreach(App\Genre::pluck('gen_title', 'id') as $key => $value)
<option value="{{ $key }}" {{ optional($movie)->genre_id === $key ? 'selected' : '' }}>{{ $value }}</option>
#endforeach
</select>
Please note that I swapped App\Genre::all() for App\Genre::pluck('gen_title', 'id'). This will only load the both fields gen_title and id and put them in an array where id is the key and gen_title the value. It is done for better performance as we do not load unnecessary data.
Also note that optional($movie) will return null when $movie is not set. This is done to reduce weird if statements and is common practice.

Select drop down from table array

Here is part of my controller;
$agreements = Agreement::all();
print_r($agreements) //this works and displays an object/array with the details!
return View::make('individual_agreements.create')
->with('individual', $individual)
->with('agreements', $agreements)
->with('content_title', 'Create new individual agreement');
Here is part of my view;
<div class="form-group">
{{ Form::label('agreement_name', 'Agreement name') }}
{{ Form::select('agreement_name', $agreements->agreement_name) }}
</div>
I have a field called agreement_name in the table.. All I want to do is turn that in to a dropdown in the most efficient Laravel/Eloquent way as possible.
I keep getting the standard "agreement_name not defined" error and I cannot find a suitable example anywhere online having looked for over an hour now.
If you need the agreements for something else on the same page try
<div class="form-group">
{{ Form::label('agreement_name', 'Agreement name') }}
{{ Form::select('agreement_name', $agreements->lists('agreement_name', 'agreement_name')) }}
</div>
This will get all the agreements names within your agreements collection and allow them to be used within the select drop down. First parameter is the column and second is the key.
Else just change your query to;
$agreements = Agreement::lists('agreement_name', 'id');
You should do this
$agreements = Agreement::lists('agreement_name', 'id');
all return you all columns in table, so that's why your code wont work
The way you have this set up allows for the use of a foreach loop to list through and display results:
<select name="agreement_name" class="">
<option value="">- Select -</option>
#foreach($agreements AS $agreement)
<option value="{{ $agreement->id }}">{{ $agreement->agreement_name }}</option>
#endforeach
</select>
Since your $agreements is an array of elements, calling $agreements->agreement_name wouldn't work, while $agreements[0]->agreement_name probably would. If you want to use the Form::select() option, you'll need to use a different method of getting your agreements. Check the other answers for that solution.
Hope this provided some insight!
Edit
I should note that this isn't the Laravel Way of doing things, but it is certainly viable.

form select on laravel

I have data look like this
id rel word
1 A word A
2 B word B
3 B word C
to get the data my controller look like this :
public function get_new()
{
return View::make('form.new')
->with('datas', Data::all());
}
and my View will look like this :
<select>
#foreach($datas as $data)
<option value="{{ $data->id }}" rel="{{ $data->rel }}">{{ $data->word }}</option>
#endforeach
</select>
How i can get that data with Form::select ?
If we use Form::select we can call that with Data::lists('word','id') and pass to the view.
but if i use this i just can get 2 data that is id and word.
how can i get all data that is id, rel and word with Form::select. Please help me.
Laravel 4s FormBuilder and HtmlBuilder aren't going to produce everything for everyone. Taylor himself has said that he wants to keep it lean and simple. The solution you have now is probably the best way you could go about it (using a simple loop).
Form::select isn't capable of automatically populating other attributes of the option element. If you want this functionality you're going to either have to make yourself a custom macro or write your own form generator.
Probably not the answer you're after but that pretty much sums it up.
Just want to update the solution...
<select name="name" id="name">
#foreach($datas as $data)
<option value="{{ $data->id }}" {{ (Input::old('name', 0) === $data->id ? ' selected="selected"' : '') }} rel="{{ $data->rel }}">{{ $data->word }}</option>
#endforeach
</select>
This will give old input on selected option

Categories