John Davidson

php - Array to string conversion error, how to store data from a form using laravel

0 comments
Message:


many to many relationship between tables posts and features


PostController


public function create()
{
$features = Feature::all();
return view('form', compact('features'));
}


public function store(Request $request)
{
$post = Post::create($request->except(['features']));
$post->features()->sync($request->feature);
}

HTML


<div class="row">
@foreach($features as $feature)
<div class="col-6 col-md-3 mt-2 ">
<div class="form-check form-check-inline mr-4 pr-4">
<input class="form-check-input" type="checkbox" id="{{$feature}}" name="feature[]" value="{{$feature->id}}">
<label class="form-check-label" for="{{$feature}}">{{$feature->feature}}</label>
</div>
</div>
@endforeach
</div>

FeatureSeeder


 public function run()
{
Feature::create(['feature'=>'Backup Camera']);
Feature::create(['feature'=>'Cruise Control']);
Feature::create(['feature'=>'Power Steering']);
Feature::create(['feature'=>'Hands-Free']);
Feature::create(['feature'=>'Heated Seats']);
Feature::create(['feature'=>'Keyless-Entry']);
Feature::create(['feature'=>'Sunroof']);
}

if the question is not well formed or any information needed feel free to comment it,
** thanks in advance**



My Answer:
>all());

$post->features()->attach($request->features);

return redirect()->route('posts.index');
}

Rate this post

4 of 5 based on 6234 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us