Laravel save dropdown value - drop-down-menu

I've made a dropdown in laravel but when I save the request it won't save the value that was selected. How do can I achieve this?
This is my code in my view:
<div class="form-group">
<label for="content" class="col-lg-2 control-label">Karakterrit</label>
<div class="col-lg-10">
<select class="form-control input-sm" name="karakterrit_id">
#foreach($karakterrit as $krit)
<option value="{!! $krit->id !!}">{!! $krit->rit !!}</option>
#endforeach
</select>
</div>
</div>
And this is my controller where I save the request:
public function store(Request $request)
{
$rit = new Rittenregistratie($request->all());
Auth::user()->Rittenregistratie()->save($rit);
return redirect('/create')->with('message','Rit '.$rit->id.' is aangemaakt.');
}
It saves everything right except the value from the dropdown which must be a number.

Related

my livewire app is returning 404 not found when I selected a dropdown item

I am designing a laravel app, which I used module and livewire. I have a dropdown item which when I selected an item its returned an error 404 NOT FOUND
Here is my livewire component.
I am thinking may the wire:model="selectedSession" and wire:model="selectedTerm" is having problem
please i need your help
<?php
namespace Modules\Student\Http\Livewire;
use Livewire\Component;
use Modules\Student\Entities\Result;
use Modules\Student\Entities\Term;
use Modules\Student\Entities\Section;
class ResultDependance extends Component
{
public $selectedSession = null;
public $selectedTerm = null;
public $results;
public function mount($id)
{
$this->results = Result::with('student', 'section', 'term', 'subject')->where('student_id', $id)->get();
}
public function render()
{
return view('student::livewire.pages.resultdependance',
['terms' => Term::all()],
['sessions' => Section::all()]
);
}
}
my blade.php codes
<div class="section">
<div class="card">
<div class="card-body">
<div class="card-title">
<h4><strong>Result</strong></h4>
<div class="form">
<form action="" method="post">
{{csrf_field()}}
<div class="form-group">
<select name="session" id="" class="form-control form-select" wire:model="selectedSession">
<option selected>Select Session</option>
#foreach($sessions as $session)
<option value="{{$session->id}}">{{$session->section_title}}</option>
#endforeach
</select>
</div>
<br>
<div class="form-group">
<select name="term" id="" class="form-control form-select" wire:model="selectedTerm">
<option selected>Select Term</option>
#foreach($terms as $term)
<option value="{{$term->id}}">{{$term->term}}</option>
#endforeach
</select>
</div>
<input type="button" value="test" wire::loading.attr='disabled'>
<h1 wire:loading>please wait</h1>
</form>
<br>
</div>
</div>
</div>
</div>
</div>
I am designing a laravel app, which I used module and livewire. I have a dropdown item which when I selected an item its returned an error 404 NOT FOUND
Here is my livewire component.
I am thinking may the wire:model="selectedSession" and wire:model="selectedTerm" is having problem
please i need your help
It's missing the logic to filter the results based on the selected session and term.
you can do it by using livewire hooks or by adding this
public function filterResults()
{
$this->results = Result::with('student', 'section', 'term', 'subject')->where('student_id', $id)->where('section_id', $this->selectedSession)->where('term_id', $this->selectedTerm)->get();
}
and in your selectedSession
<select name="session" id="" class="form-control form-select" wire:model.lazy="selectedSession" wire:change="filterResults">
Now when the user changes the selected session or term, the component will filter the results and update the view accordingly.

I want to get data to the dropdown by using another dropdown in the same page

