I cant seem to figure this out. I looked around here and different laravel forums for a solution, but no luck. Hopefully someone can point me in the right direction.
I have a show method in my controller, if I delete a record and redirect back to show, the record will not be found since it has been deleted. So I want to check if eloquent returns a model instance, if it doesnt and returns a null, I would like to redirect to the index page.
Currently with this code, event if eloquent returns a model, my show page is tripping out and keeps reloading, like an endless loop. However, if I take the redirect from the if statement and put in a dd('if statement working') to see if it fails the if statement, it works find. So the issue is with the redirect statement.
Here is a snipped of my show method:
public function show($id)
{
$vendor = $this->user->vendors()->find($id);
//if there are no results redirect to the index page.
if(is_null($vendor)) {
return redirect()->action('VendorsController#index');
}
session()->put('vendor_id', $vendor->id);
return view('vendors.show', compact('vendor'));
}
Any help is appreciated!
public function show($id)
{
$vendor = $this->user->vendors()->find($id);
if($vendor) {
session()->put('vendor_id', $vendor->id);
return view('vendors.show', compact('vendor'));
}else{
//redirect to index
return redirect()->action('VendorsController#index');
}
}
I have a show method in my controller, if I delete a record and redirect back to show
How about you modify your delete/destroy method, then send it directly to your index page after delete.
Related
I have a route set up correctly... I know because the dd($dev) shows me the model instance when not commented out. ($dev is a model instance that is successfully grabbed after translating the slugs in the URL in other funcitons before getting to show)
When I view the page/route with dd($dev) commented out, I get a blank page! No error.
DevelopmentController.php:
public function show($dev){
if(is_numeric($dev)) $dev=Development::find($dev);
if(!is_object($dev)){
dd('ERROR: No development found ', $dev); // TODO handle error
}
if (View::exists('development')) {
// dd($dev); this shows development model instance OK!
return view('development' , ['development'=>$dev]);
}
dd("View doesn't exist");
}
I have confirmed the view works with the following route which displays the view correctly;
Route::get('/test', function () { return view('development', ['development'=>Development::find(228)]); });
/resources/views/development.blade.php:
<x-layout>
<h1>Development: {{$development->description}}</h1>
</x-layout>
I have other controllers displaying their views successfully.
I must be missing something obvious, but struggling to spot it!
Any ideas?
Just for reference, I was stupid and returning the view from the show function, but not returning that value back to the route.
public function findShow($countySlug, $locationSlug, $developmentSlug){
$dev=$this->find($countySlug, $locationSlug, $developmentSlug);
return $this->show($dev);
}
Also didnt' help that I didn't put this in the question. Sorry!
In Laravel I'm developing a web app where users will answer true or false questions on separate pages. On each page I'd like to somehow save their answer and in the end save the results to the database.
I could make a POST form on each page which would increment 'points' field in the database if the answer was correct. But that would require many accesses to the database by each user as answers would be saved on each page. Is there a way to store their points to a variable on the server and then save that variable to the database in the end?
I've thought of saving points to session but that wouldn't be safe as sessions can be modified by users.
Right now my controller only returns the intro page.
class QuizController extends Controller {
public function index() {
return view("quiz.pages.1");
}
public function addPoint() {
$points++;
}
public function getPoints() {
return $points;
}
}
And a route to redirect to the next pages.
Route::get('quiz/{page}', function($page) {
return View::make('quiz.pages.' . $page);
});
The way i would achieve this in my opinion is to send hidden inputs to your pages and not save() them until the end. So it would look something like this.
firstpage.blade.php
<input type="text" name="yourinputname">
So then you can pass this input from your controller to the next page without saving it. Something like:
public function index(Request $request) {
$yourVariablename = new YourModelName;
$yourVariableName->yourDatabaseColumnName = $request->input('yourinputname');
return view("quiz.pages.1", [
'anyName' => $yourVariableName
]);
}
Notice there is no save() on the controller yet because we want to save it at the end so we just store it. From this points onward you can just do something like this in the next pages until you get to the point where you want to save.
anotherpage.blade.php
<input type="hidden" name="yourinputname">
When you gather all the data, just match them with your database columns and then save() it. I hope it solves your problem.
I have an resource controller called AppointmentsController and a model called Appointment. In my model I put a few scopes I use to filter results, like so:
public function scopeStatus($query, $statusId)
{
if($statusId)
{
return $query->where('status_id', '=', $statusId);
}
else {
return false;
}
}
I have this for month, year, label, and a few other settings. I call my query in the controller, like so:
$appointments = Appointment::latest('start')->status($statusId)->get();
All these variables ($statusId, $labelId, and so on) are set using dropdowns. Once I select another value in the dropdown, I will be redirected to my view again, with the correct value set and with the correct query.
The thing I'm struggling with how to set the $statusId (and other variables). If I use a post request I need to use an extra route (since I am using the store route from the resource route to create appointments). Which would be something like:
Route::post('appointments/whatever', 'AppointmentsController#index');
Or:
Route::post('appointments/whatever', 'AppointmentsController#whatever');
So I would fetch the $request values and then update my query accordingly, and then return the view again. Another thing I could do, is store the values in a session (not my preferred way). I could also update the values in the database, then go back to the index method, grab the results from the database and then update my view again.
Any thoughts how I should go about doing this? I can get it to work the amateuristic way but I want to learn how to do this in a proper manner, maybe there is even something I haven't thought of yet or maybe even my code is not good enough. Anything that helps me go in the right direction will answer my question.
I have a route that needs to be redirected to another page if the data they're pulling doesn't exist. The route is:
Route::get('{link}/{data}', 'LinkController#getLink');
Where {link} and {data} are model bound with:
Route::model('link', 'Link');
Route::model('data', 'Data');
As is, when the data for this link doesn't exist it 404's, and if it does exist, it's taken to the page as it should. What I would like to do is redirect to another page if the link would otherwise 404. I've found suggestions on how to do this globally, but I only want it to happen on this one route.
Any ideas?
// Link Controller
public function getLink($linkId, $dataId)
{
if ( is_null($link) or is_null($data) ) {
return Redirect::to('some/path');
}
}
If either of the passed models are null when it hits your controller method, just redirect them. As for your /{link} route that you refer to but don't show code for, do something similar in whatever closure/controller you handle that in.
Get rid of the model binding - you've left the cookie cutter realm.
Route::get('{link}/{data?}', 'LinkController#getLink');
// note I made the data ^ parameter optional
// not sure if you want to use it like this but it's worth pointing out
Do all of the model checking in the controller, something like this:
public function getLink($linkId, $dataId)
{
$link = Link::find($linkId);
$data = Data::find($dataId);
if(is_null($link)){
throw new NotFoundHttpException;// 404
}
elseif(is_null($data)){
return Redirect::to('some/view');// redirect
}
// You could also check for both not found and handle that case differently as well.
}
It's hard to tell from your comments exactly how you'd like to treat missing link and/or data records, but I'm sure you can figure that out logically. The point of this answer is that you don't need to use Laravel's model binding since you can do it yourself: find the record(s) else redirect or 404.
I'm a newbie to codeigniter and I'm attempting to write a function that would basically save a name and url to session data whenever you visited a certain page, then report it back in a small widget on the screen.
It's supposed to work as a kind of history function for what pages have been accessed and in what order. So far when working with test data it works great! however I'm trying to figure out how I can call the "add" function on each page that is visited, so we can supply the name and url of that page that was visited. Is there any way to do this? Or is there any way to report back a set of variables such as a name and url for a page after you visit it?
For example: say I visit page1, page2, page3, and page6 and I want each of those to show up in my history function. On each of those pages I would load the history view, and I would want to call the history's controller function "add(name,url)" and fill it in something like this:
add('page1','page1.php')
But I know that you're not supposed to access the controller from the history because that's not the way it's supposed to be done, however I cannot think of any better way to do this. Any help would be appreciated.
I don't know why dont you call this on every controller.
but if you want to call a function of the current controller, you have to get the instance of the current controller this way:
<?php
$CI =& get_instance();
$CI->method($param);
?>
the easiest way to do this would be to put a method in the constructor of your class. that way it will always run first thing, no matter what you are doing. remember that anything you can do in the controller -- sessions, validation, etc -- you can do in a model.
function __construct() {
parent::__construct();
// load model that has page tracker methods
$this->load->model( 'pagetracker_m' );
// call the method to track the pages, and have it return the results
if ( ! $this->history = $this->pagetracker_m->_trackpage(); ) {
echo 'Error getting history ' ; }
} // end construct
function something() {
// pass history to $data, then use echo $history->firstpage, etc in view
$data['history'] = $this->history ;
// call your view etc
}