Updating database ids, what to do with form url - php

Updating existing items in DB in my application is solved by this way:
I have url like this:
http://localhost/project/public//structure/edit/about-us
In router I have set
Route::get('/structure/edit/{url}', 'StructureController#update'); //for displaying the prefilled form
Route::post('/structure/edit/{url}', 'StructureController#update'); // for saving new values
So it means, I'm building update query where url = $url . This is the main part of my view file:
{!! Form::open(['url' => URL::current()]) !!}
I don't know where to point "form action". So I¨m using the same url as current url, so router recognizes, that this is post request and I can process the update inside the same controller and select new (updated) data to my update form.
The problem is, when I update the url via form, new value will set to database. So it means, from this moment old url doesn't exists, but and my form action point to url, which doesn't exists anymore.
What can I do with that? If you know, what I mean...

To update use patch method instead of post. Write this in web.php
Route::get('/structure/edit/{id}', 'StructureController#edit');
Route::patch('/structure/{id}/update', 'StructureController#update');
You can use either action or url as a form action. Pass the structure id in the second parameter of the action
{!! Form::model($structure,['action' => ['StructionController#update',$structure->id],'method'=>'patch']) !!}

If you do what #smartrahat suggests and still got the error you posted, then can you run php artisan route:list command and show us the structure of your routes?

Related

insert url parameter in mysql with codeigniter

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

Laravel retrieve id from URL

I would like to retrieve an id from a Laravel URL.
I've tried this:
$request->route('id');
But it would tell me that $request is undefined.
The url I am using is:
http://192.168.1.14/public/tasks/create?id=1
I've got a sidebar and within that I've got an if statement.
#if((request()->is('projects/1')) || (request()->is('tasks/create')))
<a href="{{ route('tasks.create') }}?id={{ $project->id }}">
#endif
I've hid the unnecessary code, but the second parameter is used for highlighting what page I am on in the sidebar. And the first for showing the create task option when I am on a project page.
Now, the if statement causes that its hidden ( so I can't see the error ( the error would say that there is no id but its true because it only receives the id by being on a project page with an id ))
Now, the task form on the page itself has a hidden input field with project_id so I can bind the task to the project.
Inside the value I have the $request->route('id') but it gives me an error that request is undefined.
Now I ask, how do I retrieve the id from the url, or else, can I retrieve the id another way? All the project and task blade files extend the project.index and the project.index extends the layouts.blade so its a layout within a layout.
Also, can I change the (request()->is('projects/1')) to something else so whatever project page I am on, the statement is true? For example 'projects/{project}'
Thanks for any help
Best way to do this is
You don't need to write 'create'
{{ app('request')->input('id') }}
or
{{ request()->get('id') }}
IF you are not sure how it works just
store in a variable and print it on view to cross-verify like this.
<?php
$id = app('request')->input('id');
//or
$id = request()->get('id');
?>
You can always check all request URL parameters like this.
$request = request();
print_r($request);
Create Route for getting id from url
Example:
Route::name('subscriptions.store')->post('/subscriptions/store/{id}', 'CreateSubscriptionController');

laravel - get attribute from html tag

So I have a form in my view:
{{Form::file('projectPicture', ['class' => 'uploadedImage', 'data-some-attribute' => ''])}}
with the attribute data-some-attribute.
And in my route I retrieve it like so:
$request->file('projectPicture');
How do I get a data-some-attribute in the route? Is it even possible?
I know I can use ajax to pass any data, but can it be avoided in this case?
Thank you!
It is not possible how you intend it to work, only because it's not how form data is working under the hood. The second argument in your sample Form::file is just decorating the rendered form element. It has no correlation with the form data that is transferred between the server and client.
For all intents and purposes form data is just a glorified set of key value pairs. If you wanted to pass some-data-attribute to your route controller, you have two options -
Add another form field, and make it empty using Form::hidden. In this case, you would just name the field some-data-attribute.
If your form is submitted through a POST method, you can tack on some-data-attribute onto the form's route and retrieve it from the request.
ie - your/route becomes your/route?some-data-attribute=whatever, and you can retrieve it later with something like $request->input('some-data-attribute').

Retrieve POST-data from view in controller

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.

Issue with Laravel 4 Routes?

I'm trying to learn laravel, but I'm running into an issue with routing.
I have the following in my Routes file:
Route::get('home', function()
{
return View::make('home');
});
Which works if I type
http://localhost/laravel/public/home
However, on a another page I have a form, that when submitted should take me to that page like so:
{{ Form::open(array('url' => 'home')) }}
Now this takes me to the correct address, but throws an exception. But, if I reload the page with the same URL then the page loads correctly. So what is the issue here? Is there a problem with the way my form is set up?
Route::get is when you want to issue a get request to the page, to get the contents basically. If you want to post to a page, you need to do, in addition to your Route::get,
Route::post
Another option that people will tell you is:
Route::any
but I would advise staying away from this because the logic will probably be different for the two routes.

Categories