Laravel redirecting to the previous page - laravel

I am inserting data to the database and redirecting back to the previous page using the code below in laravel
public function store(Request $request)
{
$todo=new Todo;
$todo->todo=$request->todo;
$todo->save();
return redirect()->back();
}
See My web page
After entering some data "new todo" it works well; data inserted and shows in the page but the page becomes like below.
I think the stylings are not working in the page
And after refreshing the page i got the page well.
What should be the problem?

You can try this
return back()->withInput();

You can save the previous URL in session so that you can easily redirect back to their previous URL,
Try this in your function,
public function store(Request $request)
{
$request->session()->put('url.intended',url()->previous());
$todo=new Todo;
$todo->todo=$request->todo;
$todo->save();
return redirect($request->session()->get('url.intended'));
}
Here, the Previous URL is generated by Helpers function of laravel.
Please comment if you have any doubts.

Related

Laravel Error 500 on a parametric route when in production

I built a small blog where a editor can create articles and post it and users can read it without login.
The issue that I'm having is the following :
In production server, if a user try to access a blog article from the home by clicking on the 'Read more' button, gets a 500 internal server error.
I'm also using other parametric routes and they are working perfectly fine.
In development server, everything works fine.
Any idea of what could the issue's cause?
Thanks in advance
In the home page I'm rendering a list of all the articles via the PublicController's index() method with the following code :
public function index()
{
$articles = Article::all();
return view('welcome', compact('articles'));
}
And it works perfectly fine.
But when a user click on a item of the list should be redirected, via the 'show()' method of the Public Controller, to the article's details page but in reality the user is getting a 500 internal server error.
The show() method :
public function show($id)
{
$article = Article::find($id);
return view('article', compact('article'));
}
One thing that can help you is to logging the exceptions in your Handler.php file. This way you can see the error details (message, file and code line).

view does not seems to see $error variable in laravel middlegroup "auth"

I'm having problem on my view not seeing any $Message variable, which is working on my login controller.
public function index()
{
$data = somedataFromModel::find(1);
return View::make('SomeView',compact(['sampleData' => $data]);
}
public function postOrStoreData(CustomFormRequest $request)
{
/* where update or insert or store occurs */
//after all my updating codes or inserting codes
return back()->with('Message','test');
}
with these codes I am not able to run my view correctly, it does not see $Message
it has the same implementation as my login controller and views.
right now my view needs to be authenticated to be used, so it is under the Middleware Group of 'auth', I also see the MiddleWareGroups, web and api. help me I'm lost.
My only objective is after I post any data, without using AJAX, just standard Form Action and Submit Method, I will display an alertClass with bootstrap.
redirect()->with() stores it in the session.
Per the docs, you thus access it via session('Message') in the view, not $Message.
The syntax may appear similar to view()->with(), but the functionality is not identical.

Laravel change url paramaters in controller

I have eCommerce systems with ads so I want to count every click from the ad card , so I add ad=true to check it in my controller. It counts fine and work smooth
http://127.0.0.1:8000/product/3?ad=true
but the problem is that if the page has been refreshed it will count more and more , so I need a way to remove this parameter
how can I do this. if there any way else I am open to suggestions
Upon processing the request, you can just redirect to the same route without the parameters.
E.g.,
public function action(Request $request)
{
/* Process the Request */
return redirect('theRouteName');
}
You can try this
Route::get('/product/{id}', 'YourController#funciton');
public function multi_delete($id) {
$count = Input::get('ad');
// your code goes here
\Redirect::route('product', $id)
}

laravel route issue .the page you are looking for could not be found

I want to show data from the database when I press the button in one view it displays me the specific data by the id in the other view this is my first view.
In my controller:
public function show($id) {
$symptoms=symptoms::all();
foreach ($symptoms as $symptom) $symptom=symptoms::find($id);
return view('front.second',compact('symptom'));
}
My problem is when displays the second view it can't find the page and the route just display the id not the method/id :(
Try this:
public function show(symptoms $symptom)
{
return view('front.second', compact('symptom'));
}
Or:
public function show($id)
{
$symptom = symptoms::find($id);
return view('front.second', compact('symptom'));
}
And read Laravel's Controllers Documentation carefully.
As you shared image, is clearly shows that you are using below route
{{route('show',['id'=>111])}}
Check your route by php artisan route:list and use right one, I guess you should use like
{{route('user.show',['id'=>111])}}
If you Controller name is UserController in resource

Calling a Controller from another controller in laravel 4 not working

I have a controller which saves my form data and after saving that data i am trying to redirect my page to another form.But Redirection part not working form me. This is my saving controller code.
class CompanyRegistrationController extends BaseController
{
public function saveAndCreateCompany()
{
// my code which gets form data and saves in database.
//After that i am trying to redirect this to another page.
To acheive this i am trying to do this.
App::make('AddMoreEmployeeController')->showAddMoreEmployeeDetails();
}
}
// My new page code
class AddMoreEmployeeController extends BaseController
{
public function showAddMoreEmployeeDetails()
{
return View::make('company.addEmployee');
}
}
Routes.php code
Route::post('/companyCreatedSuccessfully', array('uses' => 'CompanyRegistrationController#saveAndCreateCompany','as' => 'saveAndCreateCompany'));
Route::get('/addMoreEmployeeDetails', array('uses' => 'AddMoreEmployeeController#showAddMoreEmployeeDetails','as' => 'showAddMoreEmployeeDetails'));
But after filling the form details I am not redirecting to "addMoreEmployeeDetails" .
I am still in companyCreatedSuccessfully with blank page.But After successful save data in
database i need to redirect to addMoreEmployeeDetails.
Where i did mistake?
The problem is that you only launch other controller but do nothing with its result. You need to return it:
return App::make('AddMoreEmployeeController')->showAddMoreEmployeeDetails();
and now it will work.
EDIT
As you want to change url, it seems you want to make redirection to another controller, so you should use:
return Redirect::action('AddMoreEmployeeController#showAddMoreEmployeeDetails');

Resources