I'm very new to Laravel and I'm trying to make a crud application with a view for each db table that has a list with edit buttons on each record.
So far I have the paginated list which is working good and an edit view that is working as well.
The problem I'm facing is that whatever page I'm in I can click the edit button and edit the record successfully, but when I save the record I always go back to page 1.
I don't know and I'd like to know which is the correct (best) way to achive this.
Should I use session? Or maybe append the page parameter to the edit form action? Or else?
EDIT:
this is what edit method returns:
return redirect('/lyrics')->with('success', 'Lyric updated!');
Thank you for any help that points me in the right direction.
You can pass the page parameter to the edit page and then after the user update the record, you can redirect to the page you were in.
Something like this:
In the link where you call the edit page:
Edit
In your edit form:
<form>
... fields
<input type="hidden" name='page' value={{ app('request')->input('page') }}
</form>
Then in your update method at the controller
public function update($request)
{
// update
return redirect('homepage', ['page' => $request->page]);
}
Related
I need your help!
I am new to codeigniter and I am trying to pass a fetch parameter from url, get me the value using the following
My example url is the following: http://localhost/infocargacasosfinal/index.php/creacionacta/?NroGestion=1&NroContacto=103386816
Where what I am rescuing is the value "NroContacto", I have managed to bring me the value with the following line in the controller:
'NroContacto' => $this->input->get('NroContacto');
and show it in the view with the following:
<td>
Numero Folio: <?php echo $nrocontacto; ?>
</td>
that way I have managed to show it in the user's view, but now I can't find a way to save that value in the database when the user clicks the "save" button
You have not shared much information related to your issue. I am assuming that you need two server calls to save the URL parameter into data.
First "http://localhost/infocargacasosfinal/index.php/creacionacta/?NroGestion=1&NroContacto=103386816", is used to render the page with a form to submit.
Second, When the user clicks the submit button in the form, data is stored in a database.
If this is the case, on first page, you can collect nrocontacto as you have already done in your code and add a hidden input field <input type="hidden" name="nrocontacto" value="<?php echo $nrocontacto; ?>"> inside the form in the view file.
When the user submit the form, you can grab the value using $nroContacto = $this->input->post('NroContacto', true); (this code goes to form processing controller method). Now you can use CI query builder $this->db->insert('tableName', array('field_name' => $nroContacto)); (Note: add other required fields for insert array) to save it to the Database.
Codeigniter support seo friendly urls so you can arrange your url like this
http://example.com/index.php/news/local/metro/crime_is_up
for your url it will be
http://localhost/infocargacasosfinal/index.php/creacionacta/1/103386816
Now you can fetch like this
'NroContacto' => $this->uri->segment(3, 0);
now pass the value to your view page.
You can get detailed documentation about url segments here
https://codeigniter.com/userguide3/libraries/uri.html
I want to change some parts of my data from index page without loading edit page with Ajax.
For example my reviews has status where value is 0 or 1 and also comment section. I want to change these values from my index page.
All the code i have is simply my index method only, where i load my list
public function index()
{
$ratings = Rating::orderby('id', 'desc')->paginate(10);
return view('admin.ratings.index', compact('ratings'));
}
I need help to make my index page as a form with Ajax in order to edit from there but not to use my Update method because I'll need it for my edit.blade.php (maybe add another method in my controller?)
Thanks.
First of all you need a controller that updates the post status. As discussed it totally up to you want you want to do.
My suggestion is to create a new controller called UpdatePostStatusController and then have update method that takes post id and the status either 1 or 0 and update it.
Your index file should have javascript that gets triggred everytime you change the status like so
checkbox html
<input type="checkbox" data-uuid="{{ $post->id }}" class="update" #if($post->status) checked #endif/>
Now your ajax should
$('.update').change(function() {
data = {'status' : this.checked }
post_id = $(this).data('uuid');
//do your ajax post request here here
}
Hi Im currently trying to make a basic crud for my questions resource on laravel 5
so far so good, but now Im having troubles displaying the edit view, because the url is not being created correctly when I try to send the resource id in the url
here's the anchor Im using
<button class="submit-form button button btn btn-primary" style="margin: 0 1em;" type="submit">Editar</button>
here's the route in my routes file
Route::get('admin/preguntas/editar/{id}','QuestionsController#edit')->name('admin/questions/update');
the method in the controller works just fine, when I manually type this url
/admin/preguntas/editar/4
It shows the view without problems, but when I go from the anchor the url it goes is this one
/admin/preguntas/editar?4
of course the 4 is the id from my resource, but why is not typing the correct url?
thanks in advance
You can't wrap a hyperlink around a button so my assumption is your problem is related to the form action (since button type is submit), not the a href.
Since it appears you're using bootstrap, there is no need to use a button to get the styling of a button.
<a class="btn btn-primary"> will work just fine.
And your "QuestionsController#edit" accepts "id" argument?
like
function edit($id){}
And I think, your are using wrong route link helper
Route::get('user/{id}/profile', function ($id) {
//
})->name('profile');
$url = route('profile', ['id' => 1]);
First write proper link button as Devon said:
Editar
Second you must write proper route name format:
Route::get('admin/preguntas/editar/{id}','QuestionsController#edit')->name('admin.questions.update');
Hope it will help you to solve the problem :)
I have a page with a form for creating users. A user has an hobby, which can be created on the same page by clicking on the second button which opens the page for creating a hobby. After creating the hobby, the previous user form should be shown with the user input inserted before going to the hobby page.
Is there a way to do something like with typo3 flow / fluid without using AJAX?
I tried to submit the input to a different action by clicking on the createHobby button --> The action redirects to the new hobby page, where the user can create the hobby and after creation it should redirect back to the user form with the already filled out input fields by the user .
I used...
<input type='submit' value='Create' formaction='/hobby/create' />`
to achive this, but it seems there are some problems with the uris... I get following error:
#1301610453: Could not resolve a route and its corresponding URI for the given parameters.
I think the using the attribute formaction is not a good solution for every case, as it is not supported by IE < 10 as you can see here. I think a JavaScript backport should also be considered (dynamically change the action attribute of the form when clicking on the second button, before actually submitting the form).
Concerning your error, you should not – and probably never – use direct HTML input, instead try to focus on Fluid ViewHelpers, which allow TYPO3 to create the correct HTML input.
Try this instead:
<f:form.submit value="Create" additionalAttributes="{formaction: '{f:uri.action(controller: \'hobby\', action: \'create\')}'}" />
You can make an $this->forward(...) in an initializeActiondepending on an param of your action.
Lets imagine your default Form action is "create". So you need an initializeCreateAction:
public function initializeCreateAction()
{
if ($this->arguments->hasArgument('createHobby')) {
$createHobby = $this->request->getArgument('createHobby');
if ($createHobby) {
$this->forward('create', 'Hobby', NULL, $this->request->getArguments());
}
}
}
Now you must name your input createHobby and assign your createAction this param:
In fluid:
<f:form.button type="submit" name="createHobby" value="1">Create Hobby</f:form.button>
In your Controller:
public function createAction($formData, $createHobby = false)
{
...
}
can you explain something more ... what you show has nothing to do with typo3, I don't know where you inserted that, what version of typo3 , using any extension extra ?
I'm trying to do something very basic: retrieve a post value from a hidden field. The hidden field is obviously in my view file and I want to retrieve the value in my controller. I'm using the framework SimpleMVCFramework.
I have a hidden field in my projects.php file (the list with projects). When you click on a project, a method in the controller renders the clicked project and the corresponding page. This corresponding page is called project.php
The hidden field in my projects.php view:
<form method="post" action="project.php">
<input type="hidden" name="project-id" value="<?php echo $project['id'];?>">
</form>
This hidden form is displayed correctly in my lists with projects. I checked them in the console.
In my ProjectController.php, I try to retrieve the data using
$data['id'] = $_POST['project-id'];
Then, I send the $data variable with the rendered page, so that I can use the id. So every project in projects.php has a hidden file that outputs correctly. When I try and click on a project, it brings me to project.php, but when I check out the $data variable, the id is just empty.
The routing works like a charm, because e.g. $data['title'] = "Project"; works great and is visible when I check the $data variable. When I change
$data['id'] = $_POST['project-id'];
to
$data['id'] = "foobar";
the id in project.php isn't empty anymore, but shows foobar. So I guess that something goes wrong with retrieving the value.
I also tried to remove the action=".." from the form, but that also didn't work.
The thing I'm trying to achieve is so simple, that I don't understand what is going wrong. Is it possible that the problem lies with the framework and that the code is right?
Thanks in advance and sorry for my bad English.