How to include a string in the database from an input array with Laravel?

Asked

Viewed 181 times

0

I need to include a string data in my database table that I will take for input, which in turn is an array. Follow the form so you can see how it is.

<div class="form-inline col-md-12">
                    <div class="form-group col-md-4">
                        <label for="create-contest-theme#1" class="control-label">1º Tema: </label>
                        <div class="input-group">
                            <input type="text" name="theme[]" id="create-contest-theme#1" class="form-control">
                        </div>
                    </div>

                    <div class="form-group col-md-4">
                        <label for="create-contest-theme#2" class="control-label">2º Tema: </label>
                        <div class="input-group">
                            <input type="text" name="theme[]" id="create-contest-theme#2" class="form-control">
                        </div>
                    </div>

                    <div class="form-group col-md-4">
                        <label for="create-contest-theme#3" class="control-label">3º Tema: </label>
                        <div class="input-group">
                            <input type="text" name="theme[]" id="create-contest-theme#3" class="form-control">
                        </div>
                    </div>
                </div>

The action of this form points to my controller’s create function:

public function create(CreateRequest $request){
    $inputs = $request->except('_token');

    $contest = Contest::create($inputs);

    return $contest;
}

A model Constest

  class Contest extends Model
    {
        protected $table = 'contests';
}

And my big question is: How to treat this input theme to be a string when it is stored?

2 answers

0

Information is missing, but I’m going to assume you’re using the Laravel 5.4 and want to save each item from array as a bank record.

You need the property $fillable in the model to make a Mass Assigment.

When you populate an Eloquent template with an array through the functions create, fill or update, for safety reasons it is necessary to say which fields will be accepted.

Example

class Contest extends Model
{
    protected $table = 'contests';
    protected $fillable = ['campo_1', 'campo_2', 'campo_3'];
}
  • That’s not what I want to do, What happens is this: I have 3 inputs with the same name=theme[], which will give me an array. But to store this information in the database I wanted to turn this array into a string with a delimiter using ìmplodefor example: theme[0] = ação, theme[1]= drama i want this to be a string equal to "action,drama"

  • That’s why I signaled that information was missing on your question. It is necessary to elaborate better so that you can receive help more quickly and accurately.

0

I found what I wanted, the solution was very simple, I did not understand how things worked very well in the Port, it was a matter of reading the documentation and everything became clearer.

Solution:

public function create(CreateRequest $request){
    $inputs = $request->except('_token');
    $inputs['themes'] = implode(",", inputs['themes']);

    $contest = Contest::create($inputs);

    return $contest;
}

What I was doing before was trying to change the value of request when should change the value of the returned array $inputs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.