R
R
Rishat Sultanov2017-03-29 22:34:42
Laravel
Rishat Sultanov, 2017-03-29 22:34:42

How to insert data into database in controller?

Good afternoon, ladies and gentlemen.
There is a form implementation and I want to catch the data that is in it and throw it into the database. In order to monitor everything that happens in the admin panel.
I do not fully understand how this can be done in this situation.
My sketches:

public function postContact(Request $request) {
        $this->validate($request, [
            'email' => 'required|email',
            'subject' => 'min:3',
            'message' => 'min:10',
            'infoaboutsender' => 'min:6']);

        $data = array(
            'email' => $request->email,
            'subject' => $request->subject,
            'bodyMessage' => $request->message,
            'infoaboutsender' => $request->infoaboutsender,
            'emails' => $request->emails
        );

        Mail::send('includes.contactemail', $data, function($message) use ($data){
            $message->from($data['email']);
            foreach ($data['emails'] as $selectedOption)
            {
            $message->to($selectedOption);
                Feedback::insert('feedback')->insert([
                    'name' => $data['infoaboutsender'],
                    'email' => $data['email'],
                    'subject' => $data['subject'],
                    'message' => $data['bodyMessage'],
                    'employees' => $selectedOption,
                ]);

            }
            $message->subject($data['subject']);

        });

At the output after my implementation, I see a problem with typing ..
FatalThrowableError in Builder.php line 2087:
Type error: Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array, string given, called in

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mokhirjon Naimov, 2017-03-30
@rishatss

Try like this:

\DB::table('feedback')->insert([
    [
        'name'      => $data['infoaboutsender'],
        'email'     => $data['email'],
        'subject'   => $data['subject'],
        'message'   => $data['bodyMessage'],
        'employees' => $selectedOption,
    ]
]);

A
Alexander Aksentiev, 2017-03-29
@Sanasol

insert('feedback')->insert

A
Andrzej Wielski, 2017-03-30
@wielski

Documentation, read the documentation...

Feedback::create([
  // массив с данными
]);

Where did you get insert from?
PS Using the DB class and working with the database directly is bad form during Laravel development. Use Eloquent.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question