1
votes

I want to upload a photo with some posts.

This is my controller

public function store(WisataRequest $request)
{
  $input = $request->all();

  if ($request->hasFile('gambar')) {
    $gambar = $request->file('gambar');
    $filename = time() . '.' . $gambar->getClientOriginalExtension();

    if ($request->file('gambar')->isValid()) {
      Image::make($gambar)->resize(300, 300)->save(public_path('/upload/gambar/'.$filename));
      $input->gambar = $filename;
      $input->save();
    }
  }

  $wisata = Wisata::create($input);
  Session::flash('flash_message', 'Berhasil Terkirim');
  return redirect('admin_wisata');
}

But when it runs i found an error Attempt to assign property of non-object

3
And this error is on which line?ceejayoz
what is WisataRequest?I think it should only be Request. Also declare it at the top : use Illuminate\Http\Request;YaSh Chaudhary
@YaShChaudhary that is a user-defined Request built on top of Request. that's perfectly okay.Wreigh

3 Answers

0
votes

$input variable is not an object, it is an array. You can try accessing gambar in $input by doing $input['gambar']

0
votes

Change

$input->gambar = $filename;
$input->save();

To

$input['gambar']= $filename;
0
votes

You can put

$input['gambar']= $filename;

Instead of

$input->gambar = $filename;
$input->save();

OR

public function store(WisataRequest $request)
{

  $wista = new Wista;
  $wist->name = $request->name;
  -----
  $wista->save();

  if ($request->hasFile('gambar')) {
    $gambar = $request->file('gambar');
    $filename = time() . '.' . $gambar->getClientOriginalExtension();

    if ($request->file('gambar')->isValid()) {
      Image::make($gambar)->resize(300, 300)->save(public_path('/upload/gambar/'.$filename));
      $wista->gambar = $filename;
      $wista->save();
    }


  }


  Session::flash('flash_message', 'Berhasil Terkirim');
  return redirect('admin_wisata');
}