Hi I send a form in my contact.blade.php. I read in order to use the PUT method you have to create a hidden input field which contains the method.
#if($do == 'edit')
{{ Form::model($contact, array('method' => 'PUT', 'route' => array('contact.update', $contact->id), 'id' => $do=='edit' ? $do.$contact->id : $do.$contact_type_id, 'form_id' => $do=='edit' ? $do.$contact->id : $do.$contact_type_id)) }}
{{ Form::hidden('_method', 'PUT') }}
#endif
....
{{ Form::submit('speichern', array('class' => 'btn btn-primary')) }}
</div>
{{ Form::close() }}
The route:
Route::put('/contact/{id}', array(
'uses' => 'ContactController#update',
'as' => 'contact.update'
));
The Controller:
public function update($id)
{
dd(Input::all());
// //get user account data
// $user = User::find( Auth::id() );
// // validate input
// $v = Contact::dataValidation( Input::all() );
return Redirect::Route('user.edit', 1)->withSuccess("<em>Hans</em> wurde gespeichert.");
Q1:
As soon as I call dd(Input::all()); I don't get redirected any more, instead I see a json with my form values.
Q2:
I'm just debugging this so I didn't program it. So my second question is:
From my understanding dd(Input::all()); gets all my form data. So don't I need to store it anyways somewhere?
Q1: dd() terminates the script, hence why you are not getting redirected. It's used as a tool to essentially break and examine what is going on.
http://laravel.com/docs/4.2/helpers
Q2: You will still need a model to feed the Input::all data into. Input::all simply fetches the submitted data, it doesn't do anything with it. It ultimately depends on your use case, sometimes you may want to email the data, but obviously most times you would what to store it against your persistence layer (read database / datastore)
Question 1
when you use DD, it will show the data and stop at that line.
DD
Dump the given variable and end execution of the script.
more information you can read it here DD in DD session.
Question 2
I'am not sure about 2nd question but if you want to get value from all input you could us Input::all();
more information All input in Getting All Input For The Request session
Related
From a blade template I want to pass an array with a variable amount of values to a route, as described in the answer here.
However when I do this I can only ever access the first value in the array I pass to the route.
This is how I call the route in the blade template:
{{ route('stats.downloads', ['stat_kind' => 'files_size', 'group_by' => 'week', 'start' => '2020-11-01', 'end' => '2020-11-10']) }}
this is my route from web.php:
Route::get('stats/downloads', 'StatsController#view_stats_downloads')->name('stats.downloads');
and my controller:
public function view_stats_downloads(Request $request){
// get the input parameters
$group_by = $request->get('group_by');
$stat_kind = $request->get('stat_kind');
$company = $request->get('group_by');
$user = $request->get('user');
$start = $request->get('start');
$end = $request->get('end');
...
The problem is, that I can only ever access the first value of the array I pass to the controller (stat_kind in this case). It doesn't natter in which order I call the get() function either.
What can I do to fix this?
I'm running laravel 5
Try changing the curly braces, {{ }}, to {!! !!} where you are calling the route helper.
The & is being encoded to & so only the first query string parameter that you are sending does not have a & in front of it so it is named correctly. The others are named with the amp;, the part after the & in the encoded ampersand.
So in addition to my prior topic (thank you guys for helping) routes with compact
I am now facing troubles with my store function, I am getting the error : "Trying to get property of non-object.
This is my store function in my Controller
public function store(Request $request)
{
// validate the data
$this->validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
// store in the database
$userpost = new Usp;
$userpost->title = $request->title;
$userpost->body = $request->body;
$userpost->save();
Session::flash('success', 'The blog post was successfully saved!');
return redirect()->route('admin.userposts.show', $userpost->id);
}
This is the view to create userpost(to make it more clear, p.s. the same form with different route ofcourse, works for my simple posts but not for my user submitted posts)
#extends('index')
#section('index-stylesheets')
{!! Html::style('css/parsley.css') !!}
#endsection
#section('content')
<h1>Create New User Post</h1>
<hr>
{!! Form::open(array('route' => 'admin.userposts.store', 'data-parsley-validate' => '')) !!}
{{ Form::label('title', 'Title:') }}
{{ Form::text('title', null, array('class' => 'form-control', 'required' => '', 'maxlength' => '255')) }}
{{ Form::label('body', "Post Body:") }}
{{ Form::textarea('body', null, array('class' => 'form-control', 'required' => '')) }}
{{ Form::submit('Create Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }}
{!! Form::close() !!}
#endsection
#section('index-scripts')
{!! Html::script('js/parsley.min.js') !!}
#endsection
Method of showing the post:
public function show($id)
{
$userpost = Usp::find($id);
return view('admin.userposts.show', compact('userpost'));
}
So the fact is that the problem was not the store method but the routes.
Route::get('/userposts/{id}', 'UserPostsController#show')->name('admin.userposts.show');
Route::get('/userposts/create', 'UserPostsController#create')->name('admin.userposts.create');
Route::post('/userposts/store', 'UserPostsController#store')->name('admin.userposts.store');
When registering the routes in that order, when laravel will iterate over your routes, it will first encounter the show one, and it will therefore take "create" as the id. Therefore, it will go into the show method and it won't find any post that matches, the post being null, you get the error.
So, there are two ways of fixing this.
The first one (the easiest, works in all cases, maybe not the best) is to put the create route before the show route.
The second one, the best in my opinion, is to add a condition to the id (doesn't work in the case of a slug). As the ids are only integers, you get :
Route::get('/userposts/{id}', 'UserPostsController#show')->name('admin.userposts.show')->where('id', '[0-9]+');
Therefore, create won't match the regular expression and it won't go in the show method.
For "resource creations" (storing in database), I wouldn't use a "field-by-field" method.
Instead, I'd do something like this :
$userpost = Usp::create($request->only('title', 'body'));
I feel this is more talkative.
But, it won't work, laravel protects* us against such things. To make it work, you have two options.
(The best option in my opinion)
In your model, add a protected variable called $fillable with all your columns that you allow to mass assign*. In this case you would put :
protected $fillable = ['name'];
(The option if you are sure of what are you doing)
In your model, you can say, hey, I know what I'm doing here, just let me do my stuff without guarding me. In this case you would put :
protected $guarded = [];
Notes :
$request->only('field1', ...) gives you an array of the fields that you want with the fields name as keys, in this case it gives you ['field1' => $request->field1]. In your case it will give you ['title' => $request->title, 'body' => $request->body].
Mass assignment is when you give an array to the model and it puts all attributes to the fields of the array. More informations here https://laravel.com/docs/5.4/eloquent#mass-assignment
When I mean laravel protects us against those things, it does't really protect us because it isn't a bad practice (instead, I find it more readable), but because it does allow you to make mistakes (for exemple, setting fields that don't exist).
so I have a selection box that gives a dropdown menu to give messages a manager from the dropdown. It takes the input and then changes to a column in the database called manager for it's respective column. When I try to submit the selection menu it gives me the regular error for Laravel. But then when I put ?debug=1 at the end it submits but gives the row's manager column a value of just blank.
Here is what I have in the routes.php
Route::get('foo/{id}', 'fooController#bar');
Route::post('foo/{id}', 'fooController#bar');
This is the form.
{{ Form::open(array('url' => '/admin/foo' . $message->id)) }}
{{ Form::select('handler[]', array('unassigned', 'foo', 'bar'), null, array('style' => 'width: 127px')); }}
{{ Form::submit('Change manager') }}
{{ Form::close() }}
{{ $message->manager }}
and here is what is in the fooController
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler;
$message->save();
return Redirect::action('AdminController#foo_bar');
}
I had a problem like this the other day, I have zero recollection of what I did. I really appreciate any help, thanks! The database is postgresql if that's any help
Try a dd(Input::all()) at the beginning of your controller and make sure you're seeing what you expect.
Also since you're sending an array perhaps you have to do Input::get('handler.0') -- see here right below the Input::only() and Input::except() code block.
It would seem as though because you are naming your select handler[], PHP is grabbing it as part of an array.
When setting up your message model, try this...
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler[0];
$message->save();
return Redirect::action('AdminController#foo_bar');
}
Usually, you'd only use names in your forms post-fixed with [] when you are accepting multiple values like checkboxes/multi-selects etc... Otherwise, it's probably best to stick with not using it because it may cause confusion.
I managed to fix it in a almost frustratingly simple way by just changing the method to PUT.
like this
Form::open(array('url' => 'foo/bar', 'method' => 'put'))
I have create.blade.php view modal and i want it to be used with and without default content.
example:
<div class="form-group">
{{ Form::text('title', $content->title, array('class' => 'form-control' , 'placeholder' => 'Insert Title Here.')) }}
</div>
The $content obj doesn't always exists so i get error like this (when there is no content set to $content):
Trying to get property of non-object
This is my controller function:
public function create($default_content = '')
{
return View::make('content.create')
->with('content', $default_content);
}
I tried to set default/fake obj:
$default_content = ($default_content == '') ? new stdClass() :
$default_content;
But in the end i get error that $content->title dose not exists.
Should i set all the variables to NULL in the obj if $default_content is empty ? if so, how?
There must be a better way to handle this problem - Thanks!
There are a few things you could probably do.
I'm assuming since you are building a form, content is actually a model, in which case when you open the form, use Form::model($content), then Laravel will automatically set those values for you.
You can read more about that here... http://laravel.com/docs/html#form-model-binding
If content is not a model, you could setup a view composer, which will automatically inject $content into your view each time it's loaded.
View::composer('content', function($view)
{
// Retrieve your content
// Inject the content into the view each time it's loaded.
$view->with('content', $content);
});
Can check http://laravel.com/docs/responses#view-composers for more info
The last solution would be to check for it in your view before setting it though usually, it's a good idea to keep this kind of logic out of your views.
{{ Form::text('title', isset($content->title) ? $content->title : '', array('class' => 'form-control' , 'placeholder' => 'Insert Title Here.')) }}
I'm having a hard time setting up simple links/actions.
In my index view, I have this little form that I want to launch the getTest action in the ProjectsController when I click on the button:
{{ Form::open(array('action' => array('ProjectsController#getTest', $project->id))) }}
<button type="submit"><i class="icon-arrow-up"></i></button>
{{ Form::close() }}
This is the getTest function :
public function getTest(){
echo "test";
return 'test';
}
But this keeps getting me a "Array_combine(): Both parameters should have an equal number of elements" error.
I tried making this work with a route. with this form open instead :
{{ Form::open(['method' => 'GET', 'route' => ['test_route', $project->id]]) }}
And this route :
Route::get('projects/test', array('as' => 'test_route', 'uses' =>'ProjectsController#getTest'));
But I still have the same error.
I can't find any good doc on routing/sending to actions that don't give me this problem. I don't see what
Your route doesn't need parameter, so I think this code is sufficient:
{{ Form::open(['method' => 'GET', 'route' => 'test_route']) }}
I believe the problem is you are adding parameters to the action, but you are not managing those parameters in your routes, nor is your getTest() function accepting any parameters. Another problem is you are setting your route as a GET route, but your form is going to be using POST.
It would be much easier instead on your form to use Form::hidden('id', $project->id); And then in your getTest() function, you could get the variable using $id = Input::get('id');. You'd also be able to use your route name in your form as well. Form::open(array('route'=> 'test_route', method=> 'get'));