Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fahdshaykh's avatar

getting id null in request file

i have a form with single input. this single input name is callsign and should unique in table and my model name is callsign also in the table column name is callsign. when i am trying to get callsign model object with this->callsign for retrieving id but it return form name callsign like test. actually i want to update with unique validation and ID need id for this.

0 likes
8 replies
fahdshaykh's avatar

controller function:

				  public function update(UpdateRequest $request, Callsign $callsign)
					{
   					 dd($request->validated());
					}

request code:

	 public function prepareForValidation(): void
{
    $user_id = auth()->user()->id;
    dd($this->id);

    // replace the data ready for validation
    $this->merge([
        'user_id' => $user_id,
    ]);

}

form input

					<input type="text" id="callsign" name="callsign" tabindex="1"
                                           class="form-control @error('callsign') is-invalid @enderror"
                                           placeholder="@lang('callsign.PlaceholderTitle')"
                                           value="{{ old('callsign', $callsign->callsign) }}"
                                           @if(!$errors->any() or $errors->first('callsign')) autofocus @endif
                                           required
                                           aria-describedby="nameHelp"
                                    >

i want to get id in the request

fahdshaykh's avatar

when i try to change name and then i dump in the request i recieved with function $callsign parameter

Snapey's avatar

why do you want to get id inn the request when its already available everywhere?

fahdshaykh's avatar

i want to update with unique validation.

			return [
        'user_id' => [
            "required",
            'integer'
        ],
        'callsign_name' => [
            "required",
            "unique:callsigns,callsign," . $this->id,
            'string',
            'min:3'
        ]
        
    ];
Snapey's avatar

you are trying to say that callsign must be unique except for the same user?

fahdshaykh's avatar

@Snapey yes! i solved it. its strange for i change the form input name callsign to callsign_name then calling the model object of callsign it received with ID. thanks @snapey

EveAT's avatar

I had the same issue in a Resource Controller. Model binded variable was empty in Edit and Update methods:

// web.php
Route::resource('medias', MediaGroupController::class);

// MediaGroupController.php
public function update(Request $request, MediaGroup $media)
{
    // $media is an empty instance of MediaGroup. 
}

The name of model binded variable must be media_group and not media.

To change the name of variable you must specify it in the route's parameters:

// web.php
Route::resource('medias', MediaGroupController::class, ['parameters' => [
    'media' => 'media_group' //implicit binding type-hinted variable
]]);

// MediaGroupController.php
public function update(Request $request, MediaGroup $media)
{
    // Now $media was binded to the right model instance
}

https://laravel.com/docs/10.x/routing#implicit-binding https://laravel.com/docs/10.x/controllers#resource-controllers

Please or to participate in this conversation.