how to routes localhost url in Laravel 5.2 - php

I am working with laravel application. in my application I have controller function to view form as following.
public function create()
{
return view('projects.new');
}
when view projects/new.blade.php file it is contain form. in this form fill and save it is generated new project in project table. now I need after save this form next page redirect to the collaborator.blade.php file in the same projects folder. and local host url as this
localhost:8000/projects/10/collaborators
this is my controller method
public function projectcollaborators()
{
return view('projects.collaborators');
}
and routes
Route::get('projects/{projects}/collaborators', [
'uses' => '\App\Http\Controllers\ProjectController#projectcollaborators',
]);
how can I manage controller and routes to success above requirements?

You should have a Route::post in your route file to capture the form submit,
say something like Route::post('projects', 'ProjectController#create');
The function that returns the view should be called index with a route like this: Route::get('projects', 'ProjectController#index');
In the create function you can use Input() to grab the information submitted by the form.
After grabbing the information you need you can then add the details to your projects table using Eloquent.
e.g.
$project = new Project();
$project->name = 'Name'
$project->fields = 'Text'
$project->save();
After inserting and saving the details into your database you can do a redirect to projects/ID/collaborators route using the redirect function.
You will also need to create a route for projects/ID/collaborators so you dont get a 404...
e.g. Route::get('projects/{id}/collaborators', 'ProjectController#collaborators');

Related

Laravel get view name of rendered route

Is here a way to achieve that :
I want to check in AppServiceProvider, name of rendered view.
So, for example :
User enters at /home, for that route in controller it return view('website.home');
From AppServiceProvider get current route, and get view name that is rendered.
I don't know that it's possible to get the view name in the AppServiceProvider, as that runs prior to a view being created in the route's respective controller method. You can get the name of the view within the controller, however, after it's been created:
$view = view('website.home');
$name = $view->getName();
return $view;
The current route name is accessible within the service provider using:
request()->route()->getName();
// or
Request::getCurrentName();
edit
Actually this would probably be doable with a view composer and a wildcard match on the view:
// AppServiceProvider boot method
public function boot()
{
view()->composer('*', function($view) {
view()->share('viewName', $view->getName());
});
}

return back()->withInput when using a link

I am trying to first post my form to my method that returns a view to preview the post that has been done. When you get to the new view of the preview i want to be able to use a link to go back to the previous page with all form inputs already filled in.
I have tried doing this with setting another url and route to the link where the route triggers a method in my controller with the return back() call. This does not seem to work and i guess it is because it is outside my method with the $request.
So i am wondering how it would be possible to achieve what i want to do. Im using Laravel 5.5
EDIT
Here is my current code:
After the form is posted, you are sent to this method:
public function store(Request $request)
{
$preview = new Preview();
$preview->post_title = $request->title;
$preview->save();
$previewData = Preview::latest('id')->first();
return view('pages.preview_ad')->with('previewData', $previewData);
}
In the 'preview_ad' view, i want to have a link that routes to another method that triggers
return back()->withInput();
Although when i try to do this with the code below, it does not return the previous page with the inputs which the code above should. It works properly if i run it inside the 'store' method above, but not in the other method.
Route::get('/Skapa-annons/Tillbaka', 'PreviewsController#go_back');
-
public function go_back()
{
return back()->withInput();
}

Laravel 5.3 - flash data removed after redirect

I am using Laravel 5.3
I have two models, Parent and Student with a many-to-many relationship between them and I want to add a Parent for a Student and vice versa.
My approach was:
Create a link from Student profile to add Parent, like so:
Add Parent
Add the route students.parents.add with a controller method to flash the id to the session and redirect.
// in web.php:
Route::get('/students/{id}/parents/add', ['as' => 'students.parents.add', 'uses' => 'StudentController#addParent']);
Route::resource('students', 'StudentController');
// and in StudentsController:
public function addParent($id)
{
return redirect()->route('parents.create')->with('associated_id', $id);
}
After that, clicking the button redirects to the ParentsController create() but the session data is not there when I try return session()->all().
Am I missing something?
Make sure you are not adding web middleware in your routes.php file as it is already added in the file RouteServiceProvider.php file.
If you now apply it again in your routes.php, you will see that web appears twice on the route list php artisan route:list. This exactly makes the flash data discard.

