Home > Mobile >  Mistake in validation in Laravel
Mistake in validation in Laravel

Time:06-30

Something is wrong with my validation. Data from the form is created and I can see it when I use the dd() function. But when it comes to creating and sending that data to the database it creates an empty row. My Laravel version is 8.83.17. Here's my route:

Route::middleware(['auth.amicms'])->name('amicms.')->prefix('amicms')->group(function() {
    Route::resource('/posts', PostController::class);
});

Here's the request:

public function rules()
    {
        return [
            'name_en' => 'required',
            'body_en' => 'required',
            'name_ua' => 'required',
            'body_ua' => 'required',
            'name_ru' => 'required',
            'body_ru' => 'required',
            'meta_title_en' => 'string',
            'meta_description_en' => 'string',
            'meta_keywords_en' => 'string',
            'meta_title_ua' => 'string',
            'meta_description_ua' => 'string',
            'meta_keywords_ua' => 'string',
            'meta_title_ru' => 'string',
            'meta_description_ru' => 'string',
            'meta_keywords_ru' => 'string',
            'image' => 'required|image',
            'price' => 'required',
            'status' => 'required'
        ];
    }

Here's the model:

protected $fillable = ['name_en, body_en, name_ua, body_ua, name_ru, body_ru,
    meta_title_en, meta_description_en, meta_keywords_en, meta_title_ua, meta_description_ua, meta_keywords_ua,
    meta_title_ru, meta_description_ru, meta_keywords_ru, image, price, status'];

Here's the controller

public function store(StorePostRequest $request)
    {

        $input = $request->all();
        if ($image = $request->file('image')) {
            $imageDestinationPath = 'uploads/';
            $postImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
            $image->move($imageDestinationPath, $postImage);
            $input['image'] = "$postImage";
        }
        Post::create($input);

         return view ('amicms.posts.index', ['layout' => $this->layout]);
    }

Here's the view:

form action="{{ route('amicms.posts.store') }}" method="post" enctype="multipart/form-data">
                        @csrf
                        <div >
                            <div >
                                <div >
                                    <label>Языковая версия</label>
                                    <select id="language" 
                                            name="language" required>
                                        <option value="ru">Русский</option>
                                        <option value="ua">Украинский</option>
                                        <option value="en">English</option>
                                    </select>
                                </div>
                            </div>
                            <div >
                                <div >
                                    <label>Статус</label>
                                    <select 
                                        name="status" id="" required>
                                        <option value="1">Published</option>
                                        <option value="0">Not Published</option>
                                    </select>
                                </div>
                            </div>
                        </div>

                        @php($formLang = ['ru', 'ua', 'en'])
                        @for($i=0; $i<3;$i  )
                            <div id="build-form-{{ $formLang[$i] }}" >
                                <div >
                                    <div >
                                        <div >
                                            <label>Name (<span >{{ $formLang[$i] }}</span>)</label>
                                            <input name="name_{{ $formLang[$i] }}" type="text" placeholder=""
                                                   >
                                        </div>
                                    </div>
                                </div>
                                <div >
                                    <div >
                                        <div >
                                            <label>Full description of the entry (<span >{{ $formLang[$i] }}</span>)</label>
                                            <textarea 
                                                      name="body_{{ $formLang[$i] }}" id="summernote"></textarea>
                                        </div>
                                    </div>
                                </div>

                                <div >
                                    <div >
                                        <div >
                                            <label>Meta Title (<span >{{ $formLang[$i] }}</span>)</label>
                                            <input name="meta_title_{{ $formLang[$i] }}" type="text" placeholder="" >
                                        </div>
                                    </div>
                                    <div >
                                        <div >
                                            <label>Meta Keywords (<span >{{ $formLang[$i] }}</span>)</label>
                                            <input name="meta_keywords_{{ $formLang[$i] }}" type="text" placeholder="" >
                                        </div>
                                    </div>
                                    <div >
                                        <div >
                                            <label>Meta Description (<span >{{ $formLang[$i] }}</span>)</label>
                                            <input name="meta_description_{{ $formLang[$i] }}" type="text" placeholder="" >
                                        </div>
                                    </div>
                                </div>

                            </div>
                        @endfor

                        <div >
                            <div >
                                <div >
                                    <label>Photo</label>
                                    <input name="image" type="file" placeholder="" >
                                </div>
                            </div>
                        </div>

                        <div >
                            <div >
                                <div >
                                    <label>Price</label>
                                    <input name="price" type="number" placeholder="" >
                                </div>
                            </div>
                        </div>

                        <div >
                            <div >
                                <div >
                                    <button type="submit" >Save</button>
                                </div>
                            </div>
                        </div>
                    </form>

Here's the migration:

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('name_en')->nullable();
            $table->text('body_en')->nullable();
            $table->string('name_ru')->nullable();
            $table->text('body_ru')->nullable();
            $table->string('name_ua')->nullable();
            $table->text('body_ua')->nullable();
            $table->text('meta_title_en')->nullable();
            $table->text('meta_description_en')->nullable();
            $table->text('meta_keywords_en')->nullable();
            $table->text('meta_title_ru')->nullable();
            $table->text('meta_description_ru')->nullable();
            $table->text('meta_keywords_ru')->nullable();
            $table->text('meta_title_ua')->nullable();
            $table->text('meta_description_ua')->nullable();
            $table->text('meta_keywords_ua')->nullable();
            $table->string('image');
            $table->decimal('price', 10, 2);
            $table->integer('status');
            $table->timestamps();
        });
    }

CodePudding user response:

  protected $fillable = [
    'name_en','body_en','name_ua','body_ua','name_ru',' body_ru','meta_title_en','meta_description_en','meta_keywords_en','meta_title_ua','meta_description_ua','meta_keywords_ua','meta_title_ru','meta_description_ru','meta_keywords_ru','image','price','status'
    ];
  • Related