El conde Lucanor
El conde Lucanor

Reputation: 134

How to save 2 times different data in the same field of DB table? - Laravel

This is a little hard to explain, but I'm gonna put it the easiest possible way.

I've a form with 1 field repeated 2 times to save 2 different times in the same submit.

The purpose is to save those 2 times repeated fields in 2 different rows of the DB at the same time.

Why that structure? I don't know, but this is how it is created the form and the table, and it cannot be modified.

Any way to do it? Modifying the model? The controller maybe? Help Laravel gurus.

DB Structure

  • ID (Primary Key, AUTOINCREMENT)
  • Data (String)

DataController.php

namespace App\Http\Controllers\data;

use App\Data;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class DataController extends Controller
{
    public function create()
    {
        return view('data', ['Data' => new Data()]);
    }

    public function store(Request $request)
    {
        Data::create($request->validated());
    }
}

Data.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Data extends Model
{
    protected $table = 'table_data';

    protected $primaryKey = 'ID';

    protected $fillable = [
        'data'
    ];

    public $timestamps = false;
}

data.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Send Data</title>
</head>
<body>
    <form action="{{ route("store") }}" method="POST">
        @csrf
        
        {{-- Data in field 1 in DB --}
        <div>
            <input type="text" name="data" id="data">
        </div>

        {{-- Data in field 2 in DB --}
        <div>
            <input type="text" name="data" id="data">
        </div>

        <input type="submit" class="btn btn-primary" value="Enviar"> 
    </form>
</body>
</html>

Upvotes: 0

Views: 934

Answers (1)

Mohamed Gamal Eldin
Mohamed Gamal Eldin

Reputation: 716

You will add JSON to DB Just follow steps

{{-- At blade --}
{{-- Data in field 1 in DB --}
<div>
    <input type="text" name="data[]" id="data"> <!-- you should add[] to return array of data -->
</div>

{{-- Data in field 2 in DB --}
<div>
   <input type="text" name="data[]" id="data">
</div>

The next step

// At controller
$request->validate([
   'data.*' => ['required', 'date'], // your validation
]);
$json = json_encode($request->get('data'));
Data::create(['data' => $json]);

The next step is when you call data from the database

// Model (Data)
// We change from json to array (you can do anything here like chage date format)
public function getDataAttribute()
{
   return json_decode($this->data);
}

The last step

//anywhere
$model->data[0]
$model->data[1]

NOTE you can do want with a different key just at HTML change names data1, data2 If you want to do it just tell me and I will edit the answer.

Another note: I did not try this code, If anything wrong happens please tell me to know.

Happy Coding.

Upvotes: 1

Related Questions