Pass data from class select to get route - html

how can I pass data from this form to my route?
<div class="panel-body">
{!! Form::open(array('route'=>'show.exclusion')) !!}
<div class="form-group">
{{Form::label('choosegroup', 'Wähle eine Gruppe')}}
<select class="form-control m-bot15" name="idgroup">
#foreach($groups as $group)
<option value="{{ $group->id }}">{{ $group->name }}</option>
#endforeach
</select>
{{ csrf_field() }}
</div>
<div>
{{Form::submit('Search',['class' => 'btn btn-primary'])}}
<a class="btn btn-default btn-close" href="{{ route('home') }}">Cancel</a>
</div>
{!! Form::close() !!}
</div>
This is my route...
Route::get('exclusion/show/{id}', 'ExclusionController#show')->name('show.exclusion');
First I put the $group->id parameter into the array route part. But I cannot use them at this point of my code.
Any Ideas?
EDIT:
public function show($id)
{
$member = Nerd::find($id);
return view('groups.test')->with('member', $member);
}

You need to pass the whole Request to the function. Than it is possible to get the Values of your Formdata.
public function update(Request $request, $id)
{
$idgroup = $request->input('idgroup');
}
For more details have a look here:
https://laravel.com/docs/5.6/requests

You can use request() method to fetch form data without any class dependencies.
For example:
request()->id;

Related

populate data from database into select dropbox

I have a select box which needs to fetch from the database.
view blade:
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($service as $sn)
{
<option value="">dd($sn)</option>
}
#endforeach
</select>
controller:
public function index()
{
$service = DB::select('select service_type from services');
return view("service",['service'=>$service]);
}
I am getting an error like
Undefined variable: service (View: /home/devadmin/.config/composer/vendor/laravel/installer/serenseprj/resources/views/package.blade.php)
You can use Eloquent instead of doing this
$service = DB::select('select service_type from services');
Using Eloquent,
$services = ModelName::pluck('name', 'id')->get();
You can pass the variable to the targeted view like below:
return view('view_name', ['variable_name' => $variable]);
And, you can access the variable name like below
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($variable_name as $var)
{
<option value="{{$var->id}}">{{$var->name}}</option>
}
#endforeach
</select>
You can use this code as the references. Hope you have service model.
$services= Service::pluck('name', 'id');
return view('services', compact('services'));
then in view file you can use laravel collevtives like.
<div class="form-group col-sm-6">
{!! Form::label('service_id', 'service Id:') !!}
{!! Form::select('service_id', $services, null, ['class' => 'form-control'])!!}
</div>
you can use this too. in you blade file
<select name="service " id="service " class="form-control">
<option value=""> -- Select One --</option>
#foreach ($services as $service )
<option value="{{ $service->id }}">{{ $service->name }}</option>
#endforeach

Submit multiple forms created by foreach loop with one button

