I have a list of users, and for each user of the list there is a select box where the selected value role value is shown.
I was thinking a way on how allow to send all the modifications for each user with the submit button to a method of the controller which should update the role. I have no idea.
This is the form, which works correctly:
{!!Form::open(['url' => ['admin/users/edit'], 'class' => 'form-horizontal', 'method' => 'POST']) !!}
#foreach($users as $user)
<div class="form-group">
<div class="list-user-element">{!! $user->name !!}</div>
<div class="list-user-email">{!! $user->email !!}</div>
<div>
{!! Form::select('role', $roles, $user->role->label, ['class' => 'form-control']); !!}
</div>
</div>
#endforeach
<div class="form-group">
<div class="col-sm-offset-2 col-sm-5">
{!! Form::submit('Update', array('class' => 'btn btn-warning')) !!}
</div>
</div>
{!! Form::close() !!}
I don't know how to prepare the controller, neither which kind of info is passed to the controller.
Route::post('admin/users/edit', 'AdminController#showRegisteredUsersRoles');
And here it gives error:
public function editRegisteredUsersRoles(Request $request)
{
dd($request);
}
You could do the form like this
{!! Form::select('users[' . $user->id . '].role', $roles, $user->role->label, ['class' => 'form-control']); !!}
And then in the controller
public function editRegisteredUsersRoles(Request $request)
{
foreach ($request->users as $user_id => $role) {
//...
}
}
I'm not 100% sure of what your problem is, are you saying that there aren't all of the users' roles in the Request when you dd($request) ?
I think the problem lies in the fact that you are looping through the users throwing down select boxes with the same name. Generally, when you send a POST request, you access the information by saying $_POST[<Form's name>], so maybe there is a problem with duplication here. Perhaps you should try something like:
{!! Form::select('role' . $user->id, $roles, $user->role->label, ['class' => 'form-control']); !!}
What I changed above is simply tacking on the user's id to the name 'role' in order to make each of those unique.
Related
As in title how to do that? I have table comments, the structure of this table is:
-id
-title
-contents
-user_id
Here is code of my form:
{!! Form::open(['action' => ['LibraryController#newcomment', $sth->myuserid]]) !!}
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null) !!}
{!! Form::label('contents', 'Contents:') !!}
{!! Form::textarea('contents', null) !!}
{!! Form::submit('Send') !!}
{!! Form::close() !!}
So, comment has the user_id and I have to send it too with form, maybee by this:$sth->myuserid but how can I use it in LibraryController?
Additionally, I found this: http://laravel-recipes.com/recipes/157/creating-a-hidden-input-field is it a good option?
You could use a hidden field to store the user id and then retrieve it in your controller:
{{ Form::hidden('user_id', $user_id) }}
But if you are using Auth component you can get the authenticated user by using the Auth facade:
use Illuminate\Support\Facades\Auth;
...
// Get the currently authenticated user's ID...
$id = Auth::id();
If it's for admin panel or something, you can use hidden input:
{!! Form::hidden('user_id', $user->id) !!}
But if you want to pass an ID of a current user, do not pass it through a form. Use auth()->id() in a controller to get ID.
Okay, but what if it isn't auth but something else and I want use it:['LibraryController#sth', $sth->sth_id], how to use it in LibraryController ?
When I submit my form I receive the following error: 'MethodNotAllowedHttpException in RouteCollection.php line 219'.
How do I solve this? I have looked at various forums and other threads where this has been answered but none have worked for me.
My route file is as follows:
<?php
/*
This is the file in which the rules for how users will use the application are kept
*/
Route::get('/',function() {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController#index');
Route::resource('/questionnaires', 'QuestionnairesController');
Route::resource('/questions', 'QuestionsController');
Route::resource('/answers', 'AnswersController');
\
My create.blade.php file along with the form looks like this:
#extends('layouts.master')
#section('title', 'Create Questionnaire | SurveySays!')
#section('content')
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h1>Create Questionnaire</h1>
<h3>Create your questionnaire using the form below. Give it a title, a small description and write your ethical considerations:</h3>
#if($errors->any())
<div class="alert alert-danger">
#foreach($errors->all() as $error)
<p>{{ $error }}</p>
#endforeach
</div>
#endif
{!! Form::open(array('url' => '/questionnaires/create')) !!}
<div class="container-fluid">
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title',null,['id' => 'title','class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('description', 'Description:') !!}
{!! Form::textarea('description',null,['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('ethics', 'Ethical considerations:') !!}
{!! Form::textarea('ethics',null,['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Create', array('class' => 'btn btn-success form-control')) !!}
</div>
</div>
</div>
{!! Form::close() !!}
#endsection
Thanks :)
You should use store method instead. Also, use route, not URL (you don't want to hardcode it):
{!! Form::open(array('route' => 'questionnaires.store') !!}
create method is used to display form to a user. And store used to validate and persist user input data into DB.
More about RESTful resource controllers here.
I see that you didn't setup any POST routes, that means that you cannot send a HTTP POST request to your Laravel site.
If you add this, it will be fine.
Route::post('/questionnaires/create', 'QuestionnairesController#create');
This will setup a route (HTTP POST request) to the QuestionnairesController with the function create().
Hope this works!
Because create only allows GET requests to show the form.
https://laravel.com/docs/5.2/controllers#restful-resource-controllers
Add 'method' => 'post' to your Form::open() array and point it at just /questionnaires.
Any idea how to solve the following approach.
I have a form and want to display the entered data in a specific formtemplate before store it in the DB. If the entered data looks properly, the user can save the form.So I am searching for a way to display the entered data as a preview in a new window/tab first. With my code below I am not able to preview the form without saving the data in the databse. Also display the preview in a new window or tab is not possible. I guess there is no way to achieve this with php / laravel. I tried some onlick events, but no luck. As it seems like the route is prefered.
Any idea how to solve this?
My form looks like:
{{ Form::open(array('url' => 'backend/menubuilder/'.$id, 'method' => 'PUT'))}}
<section>
<div class="container">
<div class="row">
<div class="inputBox">
<div class="col-xs-12 col-md-6">
<h3>Montag</h3>
<div class="form-group">
{{Form::label('gericht_1_mo','Gericht 1')}}
{{Form::textarea('gericht_1_mo', Auth::user()->gericht_1_mo,array('class' => 'form-control'))}}
</div>
<div class="form-group">
{{Form::label('preis_1_mo','Preis', array('class' => 'col-md-6'))}}
{{Form::text('preis_1_mo', Auth::user()->preis_1_mo, array('class' => 'col-md-6'))}}
</div>
<div class="form-group mrgT55">
{{Form::label('gericht_2_mo','Gericht 2')}}
{{Form::textarea('gericht_2_mo', Auth::user()->gericht_2_mo,array('class' => 'form-control'))}}
</div>
<div class="form-group">
{{Form::label('preis_2_mo','Preis', array('class' => 'col-md-6'))}}
{{Form::text('preis_2_mo', Auth::user()->preis_2_mo, array('class' => 'col-md-6'))}}
</div>
</div>
</div>
</div>
</div>
</div>
{{Form::submit('update')}}
{{-- <input type="submit" name="preview" value="preview"> --}}
{{Form::close()}}
{{ Form::open(array('url' => 'backend/menubuilder/templatesview/'.$id, 'method' => 'POST'))}}
{{-- {{Form::submit('Preview',array('onClick' => 'target_blank'))}} --}}
<input onclick="newTab()" type="submit" name="preview" value="preview" >
{{Form::close()}}
My routes:
Route::get('backend/menubuilder/templates/{id}', 'MenuBuilderController#template');
Route::post('backend/menubuilder/templatesview/{id}', 'MenuBuilderController#preview');
My Controller:
public function preview($id)
{
$user = User::find($id);
$owner = (Auth::id() === (int) $id);
return View::make('backend/menubuilder/templatesview/tempone')->withUser($user)->withOwner($owner);
}
public function template($id)
{
$user = User::find($id);
$owner = (Auth::id() === (int) $id);
return View::make('backend/menubuilder/templates/tempone')->withUser($user)->withOwner($owner);
}
How do I view each individual record? Each record is a link, if clicked, it should display the record on the view page.
Route::get('question/{$id}', array('as'=>'question', 'uses'=>'QuestionController#show'));
THis is hte show method to display:
public function show($id = null)
{
return view('questions.view')
->with('title', 'Make it Snappy - Question')
->with('question', Question::find($id))
->with('users', Auth::user()->username);
}
This is the index.blade.php page
#extends('layouts.default')
#section('content')
<h1>Ask a Question</h1>
#if(Auth::check())
#if($errors->has())
<p>The following errors have occured:</p>
<ul id="form-errors">
{!! $errors->first('ask
', '<li>:message</li>') !!}
</ul>
#endif
{!! Form::open(array('url' => 'ask', 'method' => 'post')) !!}
{!! Form::token() !!}
<p>
{!! Form::label('question', 'Question') !!}<br />
{!! Form::text('question', Input::old('question')) !!} <br />
{!! Form::submit('Ask a Question') !!}
</p>
{!! Form::close() !!}
#else
<p>Please login to ask a question.</p>
#endif
<div id="questions">
<h2>Unsolved Questions</h2>
#if(!count($questions) > 0)
<p>No questions have been asked</p>
#else
<ul>
#foreach($questions as $question)
<li>{!! Html::linkRoute('question', str_limit($question->questions, 35), $question->id) !!} by
#if(count($users) > 0)
#foreach($users as $user)
#if($user->id === $question->userid)
{!! ucfirst($user->username) !!}
#endif
#endforeach
#endif
</li>
#endforeach
</ul>
{!! $questions->render() !!}
#endif
</div>
#stop
All i want is that when I click on any of the questions it should be displayed on the view page. I appreciate your support.
Route::get('question/{$id}', array('as'=>'question', 'uses'=>'QuestionController#show'));
your route accepts get while ur form method was post so it couldnt find get request so try making it get
{!! Form::open(array('url' => 'question', 'method' => 'post')) !!}
try it
{!! Form::open(array('url' => 'question', 'method' => 'get')) !!}
I have only seen how to open a Blade page with link, how do I use a button to do the same?
a button like this?
<input type="button"
onClick="parent.location='https://google.com'"
value="Google"
formtarget="_blank">
Using Form facade:
{!! Form::button('Google', [
'onClick' => "parent.location='https://google.com'",
'formtarget' => 'fromtarget'
])
!!}
And Using Form facade with url():
{!! Form::button('Delete account', [
'onClick' => "parent.location='" . url('delete/1') . "'",
'formtarget' => 'fromtarget'
])
!!}
You can do it with either a button or input element.
Here's an example with input:
<input type="submit" onClick="location.href = '{{ url('some/route') }}'">