Laravel Form Class Submit Value as Part of Array - laravel-4

When using the Form helper class with Laravel / Blade, is there a way to format the $name value such that when the data is retrieved, it is organized as an array?
For example, in the view file, I want do something like:
{{ Form::input('params.sitename') }}
{{ Form::input('params.siteurl') }}
And then in the controller, when I call:
$data = Input::get('params');
$data will return:
array(
'params' => array(
'sitename' => 'data input from user',
'siteurl' => 'more data input from user',
)
)

Good ol' php way:
{{ Form::input('params[sitename]') }}
{{ Form::input('params[siteurl]') }}
Input::get('params');
// array(
// 'sitename' => 'value-1',
// 'second' => 'value-2'
// )

Related

laravel: using parameters from routes in blade forms

This is my add.blade.php.
{{ Form::open(array('url' => $id."/item", 'method' => 'post', 'files' => 'true', 'id'=>'add')) }}
{{ Form::text('title'}}
{{Form::submit('Submit')}}
{{Form::close()}}
This is my web.php
Route::resource('{categoryId}/item', 'ItemController');
This is my ItemController.php
public function create($categoryId){
return view('item.add', array('id' => $categoryId));
}
I am trying to add new item to a category. So I click add new and it opens add.blade.php. When I submit from add.blade.php, it is redirecting it to item/create. I think it is because of url in the form of add.blade.php. What is the correct way of doing this? Thanks in advance
I think you should try this:
{{Form::open(['route' => ['item.create', $id],'method' => 'post', 'files' => 'true', 'id'=>'add'])}}
You can try prefix instead of append parameter
Route::group(['prefix' => '{categoryId}'], function()
Route::resource('item', 'ItemController');
});
then use
route('item.create',['1']);
Here in your code just pass parameter and will work like this:
['route' => ['item.create', $id]
your View
{{ Form::open(array('url' => "/item/".$id , 'method' => 'post', 'files' => 'true', 'id'=>'add')) }}
{{ Form::text('title'}}
{{Form::submit('Submit')}}
{{Form::close()}}
your Route
Route::resource('item', 'ItemController');
Route::post('/item/{categoryId}', 'ItemController#create');
your Controller
public function create($categoryId){
$id = $categoryId;
return view('item.add', compact('id'));
}

Update / post database colum in Laravel

I have a general question.
I have a search form in larvel which returns results form the database.
in these i have an input field to enter a price if price is == 0
what my problem is when i enter price and submit it returns to the search page without my previous search results i.e it doesn't refresh the same page with results and the newly updated field etc.
form in view
{{ Form::open(['action' => 'price_input'])->with($gyms) }}
{{ Form::text('enter_price', null, ['class' => 'form-control', 'size' => '50', 'id' => 'enter_price', 'autocomplete' => 'on', 'runat' => 'server', 'required' => 'required', 'placeholder' => 'enter price!', 'style' => 'margin-bottom: 0px!important;']) }}
{{ Form::submit('Search', ['class' => 'btn btn- primary', 'style' => 'margin-left: 10px;']) }}
{{ Form::close() }}
route
Route::post('/', [ //not used yet
'as' => 'price_input',
'uses' => 'PagesController#priceUpdate'
]);
Model
public function priceUpdate($gyms)
{
if (Input::has('enter_price'))
{
$price = Input::get('enter_price');
Gym::updatePrice($price);
return Redirect::back()->withInput();
}
Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gym);
}
not bothering with model as that works fine.
any ideas guys?
Thanks for your answer,
i have changed my controller to this
public function priceUpdate($gyms)
{
if (Input::has('enter_price'))
{
$price = Input::get('enter_price');
Gym::updatePrice($price);
$gyms = Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gyms);
}
$gyms = Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gyms);
}
but when i run it i get
Missing argument 1 for PagesController::priceUpdate()
with the $gyms being passed into the method.
if i take out the $gyms that goes away but not sure if its still being passed with session or not, sorry im a novice.
orignally i had a search box which when run returns
return View::make('pages.home')->with($data);
what is the difference between that and
return View::make('pages.home')->with($data);
when i do the above line it returns to the search page with no search options from before update the form, any ideas?
Currently, you are just retrieving an existing session and doing nothing with it. You need to do:
$gyms = Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gyms);
Or
return Redirect::to('pages.home')->with('gyms', Session::get('gyms'));
Then you can access the gyms in the view with $gyms.
Alternatively, you could access Session::get('gyms') in the view as well.
Also, not sure if it's just the way you pasted it here, but you have an unnecessary space before the ->with. Just wanted to make sure that's not part of the issue, too!

