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();
}
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 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();
I'm getting my feet wet with Laravel 5 and I need a little advice on Routing.
I'm running in to a problem over and over and I feel like I'm definitely not grasping the proper way to do this.
Lets say I have a route:
Route::post('viewarticles', 'LoginController#user_check_article_list');
This is where I am taken after I login.
This route shows me my articles that I can edit. On the page this route takes you to I have a list of articles and delete buttons next to them. When I click delete I have it linked to
delete/{{ $article->id }}
I then have a route:
Route::get('delete/{id}', 'ArticleController#deleteArticle');
Which then goes to this function:
public function deleteArticle(Request $request, $id) {
$article = Article::find($id);
$article->delete();
$articles = Article::latest('published_at')->Published()->get();
return view('admin.article_edit_list',['articles'=>$articles]);
}
The last two lines of this is me rebuilding that article list and then loading the view.
THE PROBLEM: In my url the page is still ../delete/someid
I really just need a way to transfer back to the routes.
Instead of returning a view, return a redirect. This will cause the url to change:
public function deleteArticle(Request $request, $id)
{
$article = Article::find($id);
$article->delete();
return redirect('/some-url');
}
Or if using named routes:
return redirect()->route('some.route.name');
Source
so if your url is staying at /delete/{id} you need to redirect to the the display articles view
public function deleteArticle(Request $request, $id) {
$article = Article::find($id);
$article->delete();
return \Redirect::to('show_articles.view');
}
how do you send the delete request ? is it via ajax?
if so you need to redirect to your view in the success method of your ajax call using js / most likely jQuery?
.success(function(){
location.href = '/your/new/view';
});
its worth checking your variables are getting through to your controller as well
public function ...
echo $id; exit(); // or dd($id);
and checking your collection loaded
$articles = ...
dd($artcles);
retrun view(...
also if you go in to app/config/app.php and set debug=true you should get an error stack which will help debug the issue. Sorry for the delay internet went down before I could finish typing.
I have a Controller witch contains more than 150 functions. almost all of the functions inside the controller are returning views.
each view should change if the user is logged in.
I almost handled this in the view side by using parent blades and etc. But in the controller side for almost every function i should check if the user is logged in and return some specific data about the user along with the view.
for example:
$user=[];
if(Auth::check())
{
$user=Auth::user;
$reputation=$user['reputation'];
$messages=$user->messages();
$notifications=$user->notification();
}
return view('pages.profile')->with(['user'=>$user , 'messages'=>$messages , 'notifications'=>$notifications]);
I thought that this is possible using middlewares but i could not figure it out.
what are the alternatives here?
for more information i am using laravel 5.
One solution could be using your custom function instead of view(). Sth like:
//in your controller action
public function someAction() {
return $this->view('pages.profile');
}
protected function view($template, $data = array()) {
return view($template)->with($data)->with([data that needs to be passed to all views]);
}