Ezra Ezra - 3 months ago 463
PHP Question

laravel 5.2 | upload file - Call to a member function getClientOriginalName() on null

i tried to upload profile picture pict but i got error "Call to a member function getClientOriginalName() on null"

this is my method :

$data = $request->input('fotodosen');
$photo = $request->file('fotodosen')->getClientOriginalName();
$destination = base_path() . '/public/uploads';
$request->file('fotodosen')->move($destination, $photo);
$data['fotodosen'] = $photo;
Dosen::create($data);


create :

{!! Form::open(array('fotodosen'=>'create', 'method'=>'POST', 'files'=>true, 'url'=>'uploads')) !!}
{!! Form::file('image') !!}
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-user"></i> Register
</button>
{!! Form::close() !!}


already edit method to :

$photo = $request->file('fotodosen')->getClientOriginalName($photo);


still got that error. what am i missing?

UPDATE :

public function store(CreateDosenRequest $request)

{



$user = User::create([
'name' => $request->input('name'),
'username' => $request->input('username'),
'email' => $request->input('email'),
'password' => $request->input('password'),
'admin' => $request->input('admin'),
]);

$dosen = Dosen::create([
'iddosen' => $request->input('iddosen'),
'nipy' => $request->input('nipy'),
'namadosen' => $user->name,
'user_id' => $user->id,
'alamatdosen' => $request->input('alamatdosen'),
'notelpdosen' => $request->input('notelpdosen'),
'tempatlahirdosen' => $request->input('tempatlahirdosen'),
'tanggallahirdosen' => $request->input('tanggallahirdosen'),
'agamadosen' => $request->input('agamadosen'),

]);
if ($request->hasFile('image')) {
$data = $request->input('image');
$photo = $request->file('image')->getClientOriginalName();
$destination = public_path() . '/uploads/';
$request->file('image')->move($destination, $photo);
$data['fotodosen'] = $photo;
Dosen::create($data);
}

Answer

You have the file name as image try to use image instead of fotodosen

$photo = $request->file('image')->getClientOriginalName();

Full code

$data = $request->input('image');
$photo = $request->file('image')->getClientOriginalName();
$destination = base_path() . '/public/uploads';
$request->file('image')->move($destination, $photo);

You can check for the file like,

if ($request->hasFile('image')) {
    // your code here
}

From Http Requests and an article file upload in laravel 5