We help IT Professionals succeed at work.

How do I display the new page I just created?

Bruce Gust
Bruce Gust used Ask the Experts™
on
Here's my code:

    public function store(Request $request)
    {
        //
		$page = $request->isMethod('put') ? Page::findOrFail($request->page_id) : new Page; //ternary IF statement determining whether or not this is a new Page or not
		
		$page->id = $request->input('page_id');
		$page->title=$request->input('title');
		$page->body=$request->input('body');
		
		if($page->save()) {
			//return new PageResource($page);
			return redirect('/display/page/{id}')->with('success', 'Page has been successfully created!');
		}
    }

Open in new window


Two questions:

First Question (and this one is ancillary to what I'm really concerned about, but since I've got a ninja on the phone...)The IF statement that's checking to see whether or not we're editing or inserting a new page - if it is a new page, does Laravel know not to worry about the fact that there isn't an incoming ID? The code works, so we're obviously not generating any errors, but I wanted to clarify that. Does Laravel simply know not to be looking for an ID if there isn't one specified?

Second Question: I want to redirect my user to a page that displays either the new page or the page that they just inserted into the database. How do I pass that variable into the "display/page{id}" URL? How do I do it if it's coming in via the form? How do I do it if was just systemically generated as being a new row in the database?

Thank you!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Hey Bruce,

When you make a request in Laravel, it sends a Verb, such as GET, POST, PUT, DELETE etc, and different verbs can be routed however you like. Generally, to make a new Object (Page in your Case), you would POST the data and to update an existing Object you would PUT the data.

In your code, you're checking to see if the method is PUT. If it is, then it assumes you're updating an existing Page, so it tries to retrieve it from the database before setting the properties. If it's not a PUT, then it creates a new empty Page object before setting the properties. Generally, when creating new objects, you won't have an ID, so $request->input('page_id') will return NULL. When your record is saved with a NULL id, it will be automatically generated (auto-increment field in the DB), and the id property of the Object will be populated for you:

$page = new Page();
$page->title = "My New Page";
$page->save();

echo $page->id; // this will automatically get populated after the save() method

Open in new window

For the redirect, it will dependd on how you have your routes set up. If you have a named route, you could just pass in the $page and it will extract the ID automatically:

return redirect()->route('yourroute', [$page])

Open in new window

If you don't have a named route, I think (can't remember!) that you need to pass in the ID specifically:

return redirect('/display/page/', ['id'=>$page->id])->with('success', 'Page has been successfully created!');

Open in new window

Bruce GustPHP Developer

Author

Commented:
Morning, Chris!

You are a hardcharger, my friend! I don't know what time it is wherever you're coming from, but how do you justify two guys being up at 6:30 in the morning on New Year's Eve cranking out code?

We must be motivated...

I've read through your response and I'm looking forward to implementing your counsel. Your explanation makes complete sense. While I was gravitating to that intuitively, I wanted that confirmed by a mind greater than my own, so thank you for that.

Listen, I'm going to go ahead and close this question. But I've got a couple more questions that deal with validation. If you've got a minute, I would really appreciate your eyes on those.

Stand by...
Bruce GustPHP Developer

Author

Commented:
BOOM!