Laravel retrieve id from URL - php

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');

Related

As a result I redirected to my ID editing form using twig and php

I am learning to use twig and I do not know how to redirect to an editing path through ID
In pure PHP it would be like this for example:
edit
In twig to redirect to a location I use my route and the URL filter for example:
Back
How do I achieve this: editar but using twig.
First make sure you have defined the route with the parameters in your routing file.
So if you want to generate url for the route: /update.php?id={id} then you can pass the id parameter like this:
{{ url('update.php', {'id': '. $row['id'] .'}) }}
Documentation

Updating database ids, what to do with form url

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?

Save fileds changes with ajax in laravel

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
}

how to get id in url and display it in a form laravel 5

i have this href that leads you to addeditvisit/.(the chosen row's id)
<td><img src="images/visit.png" style="width:30px; height:30px;"></td>
so the url looks something like this http://localhost:8000/addeditvisit/5
and inside it is a form. where i want to include the $patient->pID and make it a value of my hidden input so that i can include it saving the form.
in my controller I coded this:
$uri = $request->path();
return View::make('pages.addeditvisit', ['uri'=>$uri]);
in my form i have this input text first instead of hidden to display what really is the value and it is displays
addeditvisit/5
what i really want is to only get the 5. what function should i use? Please help me, i'm just new in Laravel 5 and still learning
You can pass your whole Patient object if you want, like this:
$patient = Patient::find($id); // get it from db, or some other data source
return View::make('pages.addeditvisit', ['uri'=>$uri, 'patient' => $patient]);
`
and then in your form you can use the
$patient->pID
or you can just pass the id itself, if you want to have only that:
return View::make('pages.addeditvisit', ['uri'=>$uri, 'pID' => $patient->pID]);

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.

Categories