Redirect back Laravel 5? - php

I have a form filter and a grid with result data. In table i click on a field and update this field,in controller return Redirect::back(), in this case all field from form are reseted, but i need to keep this value. Field what is pressed is out of this form.How can resolve this problem?
{!! Form::open(['url' => 'admin/filter', 'method'=>'put','class' => 'navbar-form']) !!}
//text fields for filter
{!!Form::close!!}
table
<td>
#if($mark->enabled==1)
<p>
{!! HTML::link('admin/showEnabled/'.$mark->id,'', ['class' => 'glyphicon glyphicon-eye-open']) !!}
</p>
#else
<p>
{!! HTML::link('admin/showEnabled/'.$mark->id, '',['class' => 'glyphicon glyphicon-eye-close']) !!}
</p>
#endif
<td>
</table>
--------------
Cotroller admin showEnabled
update field
Redirect::back()

Use the withInput() chaining method:
return Redirect::back()->withInput();
Or, in the latest version:
return back()->withInput();

Related

Fetch an id from pivot table to use in form builder in laravel

I need help to access foreign id in my pivot table to use in form builder select form. I'm trying to create a form when I insert movie and select category then they will be connected when I insert it by fetching category_id from pivot table.
I use many to many relationship and my tables are movies and categories with pivot table category_movie (id, category_id, movie_id).
This is my controller and form.
Controller
public function store(Request $request)
{
$request->user()->authorizeRoles('admin');
Movie::create($request->all());
$categories = Category::pluck('category_name', 'id')->all();
return view('movies.upload', compact('movies', 'categories'));
}
View
<div class="col-md-6">
{{csrf_field()}}
{!! Form::open(['method'=>'GET', 'action'=> 'MoviesController#store']) !!}
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label ('', 'Category:') !!}
{!! Form::select('', [''=>'Choose Categories'] + $categories, null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Insert Movie', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
First of all you don't need csrf_field() call (Form::open will inject one for you). All you need to do is add logic to your controller to process selected categories. Give your select a name and make it of type multiple (since you have many to many relationship you want user to be able to select multiple categories for a movie):
<div class="form-group">
{!! Form::label('categories', 'Category:') !!}
{!! Form::select('categories', $categories, null, ['class'=>'form-control', 'multiple' => true]) !!}
</div>
Then in your controller when storing new movie you can read selected 'categories' from the request and attach them:
$movie = Movie::create($request->all());
$movie->categories()->attach($request->get('categories'));
Also store() method is usually accessed via POST route and it returns redirect response to a page where user can view newly created movie (or all movies). To display the form it is better to create a seperate create() method accessed via GET route. And don't forget about validating info in Request - you should either utilize Laravel's FormRequest or use controller's $this->validate() method before inserting anything in your DB.

Check if Form method is post or patch in Laravel

I have an edit view and i am using a partial _form view.
Is there a way to check if the form is a patch or post?
What i plan to do is to change the hidden field in edit form
#if (form is post)
{!! Form::hidden('signature') !!}
#else
<div class="form-group">
{!! Form::label('signature', 'Signature: ', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('signature', null, ['class' => 'col-md-2 form-control', 'required']) !!}
</div>
</div>
#endif
because this variable is already saved to DB and i want to load it for edit.
Or to check if form is post, that would work also!
I usually pass the variable to a view where I set action, like:
$action = 'store';
Then I use this variable to build route name:
{!! Form::open(['route' => 'post'.$action, ....
And detect what type of action is needed:
#if ($action == 'store')
I guess it's the most readable and simple way to achieve what you're trying to achieve. You can do something similar.
Try this:
$isPut= Request::isMethod('put');
if($isPut) {
//
}

Laravel 5 forms: generate fieldv value instead of having user fill in

I am creating a form that has a field that is required (trip_route_name). I want to generate a name for this (say a random number for example) when the form is submited rather than having the user fill out the form. How do I do that? Here is the code for the form:
<div class="form-group">
<div class="col-md-6">
{!! Form::label('trip_route_name', 'Trip Route Name') !!}
{!! Form::text( 'trip_route_name', null, ['class' => 'form-control']) !!}
<p id="msg_trip_route_name" class="text-danger">{!! $errors->first('trip_route_name', ':message') !!}</p>
</div>
</div>
You can use str_random() helper:
{!! Form::text( 'trip_route_name', str_random(20), ['class' => 'form-control']) !!}
The str_random function generates a random string of the specified length.
If you don't want user could see generated random name, use hidden instead of text.

Laravel 5 - inserting old data into form via variable

I have a slight problem. I have a system whereby I can drag and drop my own forms. The html code for a form is saved in my database. When it comes to the edit page, I do something like the following
{!! Form::model($project->document, [
'class'=>'form-horizontal',
'method' => 'PATCH',
'route' => ['projects.documents.update', $project, $document->id]
]) !!}
{!! $documentData->documentData !!}
<div class="form-group">
{!! Form::submit('Save Data', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
$documentData->documentData contains the html code for this particular form.
Now my problem is, $documentData->form_data contains the old inputs for this form.
Is there any way to get this old input into the form, the way I am currently handling things?
Thanks
in controller you can access old input by $request->flash(); while in frontend you can access old by input type="text" name="name" value="{{ $name }}"

Laravel 5 updating user record with out password

Essentially I have a page where I can update user records.
{!! Form::label('username', 'Username:') !!}
{!! Form::text('username', null, ['class'=>'form-control'])!!}
{!! Form::label('password', 'Password:') !!}
{!! Form::password('password',['class'=>'form-control'])!!}
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class'=>'form-control'])!!}
{!! Form::label('email', 'Email:') !!}
{!! Form::text('email', null, ['class'=>'form-control'])!!}
Now there are 2 problems I am facing:
1) If I do not want to change the users password and just edit the name for example, it will update the users password to be empty (or ""). This will be because the text box is empty upon submission.
This is what I have tried in my controller with no joy
public function update($id, Request $request)
{
$user = User::findOrFail($id);
$newPassword = $request->only('password');
if(empty($newPassword)){
$user->update($request->except('password'));
}else{
$user->update($request->all());
}
return redirect('users');
}
Does anyone know how I can tackle this? I'm hopping laravel has a way to deal with these things.
For reference it is laravel 5 that I am using.
use: $request->get('password');
instead of: $request->only('password');
Why:
$request->only(); - returns array with listed fields
In this case it returns array with only one key and empty value, but array isn't empty.

Categories