Data is not storing in mysql database table.

  Sun 02 / 07 / 2023

  Posted by: OG Designs

Category : Laravel

Tags : laravel , php , php laravel , backend , deployment

Post Thumbnail
Description

I don't know the reasons why my data is not getting stored in the MySQL database table.

If I add color and click on submit button it will submit and return with a successful message and displayed for frontend but it will not store in MYSQL Data Table.  


View Code
                        ```
<?php

namespace App\Http\Controllers\Admin;

use App\Models\Color;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\ColorFormRequest;

class ColorController extends Controller
{
    public function index()
    {
        $colors = Color::all();
        return view('admin.colors.index', compact('colors'));
    }

    public function create()
    {
        return view('admin.colors.create');
    }

    public function store(ColorFormRequest $request)
    {
        $validateData = $request->validated();
        $validateData['status'] = $request->status == true ? '1' : '0';
        Color::create($validateData);

        return redirect('admin/colors')->with('message', 'Color Added Successfully');
    }

    public function edit(Color $color)
    {
        return view('admin.colors.edit', compact('color'));
    }

    public function update(ColorFormRequest $request, $color_id)
    {
        $validateData = $request->validated();
        $validateData['status'] = $request->status == true ? '1' : '0';
        Color::find($color_id)->update($validateData);

        return redirect('admin/colors')->with('message', 'Color Updated Successfully');
    }

    public function destroy($color_id)
    {
        $color = Color::findOrFail($color_id);
        $color->delete();

        return redirect('admin/colors')->with('message', 'Color Deleted Successfully');
    }
}

```


HERE MY PRODUCT COLOR MODEL CODE

```
<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ProductColor extends Model
{
    use HasFactory;
    protected $table = 'product_colors';
    protected  $fillable = [
        'product_id',
        'color_id',
        'quantity'
    ];
}

```


HERE MY BLADE CODE

```
  <div class="tab-pane fade border p-3" id="color-tab-pane" role="tabpanel" aria-labelledby="details-tab" tabindex="0">
                            <div class="mb-3">
                                <label>Select Color</label>
                                <hr />
                                <div class="row">
                                    @forelse ($colors as $coloritem)
                                    <div class="col-md-3">
                                        <div class="p-2 border mb-3">
                                            Color: <input type="checkbox" name="color[{{ $coloritem->id}}]" value="{{ $coloritem->id}}" />
                                            {{ $coloritem->name}}
                                            <br />
                                            Quantity: <input type="number" name="colorquantity[{{ $coloritem->id}}]" style="width: 70px; border:1px solid" />
                                        </div>
                                    </div>
                                    @empty
                                    <div class="col-md-12">
                                        <h1>No Colors Found</h1>
                                    </div>
                                    @endforelse
                                </div>
                            </div>
                        </div>
                    </div>
                    <div>
                        <button type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </form>

```
                
  Answered by CDL

You are saving it as array but for the array you need to pass the same paramter as db has. From the front end you are passing your color name with id suffix like if the color is red5, so it will append the color name with color id.


If you are not sure what the attribute are passing from html to controller so you can add dd to see the output.


Add the below code after the validated line;

~~~

$validateData = $request->validated();

dd($validateData);

~~~

And see the output and pass these custom param like below

Color::create([

'name' => $request->name_of_your_input,

'quantity' => $request->quantity_of_your_input,

]);

  Comment   Share
1 Like   0 Comments