Laravel - Form Input - Multiple select for a one to many relationship
I agree with user3158900, and I only differ slightly in the way I use it:
{{Form::label('sports', 'Sports')}}
{{Form::select('sports',$aSports,null,array('multiple'=>'multiple','name'=>'sports[]'))}}
However, in my experience the 3rd parameter of the select is a string only, so for repopulating data for a multi-select I have had to do something like this:
<select multiple="multiple" name="sports[]" id="sports">
@foreach($aSports as $aKey => $aSport)
@foreach($aItem->sports as $aItemKey => $aItemSport)
<option value="{{$aKey}}" @if($aKey == $aItemKey)selected="selected"@endif>{{$aSport}}</option>
@endforeach
@endforeach
</select>
Laravel 7 multiselect in a one to many relationship
You have the logic correct there, it's just syntax/notation problem
In place of:
in_array($tag->id, $dashboard->tag)
Use:
in_array($tag->id, $dashboard->tag()->pluck("tags.id")->toArray())
Explanation:
$tag->id
returns the ID (int from database)- since you have defined the relationship between dashboard and tags (with belongsToMany function), you just need to get an array that contains all the tag IDs of the variable
$dashboard
$dashboard->tag()
returns the relationship between $dashboard and its tags$dashboard->tag()->pluck("tags.id")
returns a laravel collection object that holds all tag IDs of $dashboard's tags$dashboard->tag()->pluck("tags.id")->toArray()
: the last toArray function convert a laravel collection object to php native array
Laravel - Store data in HasMany relationship from a multi-select form
saveMany
will work in this scenario, you just need to convert your input array to a set of new Models first. Assuming you know the Criteria ID, try this:
foreach (Input::get('bedrooms') as $bedroom) {
$selected[] = New Bedroom(['bedroom' => $bedroom]);
}
$criteria = Criteria::find($criteria_id)->bedrooms()->saveMany($selected);
Laravel 8: Multiple Select Option Does Not Send Data
I think you define your model relation wrong.
Tag model should be:
public function user()
{
return $this->belongsTo(User::class);
}
and the User Model relations:
public function tags()
{
return $this->hasMany(Tag::class);
}
If you aim to establish a one-to-many relation.
Getting selected values from a multiple select form in Laravel
First, if you want to have multiple item selected by default, you have to give an array of values as 3rd parameter, not a simple value.
Exemple:
Form::select('size', array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), array('S', 'M'), array('multiple'));
should show the select with S and M selected.
For the second point, you should try to give a name like size[]
instead of size
, it could be solve the problem (because your posted select is not a simple value, its an array of values)
Laravel Forms - Fill a Multiple Select Input to View
Fixed it by transforming the array elements to int before passing it to json_encode.
Related Topics
How to Get Data from Another Table Using Id from Another Table
Update Data on a Page Without Refreshing
How to Create Multi Level Category Hierarchy ( Category Tree ) - Codeigniter
How to See If a User Is Online in a Website With PHP and MySQL Driven Databases
Laravel: Property [Name] Does Not Exist on This Collection Instance
Stop Inserting Data in the Database When Refreshing the Page
Easiest Way to Replace All Characters in Even Positions in a String.
Checking If Email Is in MySQL Database in PHP
Assign Jquery Value to PHP Variable Inside Ajax Sussess
Character Encoding for French Accents
How to Get Newest Data With 'Created_At' Column
How to Stop PHP Code Execution
How to Use Two Foreach to Insert Data in Database
Laravel Convert Query MySQL to Model Query