How to store multi-valued values ​​in Laravel 5.2

I have a form in which news is stored.

enter image description here

Here I use Multiselect, and I want to save all the selected options in a table, since users, employees, cinemahall as a string.

My controller function for storing values

 public function store(Request $request)
{

    $input=$request->all();

    General_news::create($input);
    return redirect()->back();
}

This function stores all sent fields, but for multitasking, it only saves the last parameter ie cinemahall

enter image description here

When the form is submitted, all selected parameters are displayed, but it is not saved in the database table properly.

help me solve this problem.

+4
source share
3 answers

Make sure the name attribute is assigned to the array

<select multiple="multiple" name="news[]" id="news">

,

$news = $request->input('news');
$news = implode(',', $news);

, Users,staff,cinemahall. , , , news. , except() news , .

$news = $request->input('news');
$news = implode(',', $news);

$input = $request->except('news');
//Assign the "mutated" news value to $input
$input['news'] = $news;

General_news::create($input);
return redirect()->back();
+10

, , , , - . , posts tags tags, posts.

, , news news_types.

0

@Chay22

you can also use Mutators and Accessor https://laravel.com/docs/5.4/eloquent-mutators#defining-an-accessor

public function setFooAttribute($value)
{
    $this->attributes['foo'] = implode(',',$value);
}

public function getFooAttribute($value)
{
    return explode(',',$value);
}
0
source

All Articles