KodaC's avatar
Level 1

Variable in Request is allways empty

I have checked the code several times now and just can't find my error. With three other models everything works as desired.

My route

Route::resource('dynamic-pages', DynamicPageController::class)->except('show');

My form in the edit.blade.php

<form method="POST" action="{{ route('backend.dynamic-pages.update', [$dynamicPage->id]) }}" id="dynamicPageForm">
  @method('PUT')
  @csrf

My edit and update statement in the controller

public function edit(DynamicPage $dynamicPage)
 {
     $dynamicPage->with('translations');

     return view('dynamic-pages.edit')->with([
         'dynamicPage' => $dynamicPage,
     ]);
 }

 public function update(DynamicPageRequest $request, DynamicPage $dynamicPage)
 {
     $dynamicPage->update($request->validated());
     //......
 }

And my DynamicPageRequest file

class DynamicPageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        if (!empty($this->dynamicPage->id)) $dynamicPageId = $this->dynamicPage->id;
        else $dynamicPageId = null;
        dd($dynamicPageId, $this->dynamicPages);
        //.....
    }
}

$this->dynamicPage is always empty. But why?

0 likes
5 replies
tykus's avatar

The Route-Model bound instance should be available using $this->route('dynamicPage')

KodaC's avatar
Level 1

@tykus Thank you. But this is also empty

If I replace my update with request and give me $request and $dynamicPage both have content.

tykus's avatar
tykus
Best Answer
Level 104

@KodaC what is the wildcard parameter for the route; dynamicPage or dynamic_page? I wonder if Route Model binding is working at all?

 public function update(DynamicPageRequest $request, DynamicPage $dynamic_page)

Then in the Request:

$this->route('dynamic_page')
1 like
KodaC's avatar
Level 1

@tykus Thank you very much. With $dynamic_page it works. What is the reason that Laravel only responds to $dynamic_page and not to $dynamicPage?

tykus's avatar

@KodaC because the Route registration separates the multi-word parameter with an underscore.

Mark the thread closed if you're all set.

Please or to participate in this conversation.