So my problem is that if i try to save only one value like "Location in the layout" of a post it works, but the moment i am saving an array I'm getting something like ["value 1" , "value 2"] in the DB, therefore it cant be read and in the CRUD-Controller there is no data saved when i am editing.everything works perfectly except the value i am getting from the data. I would appreciate any help, methods or alternatives
edit.blade.php
<div class="form-group">
<label>Select Post Location</label>
<select
class="form-control select2 select2-hidden-accessible"
multiple=""
data-placeholder="Select Locations"
style="width: 100%"
tabindex="-1"
aria-hidden="true"
name="post_locations[]"
id="post_locations"
>
<option value="TopBox-GR" @if ($post->post_locations == "TopBox-GR") selected @endif>TopBox-GR</option>
<option value="TopBox-C3" @if ($post->post_locations == "TopBox-C3") selected @endif>TopBox-C3</option>
<option value="TopBox-C4" @if ($post->post_locations == "TopBox-C4") selected @endif>TopBox-C4</option>
</select>
</div>
home controller
public function index()
{
$posts = post::where([['status',1], ['post_locations','TopBox-GR']])->get();
return view('user.blog',compact('posts'));
}
and view:
<div class="topbox-c4">
@foreach ($posts as $post)
<a href="{{ route('post',$post->slug) }}">
<div class="image-topbox-c4-container">
<img src="{{ Storage::disk('local')->url($post->image)}}" />
</div>
<div class="text-topbox-c4-container">
<h3>{{$post->title}}</h3>
<p>
{{$post->subtitle}}
</p>
</div>
</a>
@endforeach
</div>
post.controller:
public function update(Request $request, $id)
{
$this->validate($request,[
'title'=>'required',
'subtitle'=>'required',
'slug'=>'required',
'body'=>'required',
'image'=>'required',
'post_locations'=>'required',
]);
if($request->hasFile('image'))
{
$imageName = $request->image->store('public');
}
$post = post::find($id);
$post->image = $imageName;
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->post_locations = $request->post_locations;
$post->body = $request->body;
$post->status = $request->status;
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
$post->save();
return redirect(route('post.index'));
}
My Answer:
To store multiple select values in Laravel 8, you need to make sure that you are storing the values as an array in the database. In your case, you are storing the values as a string, which is why you are seeing the values stored as ["value 1", "value 2"] in the database.
To fix this issue, you can update your post.controller code to store the post_locations as an array. Here's how you can do it:
php
$post->post_locations = json_encode($request->post_locations);
This will store the post_locations as a JSON encoded string in the database, which can be decoded back to an array when retrieving the data.
Additionally, when retrieving the post_locations in your view, you can decode the JSON string back to an array like this:
php
$post_locations = json_decode($post->post_locations);
Then, you can loop through the $post_locations array in your view to display the selected locations.
By making these changes, you should be able to store and retrieve multiple select values in Laravel 8.
Rate this post
4 of 5 based on 9224 votesComments