vendredi 13 juillet 2018

How to access the auto increment value in store method of resource controller in Laravel

I am making a blog. I want to concatenate the soon to be post id to the end of the post slug to make it unique but I can't find a way to access the auto increment value which was not sent through my form in request variable.

public function store(Request $request)
{
    $this->validate($request, array(
        'title' => 'required|max:255',
        'body' => 'required'
    ));

    $cleaned = preg_replace('/[^a-zA-Z0-9\s]/', '', $request -> title);
    $cleaned = strtolower($cleaned);
    $pieces = explode(" ",$cleaned);
    $slug = implode("_", $pieces);
    $slug  = $slug."_".     <------;      //HERE IS THE PROBLEM

    $post = new Post;
    $post -> title = $request -> title;
    $post -> body = $request -> body;
    $post -> slug = $slug;

    $post -> save();

    Session::flash('success','Post Successful');

    return redirect()->route('posts.show', $post->id);
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire