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();
Related
so at the moment my 'store' resource controller adds to the table in my database and fetches the view for my 'index' page (my home page) however when i refresh this view it duplicates the last row added to the database. i have noticed the URI is 'http://127.0.0.1:8000/store' instead of 'http://127.0.0.1:8000/index' can someone please explain what is going on. i apologise for the lack of technical terms i'm a new apprentice and am trying to figure this out using my own initiative but so far have had no luck. i also noticed that my log out function does the same thing although the URI is slightly different in that it still displays the CSRF token in the URI however it still says its at the 'logout' file path, here is an example of this; 'http://127.0.0.1:8000/logout?_token=bqTl4J3EZyKj7LS5ZvqfRB8k1Qg02IT1j4WlBG51&dir2login='
FYI i have already tried return $this->index();
my logout controller method;
public function logout(){
Auth::logout();
return view('index',['posts'=>$this->getTable()]);
}
my store controller method:
public function store(Request $request){
//creates new row in database table
//$clientIp=$request->ip();
$post = new PostModel();
//gets email of user currently logged in
$post->email=Auth::user()->email;
$post->ip=$request->ip();
$post->content=$request->content;
$post->company=$request->company;
$post->rating=$request->rating;
//saves to database
$post->save();
return view('index',['posts'=>$this->getTable()]);
}
Have you tried
return redirect('/');
What you're trying to do here is get the user to the index view. However, instead of returning a different view (and having to define the logic of 'posts' again), it's much easier to redirect the user to the index method of your controller. You have multiple options to do so. I have three listed below:
This is what you currently have:
return view('index',['posts'=>$this->getTable()]);\
you could do with the "action" (ControllerName#FunctionName):
return redirect()->action('PostController#index');
or with a "named route":
return redirect()->route('posts.index');
or to a direct URL
return redirect('/');
I have a Data model that I want people to be able to view individual records of and then edit/add data to. I have managed to get the view route working;
Route::get('/data/{data_token}', 'DataController#show');
Data_token, is a unique string. This then uses this DataController function;
Public function show($data) {
$data = Data::where('data_token',$data)->first();
return view('data.show', compact('data'))
}
After which I can display the data on the page, and have a form for editing (actually its for adding data that doesn't exist, but whatever, same principle right).
On the form on the data.show view, I am sending it to a different view;
Route::get('/data/{data_token}/edit', 'DataController#edit');
This can use the $request variable to return the forms values, but I can't relate it to the data row I was previously editing?
how do I get the {data_token} passed to the edit function of the controller?
Edit( adding route files)
Noticed I forgot the {'data_token'} in the post route.
/Begs forgiveness
I think you've misunderstood how the routes and controllers work. What you're looking at is a fairly simple CRUD setup like the following;
Route::get('/data/{data_token}', 'DataController#show');
Route::get('/data/{data_token}/edit', 'DataController#edit');
Route::post('/data/{data_token}/edit', 'DataController#update');
Now your controller would have;
public function show($dataToken) { ... }
public function edit($dataToken) { ... }
public function update($dataToken, Request $request) { ... }
Then you'd have your form on the edit view like so;
<form action="{{ route('DataController#update') }}" method="post">
Laravels router will always try to pass in the URI variables as arguments to the methods provided.
Providing that I have understood what you need, this should suffice.
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();
}
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');
I have this code in a project with Laravel 5:
return back()->with('msg_ok','successfully sent');
The param msg_ok is pushed in Session, but I´m not want use session params, I want pass the msg_ok parameter as variable.
For example I want print this en my blade file:
{{ $msg_ok }}
Laravel back() function will always return data in a Session. Normally you can't return variable with back() function. You have to use view() function for that.
Alternate Solution
You can use keep() function for storing data in session like variable. Which will not be flushed after refresh.
e.g
$request->session()->keep(['username', 'email']);
and then get data with key.
Laravel back() will redirect you back from the form where it is submitted,
and back()->with('msg_ok', 'successfully..');
if you place an item inside back()->with(); it will automatically place the value
inside session.
You can use return redirect()->to('url/path?msg_ok=successfully') add the query string parameter to url to pass it as variable.
and then on the controller where you have been redirected inject the Reqest class to your method
e.g
public function index(Request $request) {
$msg_ok = $request->get('msg_ok', '');
return view('view.blade', compact('msg_ok'));
}
Place the variable string inside the compact to make it accessible to your view.
You can just use Session::get('msg_ok') in your blade file. Or pass the variable to the view explicitly, in controller, like
return view()->make('view', ['msg_ok' => session()->get('msg_ok')]);