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

davy_yg's avatar
Level 27

Call to a member function store() on null

Any clue why I get this error:

Call to a member function store() on null

ProfileController.php

 public function store(Request $request)
	{
	$this->validate($request,[
		'photo' => 'required',
		'nim' => 'required',
		'nama' => 'required',
		'alamat' => 'required'
	]);

	$profile = new Profile;
	// $profile->photo = $request->photo;

	// storage/app/photo/random.jpg
	$path = $request->file('photo')->store('public/photo');
	$profile->photo = $path;

	$profile->nim = $request->nim;
	$profile->nama = $request->nama;
	$profile->alamat = $request->alamat;
	$profile->save();

	Session::flash('flash', 'Data sudah tersimpan');

	return view('admin.add_profile');
	}

add_profile.blade.php

<form method="post" action="{{ url('profile') }}" enctype="multipart/form-data">
@csrf
<div class="form-group">
	<label>Photo</label>
	<input class="form-control" name="photo" type="file" placeholder="Enter email">
</div>
<div class="form-group">
	<label>NIM</label>
	<input class="form-control" name="nim" type="text" placeholder="Enter NIM">
</div>
<div class="form-group">
	<label>Nama</label>
	<input class="form-control" name="nama" type="text" placeholder="Enter Name">
</div>
<div class="form-group">
	<label>Alamat</label>
	<input class="form-control" name="alamat" type="text" placeholder="Enter Address">
</div>
<br>
<button class="btn btn-primary">Submit</button>

ref: https://laracasts.com/discuss/channels/laravel/call-to-a-member-function-store-on-null-3

0 likes
15 replies
Sinnbeck's avatar

Validate that the image is a valid image as well

$this->validate($request,[
		'photo' => 'required|image',
		'nim' => 'required',
		'nama' => 'required',
		'alamat' => 'required'
	]);
Snapey's avatar

not your problem, but NEVER return a view from a post request

davy_yg's avatar
Level 27

Then what should I return? since I have to return to the same page to get the error validation message.

Snapey's avatar

You should return to the route that displayed the form in the first place

davy_yg's avatar
Level 27

Let say I want to return to the store function:

Route::post('profile', 'ProfileController@store')->name('profile.store');

so it will be like:

redirect store();

like that?

Snapey's avatar

No, the route that GETs the form

Sinnbeck's avatar

The easiest thing to do is to return back

return redirect()->back();
davy_yg's avatar
Level 27

For some reason using view still works just fine.

return view('admin.add_profile');
Snapey's avatar

Yes it will show form, but you will have strange errors like when you post the form return the view and the user then presses refresh. Your browser will try doing a post again.

But what do I know ?

davy_yg's avatar
Level 27

Perhaps you are right. Earlier I have double post when trying to add the profile.

Will it works just fine with:

return redirect()->back();

?

What exactly should I write the return?

Sinnbeck's avatar

yes that will work fine. Unless it should return to a specific page

For example it can return to a named route

return redirect()->route('admin.profiles');
Snapey's avatar

Yes, that will work fine, however in most of my crud pages I actually want to return to the index after writing the data, not back to the same form

ruchgupta20's avatar

May be u forget to apply enctype="multipart/form-data" in a form

1 like

Please or to participate in this conversation.