In my blade template, I use a foreach to create one form per iteration. Currently, each form has a submit button, but I want to use one submit button to submit all the form, because now I cannot submit all forms at the same time. How can I fix this?
#if(count($task_criterias) > 0)
#foreach($task_criterias as $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
<div class="form-group" hidden >
{{ Form::label('criteria_id', 'criteria_id') }}
{{ Form::text('criteria_id', $task_criteria->id, ['class'=>'form-control']) }}
</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark', '',['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
{!! Form::close() !!}
</td>
#endforeach
<input type="button" class="btn btn-info " value="Submit" onclick="submitForms()" />
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'/criteria_marks/post',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
#endif
There can be only one post submit per request. One of the easiest solution is to perfom multiple requests using ajax:
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'* route to CriteriaMarksController#store *',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
Or you can make one big form:
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
#foreach($task_criterias as $key => $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark['.$task_criteria->id.']', '',
['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
</td>
#endforeach
<input type="submit" class="btn btn-info " value="Submit" />
{!! Form::close() !!}
This will form associative array selfmark in post with id => value pairs
Not sure how to do it with the Form facade.
What you have now is:
<input name="criteria_id" value="{{ $task_criteria->id }}" />
<input name="selfmark" value="{{ $task_criteria->id }}" />
But what you need to have is:
<input name="criteria_id[]" value="{{ $task_criteria->id }}" />
<input name="selfmark[]" />
Note the [] in the input names.

Radio Button Pass The Wrong Value

I have a radio button form with only two values, admin and operator.
Here is the blade
{{-- Level --}}
#if ($errors->any())
<div class="grouped fields {{ $errors->has('level') ? 'input error' : 'success' }}">
#else
<div class="grouped fields">
#endif
{{ Form::label('level', 'LEVEL:') }}
<div class="field">
<div class="ui radio checkbox">
{{ Form::radio('level', 'admin') }}
<label>Administrator</label>
</div>
</div>
<div class="field">
<div class="ui radio checkbox">
{{ Form::radio('level', 'operator') }}
<label>Operator</label>
</div>
</div>
#if ($errors->has('level'))
<div class="ui pointing red basic label">
{{ $errors->first('level') }}
</div>
#endif
</div>
store controller
public function store(Request $request)
{
$data = $request->all();
$validasi = Validator::make($data, [
'name' => 'required|max:200',
'email' => 'required|email|max:100|unique:users',
'password' => 'required|confirmed|min:6',
'level' => 'required|in:admin,operator',
]);
if ($validasi->fails()) {
return redirect('dashboards/users/create')
->withInput()
->withErrors($validasi);
}
// hash pass
$data['password'] = bcrypt($data['password']);
User::create($data);
Session::flash('flash_message', 'Data user berhasil disimpan');
return redirect('dashboards/users/index');
}
I'm not sure whats wrong with the blade or maybe controller. no matter what i choose, the radio button only pass the admin to the database
any clue?

how to store drop-down list select into database?

I have a form for creating projects. It contains 2 foreign keys domain and owner (domain_id and owner_id). in fact these are 2 drop-down lists. when i try to submit the form, and check my database, I found out that owner and domain have NULL as value, eventhough i selected values from the drop-down list.
this is projectController :
public function create()
{
$domains = Domain::all('nameDomaine', 'id');
$owners = Owner::all('nameOwner', 'id');
return view('projectss.create', compact('domaines', 'owners'));
}
public function store(Request $request)
{
$domain_id = Domain::all()->pluck('nameDomain', 'id');
$owner_id = Owner::all()->pluck('nameOwner', 'id');
$this->validate($request, [
'title' => 'required',
'code' => 'required',
'domain_id' => 'required',
'owner_id' => 'required',
]);
Project::create($request->all());
return redirect()->route('projects.index')
->with('success', 'Project created successfully');
}
and this is create.blade.php :
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> Domain : </strong>
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $id => $domain)
<option value="{{ $id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> owner : </strong>
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $id => $owner)
<option value="{{ $id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
</select>
</div>
</div>
I read a lot of posts about this problem, but none of them is working for me.
You can try this
in controller's store function
$domain_id = Domain::all('nameDomain', 'id');
$owner_id = Owner::all('nameOwner', 'id');
In you view
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $id => $domain)
<option value="{{ $domain->id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
and for second select option
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $id => $owner)
<option value="{{ $owner->id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
You was not fetching id of your object. The $id you were fetching will just give you the index.
This should work :)
Change
#foreach($owners as $id => $owner)
<option value="{{ $id }}">{{ $owner->nameOwner }}</option>
#endforeach
to
#foreach($owners as $owner)
<option value={{ $owner->id }}>{{ $owner->nameOwner}</option>
#endforeach
And repeat with the other foreach loop.
If that doesn't work, in your controller add dd($request); to see what info is being passed to it and share with us, please.
Seems close but a few tweaks are needed. Also check your Project model and see if there is a $fillable array. If there is you'll need all the fields in your validate call in it e.g.
protected $fillable= [
'title'
'code'
'domain_id'
'owner_id'
];
Remove the calls in your store function.
public function create()
{
$domains = Domain::all('nameDomaine', 'id');
$owners = Owner::all('nameOwner', 'id');
return view('projectss.create', compact('domaines', 'owners'));
}
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'code' => 'required',
'domain_id' => 'required',
'owner_id' => 'required',
]);
Project::create($request->all());
return redirect()->route('projects.index')
->with('success', 'Project created successfully');
}
Make sure you're adding the right ID.
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> Domain : </strong>
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $domain)
<option value="{{ $domain->id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> owner : </strong>
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $owner)
<option value="{{ $owner->id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
</select>
</div>
</div>

post request not working Laravel 5

I am trying to submit a form using post method, but it does not seem to be working. I have enabled error debug on but still no error is shown. After submitting the form the same page is loaded without any errors.
This is my route
Route::post('/home' , ['as' => 'store-post' , 'uses'=>'FacebookControllers\PostsController#save']);
And my form is
{!! Form::open(['route'=>'store-post' , 'class'=>'form'])!!}
<div class="form-group">
<label for="textarea"></label>
<textarea class="form-control" rows="5" name="textarea">What's on your mind?</textarea>
</div>
<div class="form-group col-sm-12">
<input type="submit" value="Post" class="btn btn-primary pull-right">
</div
{!! Form::close() !!}
This is my Controller
class PostsController extends Controller
{
public function save(PostsRequests $request){
$input = $request->all();
$post = new Post();
$post->user_id = Auth::user()->id;
$post->content =$input;
$post->user()->associate(1);
$post->save();
/*return redirect('home');*/
return ('something');
}
}
After hours of searching and trying, finally, I found the solution. I was using my own request class and path was not correct so I corrected the path of PostsRequests and now it works.