1

i want insert data to database, but when i run function doesn't work and does not output an error, data not insert into database but is not show an error? why i should i do ?

public function index(){
    $data_subgroup = \App\Subgroup::all();
    $data_category = \App\Module::all();
    return view('/dashboard/subgroup', ['data_subgroup' => $data_subgroup,
                                        'data_category' => $data_category,                                        
    ]);
}

public function create(Request $request){

    $this->validate($request, [
        'subgroup_logo' => 'file|image|mimes:jpeg,png,jpg|max:2048',
        'subgroup_name' => 'required',
        'module_id'     => 'required'
    ]);

    $subgroup_logo = $request->file('subgroup_logo');
        $filename ='subgroups/'  . time() . '.' . $subgroup_logo->getClientOriginalExtension(); //Memecah Filename
        Image::make($subgroup_logo)->resize(200, 200)->save( public_path('/uploads/' . $filename)); //Resize dan Save Avatar ke Database

    Subgroup::create([
        'subgroup_logo' => $filename,
        'subgroup_name' => $request->subgroup_name,
        'module_id'   => $request->module_id
    ]);
    return redirect()->back();
}

This my Route code:

    Route::get('/dashboard/subgroup', 'Dashboard\SubgroupController@index');
Route::post('/dashboard/subgroup/create', 'Dashboard\SubgroupController@create');

This my Model code:

class Subgroup extends Model{
protected $table = 'subgroup';
public $timestamps = false;
protected $primaryKey = 'subgroup_id';

protected $fillable = ['subgroup_id', 'subgroup_name', 'subgroup_logo', 'module_id'];

public function module(){
    return $this->belongsTo('App\Module' , 'module_id');
}

}

UPDATE : i think error is from insert data from select option,

i change :

$this->validate($request, [
    'subgroup_logo' => 'file|image|mimes:jpeg,png,jpg|max:2048',
    'subgroup_name' => 'required',
    'module_id'     => 'required'
]);

to :

Validator::make($request->all(), [
        'subgroup_logo' => 'file|image|mimes:jpeg,png,jpg|max:2048',
        'subgroup_name' => 'required',
        'module_id'     => 'integer|required'
    ]);

i have select option views :

<label for="category">Select Category</label>
                    <select class="form-control form-control-sm">
                        @foreach($data_category as $category)
                        <option id="module_id" name="module_id" value="{{$category->module_id}}">
                            {{$category->module_name}}</option>
                        @endforeach
                    </select>

I have error :

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'module_id' cannot be null (SQL: insert into subgroup (subgroup_logo, subgroup_name, module_id) values (subgroups/1570166504.png, UNSIKA, ?))

17
  • Show Subgroup eloquent model class. Commented Oct 4, 2019 at 4:38
  • I think your form is missing enctype="multipart/form-data" Commented Oct 4, 2019 at 4:38
  • @ZainFarooq i have enctype Commented Oct 4, 2019 at 4:42
  • Have you set action properly inside form? Commented Oct 4, 2019 at 4:46
  • @ZainFarooq my action="/dashboard/subgroup/create" Commented Oct 4, 2019 at 4:47

2 Answers 2

1

Set name attribute to your select input. You have set it in option tag which is wrong

<select class="form-control form-control-sm" name="module_id">
                        @foreach($data_category as $category)
                        <option id="module_id" name="module_id" value="{{$category->module_id}}">
                            {{$category->module_name}}</option>
                        @endforeach
                    </select>
Sign up to request clarification or add additional context in comments.

Comments

0
//set name and id attribute to your select input then check it.it's should 
//  be works properly.

  <label for="category">Select Category</label>
  <select class="form-control form-control-sm" name="module_id" id="module_id">
      @foreach($data_category as $category)
          <option value="{{$category->module_id}}">
              {{$category->module_name}}</option>
      @endforeach      
  </select>

Comments

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.