0

I want to submit data into db when the user fills a form. But it is behaving strangely.

This is my contoller function:

      namespace App\Http\Controllers\PrivatePages;
      use App\Http\Controllers\Controller;
      use App\Http\Requests\Request\CreateProductRequest;
      /*use App\Productuse Illuminate\Support\ServiceProvider;*/
      use Illuminate\Support\Facades\View;
      use Illuminate\Support\Facades\DB;
      use App\Http\FormRequests;


    public function UpdateAdminProfile(CreateProductRequest $request){
    Product::StoreProductDetails($request);
  }

This is my model function which defines the funtion:

   namespace App;

   use App\Http\Requests\CreateProductRequest;
   use Illuminate\Database\Eloquent\Model;
   use App\Http\Controllers\PrivatePages;



  use  Illuminate\Http\Request;

public static function StoreProductDetails(CreateProductRequest $request)
{

        $saveproduct = new Product();
        $saveproduct->name = $request->get('name');
    $saveproduct->description = $request->get('description');
    $saveproduct->save();

}

this is my request file:

<?php
     namespace App\Http\Requests\Request;
    use Illuminate\Foundation\Http\FormRequest;
    class CreateProductRequest extends FormRequest
 {
 /**
  * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{

    return [

        'name' => 'required',
        'description' => 'required',
        'contactinfo'=> 'required',

    ];

}



   public function messages()
   {
      return[
        'name.required' => 'Enter Name',
        'description.required' => 'Enter description',
        'contact.required' => 'Enter contact info',

    ];
    }
  }

my form

   @if (Session::has('success'))
   <div class="alert alert-success" role="alert" style="font-size: 18px;">
    <strong>Success: </strong>

    {{ Session::get('success') }}
 </div>
  @endif

       <form id="" method="post" class="form-horizontal" action="{{ 
      route('updateadminprofile')}}"enctype="multipart/form-data" >



      <div class="form-group">
      <label class="col-sm-4 control-label" for="userName"> Name</label>
      <div class="col-sm-5">
     <input type="text" class="form-control" id="name" name="name" 
      placeholder="name}"  value="name" />
     </div>
    </div>
   <div class="form-group">
   <label class="col-sm-4 control-label" for="userName"> Description</label>
   <div class="col-sm-5">
    <input type="text" class="form-control" id="description" 
   name="description" placeholder="Description}"  value="description" />
  </div>
  </div>
     <button  style="margin-left: 30%" type="submit" class="btn btn-primary" 
       name="signup" value="sumbmit" >Save</button>

It is not giving error

 jsut refreshes the page and no record is saving in db

Why is it throwing an error as every thing seems good? Now it just Refreshes the page. not even going to route specified in action of form

2
  • 1
    What if you dd($request);? Commented Nov 24, 2017 at 14:58
  • on dd it gives me this line "CreateProductRequest {#177}" Commented Nov 24, 2017 at 15:16

2 Answers 2

3

Change Your CreateProductRequest to the following by extending FormRequest not Request

use Illuminate\Foundation\Http\FormRequest;

class CreateProductRequest extends FormRequest
{
public function rules()
{
    return [
        'name' => 'required',
        'description' => 'required',
        'contactinfo'=> 'required',
    ];
}

public function messages()
{
    return [
        'name.required' => 'Enter Name',
        'description.required' => 'Enter description',
        'contact.required' => 'Enter contact info',
    ];
}
}

Hope this works :)

Sign up to request clarification or add additional context in comments.

12 Comments

have you checked your database record??
can you update your modified code?? and make sure your model and db table are correctly connected and working.
it should work now, what's your problem exactly after updating??
As per above code. when i try to submit form using button present in my form record in not saving in db. infact it is now not even going to route specified in the action of form. On button click of submit browser just refreshes and stays on the same form.
in your rules function before retrun try to dd($request->all()) or dd(Input::all())
|
1

Please try this:

public static function StoreProductDetails(CreateProductRequest $request)
{
    $saveproduct = new Product();
    $saveproduct->name = $request->get('name');
    $saveproduct->description = $request->get('description');
    $saveproduct->save();
}

And change your CreateProductRequest.php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreateProductRequest extends FormRequest
{
    public function rules()
    {
        return [
            'name' => 'required',
            'description' => 'required',
            'contactinfo'=> 'required',
        ];
    }

    public function messages()
    {
        return [
            'name.required' => 'Enter Name',
            'description.required' => 'Enter description',
            'contact.required' => 'Enter contact info',
        ];
    }
}

Edited: you can check errors in view

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.