I have a Form that contains a Dropdown and a Submit button. Like so:
View path: webmasters/filters.blade.php
{{ Form::open() }}
{{ Form::select('filt', $Dropdown, 2) }}
{{ Form::Submit('Filter') }}
{{ Form::close() }}
{{ Form::close() }}
And a controller that populates the Dropdown with values queried from a DB. Like so:
Controller name: WebController.php
class WebController extends BaseController {
public function getFilters() {
$filters = Dropdown::getFilters();
return View::make('webmasters.filter',['Dropdown'=>$filters]);
}
Here is my route:
Route::resource('topPage', 'WebController#getFilters');
getFilters is a model method that queries the DB for the values that come into the dropdown.
I would like to call a controller method upon submitting the form so that the said method queries another DB and returns a table (on the same page as the dropdown and submit button) based on the selected value of the dropdown.
I'm probably going about this the wrong way, so i'd really appreciate if anybody gave me a headsup. I would still prefer not to use Ajax on this because i dont know my way around it.
Thanks for your help.
Since the default form created by Form::open() is using POST you can just add another method in your controller and are all set
public function postFilters() {
$filt = Input::get('filt'); // getting the value of the select
}
Related
I am having a real issue getting a button to scan and clear out an entire column using an eloquent model. I have two columns in my SQLite DB, "States" and "Totals"... I want the States to stay in their own order, but I want the totals to be cleared out upon the user selecting a button. The character type for 'totals' is BigInt... After the user selects the button, I want them redirected to the home page (with the values cleared so they can start over).
Here are my routes:
Route::resource('states', 'StateController');
Route::get('/', 'StateController#index');
Route::post('create', 'StateController#store');
Route::post('states.update', 'StateController#update');
Here is my controller:
public function update()
{
Distributors::update(['total' => null]);
return View::make('states');
}
Here is my form:
{{ Form::open(['route' => 'states.update']) }}
{{ Form::submit('Destroy and Start Anew') }}
{{ Form::close() }}
The error I get is:
MethodNotAllowedHttpException
Is there a simple issue with my routes? I can't figure it out.
You did not specify a method attribute on your form, so it will automatically execute a GET request. Your state.update route is only setup to accept POST requests.
Change your form to this:
{{ Form::open(['route' => 'states.update', 'method' => 'post']) }}
Please delete
Route::resource('states', 'StateController');
in your route and try again.
I'm trying to populate a form with my logged in user so I can edit some profile information.
I currently have this code (only trying to display the email first, will populate the form later once this works):
I made an editprofile.blade.php template with:
#extends('masterpage')
#section('content')
<h1> Edit Profile </h1>
{{ Form::model($user) }}
<div class="form-group">
{{ Form::label('email', 'E-mail') }}
{{ Form::Text('email', null, ['class' => 'form-control']) }}
</div>
{{ Form::close() }}
#stop
In myProfileController.php I have:
<?php namespace App\Http\Controllers;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Html\FormFacade;
use Illuminate\Html\HtmlFacade;
class ProfileController extends Controller {
public function show() {
$user = Auth::user();
return view('pages.editprofile')->withUser($user);
}
}
In my route I added:
Route::get('/profile/edit', 'ProfileController#show');
But when I try to load the page I get this:
I have two questions:
1: Why isn't there a form showing up with the data? As you can see the email is being loaded into the form but it displays it as text.
2. Once the data is loaded into a succesful form and the user has edited some of the data, how would I make update function to save the edited data?
I think that you are just outputting the raw data because probably you are using double brackets {{ }}.
As i remember i had a same thing when i migrated a site from laravel 4 to laravel 5. They changed the way blade output and echo the data. try to change double brackets {{ }} to this {{!! !!}} or this {{{ }}} i don't remember exactly which one it is.
Edit: about saving you can use controllers so when user post the data it goes to something like the same url just with post method, so it calls the update function inside controller.
For some functions where user should have full control over his profile like edit, update, delete, create i would use the examples from this http://laravel.com/docs/5.0/controllers#restful-resource-controllers take a look at restful controller
I want to implement a text input field via Blade to my view in Laravel 4.
{{ Form::open(array("route" => "search.show")) }}
{{ Form::text("name") }}
{{ Form::close() }}
The text input posts a string to my controller, that is used to select some data from my database via
class SearchController extends \BaseController {
public function show($name) {
return(Item::where("name", "like", "%" . $name . "%")->get());
}
}
When I submit the form with the text input, I want to hit the search.show Route, that I implemented with
Route::resource("search", "SearchController");
Now I want to know, how to post the text to my routes.php, that my controller is able to get the input by the parameter of the show method?
I know I could enter the data via Input::get("name"), but isn't it a design failure, when Router::resource gives me the template for this?
Sorry for my bad english, but I hope you guys could help me out.
Thank you.
You're specific need isn't answered by the Resource controller layout. A search in general will have a number of different facets beyond a keyword, although this isn't always true.
The best approach I can offer you would be to avoid using any of the pre-existing Resource Controller routes as you may need them for what they are actually for in the future.
You can add a search method to your Resource controller when you declare the routes.
Route::get('search', [
'uses' => 'SearchController#search',
'as' => 'search'
]);
Route::resource('SearchController');
However, as a resource controller is not a good solution for this. Use a regular controller. Just remove the resource controller call in routes.php and only implement the search method.
public function search()
{
// Your code here
}
The advantages to this are that you take back full control over the naming of the route, the HTTP verb used (GET makes more sense for a search) and you don't pollute a resource controllers templated routes with functionality that doesn't belong.
The second part is how to pass variables. Searching is typically difficult using parameters passed in as separated arguments in the url. Using a GET request just use a query string.
http://myapp.com/search?keyword=search%20%keyword
Now just use the Input facade or dependency inject the Request class into the controller.
$keyword = Input::get('keyword');
if you using form better not using function which hash variable like show($id) function, because url should be like this .../search/show/{$id}, try use store() function.
public function store()
{
$name = Input::get("name");
/* your code */
}
==
{{ Form::open(array('route' => "search.store")) }}
{{ Form::text("name") }}
{{ Form::close() }}
You can inject a Request object to show method of the controller.
class SearchController extends \BaseController {
public function show(Request $request) {
$name = $request->input("name");
}
}
Or you can do form model binding : http://scotch.io/quick-tips/php-tips/laravel-php/laravel-form-model-binding.
On Laravel 4.x you have to use the Input Facade
class SearchController extends \BaseController {
public function show(Request $request) {
$name = Input::get("name");
}
}
On a dashboard page, I've created a select list in a form that lists the names of components; the value that's passed from the select list is obviously the component id. On pressing submit, the user is routed to a page that displays the data about that component. Should be dirt simple...
Controller:
public function showDashboard()
{
$components = Component::lists('name','id'); ...
return View::make('dashboard', array('components'=>$components, ...))
}
dashboard.blade.php:
{{ Form::open(array('route' => array('components.show', $components->id), 'method'=>'get')) }}
{{ Form::Label('id','Component:') }}
{{ Form::select('id', $components) }}
{{ Form::submit('Show Component', array('class'=>'button')) }}
{{ Form::close() }}
I've tried various ways of doing this, and get a different error every time. The above code doesn't even let me display the dashboard page -- I get a "Trying to get property of non-object" error. Clearly, it's not liking $components because that was passed as a list array and not an object. As I said, I'm sure this is dirt simple, I just can't figure out the proper syntax, and Laravel docs aren't giving me the answer. Thanks!
The problem isn't the dropdown, or the lists method, but rather in your form opening. Here, you have $components->id as an argument to the route, but $components is an array and you can't access an id property on it.
Finally figured this out. I had posted a similar question here subsequent to this one, and rather than repeat the answer, it is here:
How to pass id value from select list to controller when controller expects an object? laravel-4
The very short version: change Route::get to Route::post. Details with code in the link above. Problem solved!
All the first three methods of all my controller work well, but when i add a fourth one this one doesn't work (the construct method not included) and give me a blank page with the url of the controller action.
my controller class:
class StoreController extends BaseController{
public function __construct(){
parent::__construct();
$this->beforeFilter('csrf', ['on'=>'post']);
}
public function getIndex(){
return View::make('store.index', ['products'=>Product::take(4)->orderBy('created_at','DESC')->get()]);
}
public function getView($id){
return View::make('store.view', ['product'=>Product::find($id)]);
}
public function getCategory($cat_id){
return View::make('store.category', [
'products'=>Product::where('category_id','=',$cat_id)->paginate(6),
'category'=>Category::find($cat_id)]);
}
public function getSearch(){
$keyword=Input::get('keyword');
return View::make('store.search', [
'products'=>Product::where('title','LIKE','%'.$keyword.'%')->get(),
'keyword'=>$keyword]);
}
}
In my route.php file:
Route::controller('store', 'Storecontroller');
And the triggerer form of the action is:
<div id="search-form">
{{ Form::open(['url'=>'store/search', 'method'=>'get']) }}
{{ Form::text('keyword', null, ['placeholder'=>'Search by keyword', 'class'=>'search']) }}
{{ Form::submit('Search', ['class'=>'search submit']) }}
{{ Form::close() }}
as I said the getSearch method doesn't work and I'm given a blank page with the url of the action (not the returned view)
thanks
As the page is blank, make sure that debug is set to true in your app config (should be the topmost setting, preferably in the local dir). This means that when an error occurs you will be shown an error page with a detailed stack trace and error message, making it easier for you to debug your app.
Make sure that the view store.search exists, is named correctly (check for typos) and contains the html/php you need to display the $products.
Next, you have two possibilities:
Set a default value for the keyword input
// For example:
Input::get('keyword', 'default')
Check if there is a keyword specified
// For example:
if (Input::has('keyword')) {...} else {...}
As a side note: You should not perform (heavy) tasks inside of arrays like you do here. Put them inside variables and include them into the view array like you did with the keyword variable. Your code will also be more readable and maintainable. There's a lot more about this, but thats of the point here.
Let me know if this helped you.