I have a dropdown on my page to load districts. And I have another dropdown to load Divisions according to that selected district. The below code I have got all the divisions. But I want only the divisions according to the selected districts. How can I do?
#php
$districtAll=\App\District::all();
#endphp
<div class="row">
<label class="col-md-4 control-label" for="district">9. District</label>
<div class="col-md-8">
<select name="district">
<option type="text" class="detail-wp" value="">Select District</option>
#foreach($districtAll as $all)
<option value="{{$all->DISTRICT_ID}}">{{$all->DISTRICT}} </option>
#endforeach
</select>
</div>
</div>
<br>
#php
$getDsAll=\App\DsDivision::all();
#endphp
<div class="row">
<label class="col-md-4 control-label" for="ds_division">10. DS Division</label>
<div class="col-md-8">
<select name="ds_division" class="form-control">
<option type="text" class="detail-wp" value=""></option>
#foreach($getDsAll as $all)
<option value="{{$all->DIVISION_ID}}">{{$all->DIVISION}} </option>
#endforeach
</select>
</div>
</div>
You can do it using ajax request:
$('select[name=district]').on('change', function(){
$districtId = $(this).val();
$.get('/division/district/'+$districtId).done(function(res){
$.each(res, function (key, value) {
$('select[name=ds_division]').append(`<option value='` + value.id + `'>` + value.name +`</option>`);
});
});
to make this working you will need a route like
Route::get("/division/district/{district}", "DivisionController#getByDistrictId");
DivisionController#getByDistrictIdwill be
public function getByDistrictId($district)
{
return Division::where('district_id',$district)->get();
}
this code just to give you an idea of how you can do it.

how to validate select box in laravel (without using JS)

<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Publication Status</label>
<div class="col-sm-10">
<select name="publicationStatus" class="form-control">
<option value="">Select Manufacturer</option>
<option value="1">Published</option>
<option value="0">Unpublished</option>
</select>
<span class="text-danger">{{$errors->has('publicationStatus')? $errors->first('publicationStatus'):''}}</span>
</div>
</div>
my controller
public function storeManufacturer(Request $request){
$this->validate($request,[
'manufacturerName'=>'required',
'manufacturerDescription'=>'required',
'publicationStatus'=>'required',
]);
above 'manufacturerName' & 'manufacturerDescription' is working but publicationStatus is not validate in the same way.Maybe the reason is that contain multiple value.
You could check data of $request with dd() in your controller firstly:
dd($request->publicationStatus);

Failed to save value select option

what's wrong in my project.
I want to save with select option, but the filed doesn't save data select option. it just save input text.
the project is taken different table, and the form just get the project_name and project_id. project_id will save in in table spent_times.
and the select option will save task_category in table spent_times
this my model
protected $table = 'spent_times';
protected $fillable = [
'task_category',
'story/meeting_name',
'assign',
'estimated_time',
'user_story',
'spent_time',
'percentage',
'lateness',
'index',
'project_id'
];
public function users() {
return $this->hasMany(User::class);
}
public function project() {
return $this->belongsTo(Project::class);
}
my create.blade.php
<form action="{{route('store')}}" method="POST">
#csrf
<div class="box-body">
<div class="form-group">
<label for="">Project *</label>
<select class="form-control select2" style="width: 100%;">
<option>Select One</option>
#foreach($projects as $id => $project)
<option value="{{$id}}">{{$project}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Story # Meeting Name *</label>
<input type="text" class="form-control" name="user_story">
</div>
<div class="form-group">
<label for="">Category *</label>
<select class="form-control select2" style="width: 100%;">
<option>Select One</option>
#foreach($task_categories as $category)
<option value="{{$category}}">{{$category}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Estimated *</label>
<input type="text" class="form-control" name="estimated_time">
</div>
</div>
<div class="box-footer">
<a href="{{route('index')}}">
<button type="submit" class="btn btn-primary col-md-12" style="border-radius : 0px;">SAVE</button>
</a>
</div>
</form>
my controller
public function create()
{
$spentimes = new SpentTime;
$project = new Project;
$projects = Project::select('project_name', 'id')->get();
return view('Ongoings.index', compact ('projects', 'task_categories', 'spentimes', 'project'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd($request->all());
$spentime = SpentTime::create([
'project_name' => request('project_name'),
'user_stroy' => request ('user_story'),
'task_category' => request('task_category'),
'estimated_time' => request('estimated_time')
]);
$spentime->save();
return redirect()->route('index');
}
error like this
error in web browser
please help me
You are expecting those fields from request in controller.
project_id
meeting_name
task_category
estimated_time
But You are just sending estimated_time,
other 3 fields are not present in you create.blade.php
In create.blade.php, category select input don't have any name.
that's why task_category is getting null from request('task_category'). But task_category field is not nullable in your database table.
Send form input properly as you are expecting in your controller. You can use dd($request->all()) to check.
I believe this is your task_category select box.
so this select box doesn't have a name attribute. that's the error.
Possible solution.
<div class="form-group">
<label for="">Category *</label>
<select name="task_category" class="form-control select2" style="width: 100%;">
<option>Select One</option>
#foreach($task_categories as $category)
<option value="{{$category}}">{{$category}}</option>
#endforeach
</select>
</div>

Can't make multiple select in registration form

I use the basic registration form but I want to add a multiple select, a User can have multiple Skills and select them when he register.
My problem is that when I select multiples values Laravel only take the last one, so I search on the web for a solution and they say to add [] after my class name, so this is what I do :
<div class="form-group row">
<div class="col-md-6">
<?php $competences = \App\Competence::all(); ?>
<select name="competences[]" multiple="" class="form-control">
<option value="" selected disabled style="display:none">choose your school</option>
#foreach ($competences as $competence)
<option value="{{ $competence->id }}">{{ $competence->name }}</option>
#endforeach
</select>
</div>
</div>
But now, when I submit the form the page reload and stay in the register form, and the user is not register... I don't know what to do, anyone of you have a solution ?
Here is how I check the competences in my registerController
$competences = $data['competences'];
foreach ($competences as $comp){
$user->competences()->save(Competence::find($comp));
}
Use multiple="multiple" OR just multiple in blade.php:
<div class="form-group row">
<div class="col-md-6">
<?php $competences = \App\Competence::all(); ?>
<select name="competences[]" class="form-control" multiple>
<option value="" selected disabled style="display:none">choose your school</option>
#foreach ($competences as $competence)
<option value="{{ $competence->id }}">{{ $competence->name }}</option>
#endforeach
</select>
</div>
</div>
then in your controller, verify that you're actually getting the data in the form of an array by just printing $data
return $data;

Resources