I'm trying to validate two different types of data in a single axios call: Profile and ProfileSocial. The problem is, when I create a ProfileSocialRequest based on the second model and try to validate it, it returns Call to a member function validated() on null
; if I return the data before attempting to validate it, it returns a valid object.
ProfileSocial
class ProfileRequest extends FormRequest
{
public function rules()
{
return [
'name' => [
'required',
'string',
],
...
'socials' => [
'required',
'array',
'min:1',
],
];
}
}
ProfileSocialRequest
use Illuminate\Foundation\Http\FormRequest;
class ProfileSocialRequest extends FormRequest
{
public function rules()
{
return [
'profile_id' => [
'required',
'integer',
],
'social_type_id' => [
'required',
'integer',
],
'handle' => [
'required',
'string',
],
];
}
}
ProfileController
public function store(ProfileRequest $request)
{
$data = $request->validated(); // this (ProfileRequest) works fine
$sosicalReq = new ProfileSocialRequest($request['socials'][0]); // This returns a valid object: {"social_type_id":1,"handle":"mySocialNetworkHandle","profile_id":1000}
$socialReqData = $sr->validated(); // ERROR: Call to a member function validated() on null
...
}
My question is, why is $socialReq
being read as null when calling validated() if every step of the way it returns a complete object?