Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How do I display the new page I just created?

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!
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

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...
BOOM!