vendredi 15 février 2019

get the id of a category chosen by user

i brought data ID and name from category tables, and viewed all names on a dropdown list , after choose the name i want to store the id of the name in the product table as a foreign key

this is my functions

public function create()
{
    $category = category::pluck('name','id');
    return view ('books.addbook',compact('category'));
}
public function store(Request $request)
{
    $this->validate($request,[
        'category'=>'required',            
    ]);
    books::create($request->all());
}

this is the view

<div class="form-group row">
    <label for="colFormLabelLg" class="col-form-label col-form-label-lg">category</label>
    <select class="col-sm-10" name ="category">
    <option ></option>          

    @foreach($category as $cat)
    <option class="col-sm-10" value="" name ="category" class="form-control form-control-lg" ></option>          
    @endforeach
    </select>
</div>
</div>

how can i store only the id of the category?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire