Nidal
1 week ago

Is there a better way to get model id before it's created ?

Posted 1 week ago by Nidal

Here I'm creating a page that has ['title', 'body', 'cover']

public function store(Request $request)
    {
        $attributes = request()->validate([
            'title' => ['required'],
            'body' => ['required'],
            'cover' => ['image'],
        ]);
        
        $page = Page::create($attributes);

        $page->update([
            'cover' => $attributes['cover']->store('/pages/' . $page->id . '/cover/', 'public'),
        ]); 

        return redirect($page->path());
    }

In the store method i create the page with only the title and body and after updated the cover field. I do this to have accurate access to the pages ID to be able to create a folder dedicated to that page in storage.

Is there a better way or is this good enough ?

Please sign in or create an account to participate in this conversation.