How to include Laravel Controller file like blade

First my master.blade file include menu bar using this
Include Blade file menu.blade.php
#include('menu')
But Finally I realize to send some data from db to menubar, then I create controller, Controller name is MenuController, then I create route "admin-menu". Now I want to include that link to my master blade. how to do that thank you,
To pass data to a view from either a route closure or a controller you do one of the following:
$now = \Carbon\Carbon::now();
View::make('my-view', ['name' => 'Alex', 'date' => $now]); // pass data into View:::make()
View::make('my-view')->with(['name' => 'Alex', 'date' => $now]); // pass data into View#with()
View::make('my-view')->withName('Alex')->withDate($now); // use fluent View#with()
So you'd just use them in the View::make() call as you presumably already are:
// in a route closure
Route::get('some-route', function () {
return View::make('menu', ['name' => 'Alex']);
});
// in a controller
public function someRoute()
{
return View::make('menu', ['name' => 'Alex']);
}
Interestingly, in a lot of frameworks/templating systems, if you wanted to include a partial, you'd pass the data you want to be available in that partial in the partial call, but Laravel doesn't quite do this. For example in a made-up system you may have something like this:
// in controller:
$this->render('home', ['name' => 'Alex', 'age' => 30]);
// home.php
<?php echo $name; ?>
<?php echo $this->partial('home-age', ['age' => $age]); ?>
// home-age.php
<?php echo $age; ?>
But in Laravel, all current view variables are automatically included into partials for you. Now I tend to like to specify the variables anyway (Blade does allow you to do this as above), and obviously it can be used to override a view variable:
// route:
return View::make('home', ['name' => 'Alex', 'age' => 30, 'gender' => 'male']);
// home.blade.php
{{ $name }}
#include('home-extra', ['age' => 20])
// home-extra.blade.php
{{ $age }}
{{ $gender }}
The above code would output:
Alex
20
male
So the age is overridden in the #include, but the un-overridden gender is just passed along. Hopefully that makes sense.

check if value already exists in db

I have an insert form and a dropdownbox which displays all cars names and when selected one it saves the id in column "car_id" which is unique. What I want is to check if this id already exists and if yes to display a validation message:
create controller
public function create() {
$cars = DB::table('cars')->orderBy('Description', 'asc')->distinct()->lists('Description', 'id');
return View::make('pages.insur_docs_create', array(
'cars' => $cars
));
}
insur_docs_blade.php
<div class="form-group">
{{ Form::label('car', 'Car', array('class'=>'control-label col-lg-4')) }}
<div class="col-lg-8">
{{ Form::select('car', $cars, Input::old('class'), array(
'data-validation' => 'required',
'data-validation-error-msg' => 'You did not enter a valid car',
'class' => 'form-control'))
}}
</div>
</div>
You can use Laravel's Validator class for this. These are a few snippits of how it works. This methods works by using your data model. Instead of writing everything out I added a few links that provide you all the information to complete your validation.
$data = Input::all();
$rules = array(
'car_id' => 'unique'
);
$validator = Validator::make($data, $rules);
if ($validator->passes()) {
return 'Data was saved.';
}
http://laravelbook.com/laravel-input-validation
http://laravel.com/docs/validation
http://daylerees.com/codebright/validation
You can use Laravel's "exists" method like this:
if (User::where('email', $email)->exists()) {
// that email already exists in the users table
}

Laravel 4: pointing a form to a controller function

I can't understand, how to set up the Form action to direct to a function of a specific controller.
This is my blade code:
{{ Form::open(array('route'=>'user.search')) }}
But I get this error :
Unable to generate a URL for the named route "user.search" as such route does not exist.
the controller (UserController) has a function with this prototype
public function search(){ ... }
I have also tried to set up a route like this in route.php
Route::post('user/search', 'UserController#search');
What is wrong with this code?
You can do it like
{{ Form::open( array('url' => URL::to('user/search')) ) }}
Because you don't have a name for the route. To define a name for the route, use following syntax,
Route::post('user/search', array( 'as' => 'userSearch', 'uses' => 'UserController#search' ));
So, you can use the route by it's name, as
{{ Form::open( array('route' => 'userSearch') ) }} // 'search' method will be invoked
Also, you can directly use the action of a controller as
{{ Form::open( array('action' => 'UserController#search') ) }}
Check Routing and Form.

Resources