I've created a button that will reset all the values inside the checkboxes. This happens thanks to a method that performs an update thanks to an ID.
View:
<div>
{!! Form::open(['method' => 'PUT','route' => ['checklists.updateRichiesta', $richiesta->id]]) !!}
<button type="submit" onclick="return ConfirmDelete();" class="#">
<i class="fa fa-times"></i>
</button>
{!! Form::close() !!}
</div>
ChecklistController:
public function updateRichiesta(Request $request, $id)
{
\App\Checklist::where('richiesta_id', $id)->update(['isCheck' => 'false']);
\App\Richiesta::where('id', $id)->update(['stato' => 1]);
return Redirect::back();
}
Route.php
Route::resource('checklists', 'ChecklistController');
Error:
Route [checklists.updateRichiesta] not defined.
How can i resolve this problem?
When you use resource route, the name of update route is update. So, change the route name to:
'route' => ['checklists.update', ...
And rename controller method to update:
public function update(Request $request, $id)
https://laravel.com/docs/5.5/controllers#resource-controllers
Either edit your form opening tag like this:
{!! Form::open(['method' => 'PUT','route' => ['checklists.update', $richiesta->id]]) !!}
or add updateRichiesta method to your controller
As it is a resource you must used the resource syntax to defined routes.
https://laravel.com/docs/5.5/controllers#resource-controllers
In this case you would call the route by:
{!! Form::open(['method' => 'PUT','route' => ['checklists.update', $richiesta->id]]) !!}
Related
I am currently building a feature where a user can select an option from a select box, once an item is selected you can click a button which hits my endpoint which submits and stores it.
However if i dont select anything but then click the button it just hits a 404 page on the same endpoint.
Blade
Below contains the blade syntax for the select box and button.
<div class="row align-items-center">
<div class="col-md">
<small class="text-success">
please select one of the teams below to store your preference.
</small>
{!! Form::open(['route' => ['team.create.link', $team->id],'method' => 'post', 'class' => 'needs-validation','novalidate', null]) !!}
{!! Form::select('teams[]', $teams, '', ['class' => 'custom-select', 'multiple'], ['required']) !!}
<button class="btn btn-primary btn-sm mt-3 float-right">
<i class="fas fa-fw fa-plus-circle mr-2"></i>
Add</button>
{!! Form::close() !!}
</div>
</div>
Method
Below is the method used for storing the new input within the pivot table.
public function link(string $teamId)
{
$team= Team::findOrFail($teamId);
$links = Input::get('teams');
$link = Team::findOrFail($links);
$team->links()->attach($link);
session()->flash('success', 'Link Added.');
return back();
}
Help
How would i modify this so that the button cant be clicked and returns a required error if an option isnt selected? i've tried adding ['required'] to the form::select but i had no luck with that.
Can anyone push me in the right direction?
You can validate your Reqeust with $this->validate
use Illuminate\Http\Request;
public function link(Request $reqeust, string $teamId)
{
$request->validate([
'teams' => 'required',
]);
$team = Team::findOrFail($teamId);
$links = Input::get('teams');
$link = Team::findOrFail($links);
$team->links()->attach($link);
session()->flash('success', 'Link Added.');
return back();
}
Take a look at the official Laravel Documentation for Validation
In an attempt to program a search bar, I created a GET method and added a new controller where it gets the relevant data and returns it with the view.
//This is the form in the view named "index.blade.php"
{!! Form::open(['action' => 'SearchesController#search', 'method' => 'GET']) !!}
<form class="form-inline md-form mr-auto mb-4">
{{Form::text('search', '', ['class'=>'form-control', 'placeholder'=>'Search Anything'])}}
<button class="btn aqua-gradient btn-rounded btn-sm my-0" type="submit">Search</button>
</form>
{!! Form::close() !!}
//---------------------------------------------------------
//This is in the "web.php"
Route::get('/posts/search', 'SearchesController#search');
//---------------------------------------------------------
//The following code is the controller named "SearchesController.php"
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Post;
use DB;
use Illuminate\Support\Facades\Auth;
class SearchesController extends Controller
{
public function search(Request $request)
{
$this->validate($request, [
'search' => 'required',
]);
$search = metaphone($request->input('search'));
$posts = Post::where('sounds_like','LIKE',"%{$search}%")
->orderBy('title', 'desc')
->paginate(10)
->get();
return view('posts.index')->with('posts',$posts);
}
}
I expected the output of a view with all the data returned with the view, but instead got the error message :
"Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in /Applications/AMPPS/www/lsapp/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php on line 23 and at least 1 expected"
You desn't need a get() if you are using paginate(). Paginate will execute the query too and will create a collection, and the get() method will be executed as an instance of this get() and will require the key parameter . So you can remove it:
public function search(Request $request)
{
// ...
$posts = Post::where('sounds_like','LIKE','%'. $search . '%')
->orderBy('title', 'desc')
->paginate(10);
return view('posts.index')->with('posts',$posts);
}
Not related to the error, but also you have a form inside a form. Choose one and remove the other:
{!! Form::open(['action' => 'SearchesController#search', 'method' => 'GET', 'class'=>'form-inline md-form mr-auto mb-4']) !!}
{{Form::text('search', '', ['class'=>'form-control', 'placeholder'=>'Search Anything'])}}
<button class="btn aqua-gradient btn-rounded btn-sm my-0" type="submit">Search</button>
{!! Form::close() !!}
here you can see my form where I put in a username and have a hidden idgroup field.
{!! Form::open(array('route'=>'create.invitation')) !!}
<div class="form-group">
{{Form::label('username', 'Username')}}
{{Form::text('username', '', ['class' => 'form-control', 'placeholder' => 'Enter Username'])}}
<input type="hidden" name="idgroup" value="{{$group}}"/>
{{ csrf_field() }}
</div>
<div>
{{Form::submit('Submit',['class' => 'btn btn-primary'])}}
<a class="btn btn-default btn-close" href="{{ route('home') }}">Cancel</a>
</div>
{!! Form::close() !!}
After that this route leads me to my controller function
Route::post('invitation/show', 'InvitationController#create')->name('create.invitation');
How can I add the username and the idgroup to my url?
My problem is now when I click submit I get back this url http://127.0.0.1:8000/invitation/create and when I click enter to the url line I get an error no message because no parameter will pass to the function.
Add. Here is the function
public function create(Request $request)
{
$request->validate([
'username' => [
'required', 'alpha_num', new ExistingUser, new UserNotAdmin
]
]);
$username = $request->username;
$iduser = User::where('name', $username)->select('id')->first();
$group = $request->idgroup;
return view('invitation.overview')->with('group', $group)->with('iduser', $iduser);
}
You cannot pass parameter inside POST body without submitting a form.
But you can try to allow both GET or POST by using any() for the route, so you can test the page around.
Route::any('invitation/show', 'InvitationController#create')->name('create.invitation');
And then, you can try pass variable through queries inside URL
http://127.0.0.1:8000/invitation/create?username=something&idgroup=1
Missing required parameters for [Route: templates.answers.store] [URI: templates/{template}/answers]. (View: D:\Applications\xampp\htdocs\clientpad\resources\views\templates\answers.blade.php)
I am having the above error when I try and use a form with my foreach loop. I am not even sure why this is happening, maybe because I am new at Laravel. But this error goes away once I get rid of the AnswerController#store from the Eloquent form. It is possible I am doing this whole form wrong.
Here is what I want to do: A user made a template with questions, on the click of use button which goes to this url: http://clientpad.test/templates/{id}/answers they see their made questions which are shown with a foreach loop. Around it a Form is made so a user can answer the questions made. The form and answer field shows when I delete the action AnswerController#store, otherwise I get the above error.
Here is the code:
AnswerController:
public function index(Template $template, Question $question)
{
$questions = $template->questions->mapWithKeys(function($question){
return [$question->id => $question->question];
});
return view('templates.answers')->with('template', $template)->with('questions',$questions);
}
public function store(Request $request, Question $question, Answer $answer)
{
$answers = new Answer;
$answers->answer = $request->input('answer');
$answers->question_id = $request->input('question_id'); //current template id
$question->answers()->save($answers);
dd($question);
return redirect('/dashboard')->with('success', 'Your Question Was Successfully Created');
}
answers.blade.php
{!! Form::open(['action' => 'AnswersController#store', 'method' => 'POST']) !!}
#foreach ($questions as $question) <!-- Index counts the questions shown -->
<div class="panel panel-default">
<div class="panel-body">
<p class="pull-left question2"> {{$question}}</p>
<div class="form-group answer">
{{Form::label('', '')}}
{{Form::text('answer', '', ['class' => 'form-control', 'placeholder' => 'Type in your answer'])}}
</div>
</div>
</div>
#endforeach
<hr>
{{Form::submit('Save', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
And I am just using the resource in routes.
Your are calling a route templates/{id}/answers in your Blade view that is missing the {id} parameter. Reading the error thoroughly will help you understand.
Instead of writing:
Form::open(['action' => 'AnswersController#store', 'method' => 'POST'])
You write:
Form::open(['action' => ['AnswersController#store', $template_id], 'method' => 'POST'])
The $template_id will fill the {id} in your route URL templates/{id}/answers.
I'm new in laravel and I want to make a form to edit data from my database so the user just have to change some fields, I would be glad if somebody could give me an example.
I'm using laravel 5.5 and mysql
Please check out this edit example in laravel -
In Routes
Route::get('PartnerType/edit/{id}', 'PartnerTypeController#edit');
Route::post('PartnerType/update', 'PartnerTypeController#update');
In controller,
public function edit($id){
$data['propertyType'] = PropertyType::where('id', $id)->first();
return view('propertyType.edit', $data);
}
public function update(Request $request){
//Validate user inputs
$validator = \Validator::make($request->all(), ['name' => 'required']);
//Check whether validation is failed or passed
if($validator->fails()){
//Redirect back with validation errors
return redirect()->back()->withErrors($validator->errors())->withInput();
}
//Save Details
$propertyType = PartnerType::where('id', $request->id)->first();
$propertyType->name = $request->name;
$propertyType->save();
//Redirect with success message
return redirect()->to('manage/PartnerType/show')->with('success', 'PartnerType updated successfully');
}
In view ,
{!! Form::model($propertyType, array('url'=>array('manage/propertyType/update'), 'method' => 'POST', 'id' => 'edit_propertyType_form')) !!}
<div class="form-body pb0">
<div class="form-group">
<input type="hidden" name="id" value="{{$propertyType->id}}">
{!! Form::label('name', 'Name*') !!}
<div class="input-group">
{!! Form::text('name', $propertyType->name, array('class' => 'form-control','id' => 'name', 'placeholder' => 'Name')) !!}
</div>
</div>
<div class="status-label">
{!! Form::submit('Submit',array('class' => 'btn blue')) !!}
</div>
{!! Form::close() !!}
The question is kind of vague as what you intend to do. Have you tried something out? if so, what is it?
Let's start from the beginning:
As far as I can tell right now (need more details) you need:
A view file (example: edit.blade.php) which will be inside the folder resources/views and inside the folder you have to create with the proper name (example: /resources/views/something/edit.blade.php)
Then create the form where the user is going to edit the information from the database.
Go to your routes folder and web.php file to set the routes you will need in order to PUT/PATCH the new information from the form to de DB.
Do you have your database configured already?
Please send more information, and I will answer more in detail on what you need or you can go to the official documentation for further information as well.