Pass value from one view to other laravel routes configuration

I have just started using laravel and currently using the version 5.2.
I am using two forms to get the data. First form in first view and second form in second view. I need to figure out a way to tell the second form, the form id of the first one, which the second form will be linked to.
I know this can be done by passing the values using the URL. But I lack the knowledge of the correct syntax.
1- How to send data while redirecting?
2- How should the route look like?
3- How to access that value in the second view, in order to pass that value when the second form submits?
I have googled a lot about this but couldn't understand those advanced syntax.
Any help is appreciated.
Thanks in advance.
This is the code in controller:
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
Session::flash('values',$request->azauj_id);
return redirect('/add/requirement');
}
public function getCreateRequirement(Request $request){
$att = Session::get('value');
Session::flash('value',$att);
return view('req');
}
public function postCreateRequirement(Request $request){
dd(Session::get('value'));
}
The forms are plain html forms with post methods of submission
When I use dd(Session::get('value'));, i get null. It means that the value is not being passed. To the postCreateRequirement method which is called when the second form is submitted.
Below are the routes.
//For Add Profile Page
Route::get('/add', 'ProfileController#getCreateProfile');
//For Add Profile Form Submission
Route::post('/add', 'ProfileController#postCreateProfile');
//For Add Requirements Page
Route::get('/add/requirement', 'ProfileController#getCreateRequirement');
//For Add Requirements Form Submission
Route::post('/add/requirement', 'ProfileController#postCreateRequirement');
1- How to send data while redirecting?
You can simply pass data with your redirect using the ->with() method which creates an session that will only appear on the next page more here
Example:
Say you want to send a status down to your view you add the with to your redirect:
// Where you are redirecting to
redirect("/otherRoute")->with("status", "Profile updated!");
// session name // data
Now you simply check if the session exist and echo it out:
// If the session does not exist it will return false and not create it
#if (session("status"))
<div class="alert alert-success">
// echo out the session
{{ session("status") }}
</div>
#endif
2- How should the route look like?
Routes should be defined in the routes.php file located in the http directory assuming you are posting the data you should make the routes and connect them to your controller like so:
//For Add Profile Page
Route::get('/add', 'ProfileController#getCreateProfile');
//For Add Profile Form Submission
Route::post('/add', 'ProfileController#postCreateProfile');
//For Add Requirements Page
Route::get('/add/requirement', 'ProfileController#getCreateRequirement');
//For Add Requirements Form Submission
Route::post('/add/requirement', 'ProfileController#postCreateRequirement');
3- How to access that value in the second view, in order to pass that value when the second form submits?
You could simply use the ->with() method in your redirect
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
return redirect('/add/requirement')->with("value",$request->azauj_id);
}
Get the value
public function getCreateRequirement(Request $request){
$value = session("value");
// compact it and trow it in an input to pass it trough to the last controller method
return view('req');
}
public function postCreateRequirement(Request $request){
$request->get("value");
}
OR
Create a global session and flush it afterwards
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
session("value",$request->azauj_id);
return redirect('/add/requirement');
}
Get the value
public function getCreateRequirement(Request $request){
return view('req');
}
public function postCreateRequirement(Request $request){
$value = session("value");
$request->session()->forget("value");
}
Note: Laravel does not flush sessions when somone logs out these will remain if not flushed by hand or using the method $request->session()->flush();

Link to a another page from a dynamic page in laravel

I created a dynamic page by taking data from the database.
Example: example.com/post/SE12
This /SE12 is dynamically created. Now I have a button in this page where I want to edit the post.
example.com/post/SE12/edit.
How can I link a button to this page using laravel? and how can I route this page and call it in controller?
In route.php
Route::resource('posts','PostsController');
Route::get('/posts/{code}', [ 'as'=>'post-show', 'uses'=>'PostsController#show']);
routes.php:
Route::get('post/{code}/edit', [ 'as'=>'post-edit', 'uses'=>'PostsController#edit']);
Controller:
public function edit($id)
{
echo $id; //this will give the code. e.g. SE12
}
View:
some title
N.B. you are resource controller and manual mapping at the same time. stick to either one as much as possible. otherwise routes will get conflict.

Categories