-1

hello im trying to save my filename in database laravel , but it gives me im doing upload image with laravel. The image is uploaded yet the field from my rent database not. This is what happens.

Creating default object from empty value

im using Storage:link

Controller use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage;

public function insertproof(Request $request)
    $id = $request['id'];
    if ($request->hasFile('rent_proof')){
        $filepath = $request->file('rent_proof')->store('app/folder_proof');
    }
    $result = rent::find($id);
    $result->rent_proof=$filepath;
    $result->save();
}

and here is my view and this view is from mymodal

<form method="POST" action="{{route('visitor_rent.insertproof')}}"
    enctype="multipart/form-data">

    {{ csrf_field() }}
    <div class="form-group">
        <input type="file" class="form-control" name="rent_proof"  id="proof">
    </div>
    <button type="submit" class="btn btn-success">Save</button>
</form>

This is my model

public class Rent extends Model
{

    protected $primaryKey = 'id';
    protected $fillable = ['rent_proof'];
}

edit : hello this one that gives me the error

    $result = rent::find($id);
    $result->rent_proof=$filepath; //<--this line is giving me the error
    $result->save();        

thanks for the care is there anything that i miss?

Version laravel 5.5 PHP 7.2

Mich ni
  • 15
  • 1
  • 6
  • 1
    if your -1 help you make your days better its fine. im still a nob and i need guidance, its fine -1 me but at least try to tell me what is my fault. here is same case https://stackoverflow.com/questions/43488754/laravel-5-4-error-creating-default-object-from-empty-value but still not answered too, i need someone to pin point what my missing things, i can continue after that even you dont provide me any code at all.. – Mich ni Oct 04 '18 at 04:02
  • Someone has -1 your question because the question is not obvious. Now tell me what exactly are you gonna achieve and what is the exact error. and I will re-edit your question as the community can understand the real situation. I'm trying to help, and i'm not the one who -1 your question :) – Tharaka Dilshan Oct 04 '18 at 04:09
  • hello sir yes i found that i not bound the error code that whoops throw, thanks for the feed back, it makes me a better person – Mich ni Oct 04 '18 at 04:10
  • In Laravel exception trace, It provides you the exact line number which has thrown the error. – Tharaka Dilshan Oct 04 '18 at 04:11
  • `$result = rent::find($id);` , looks like `$result` is `NULL` ,, are you sure the value `$id` is exist in table ? – Farsheel Oct 04 '18 at 04:13
  • try this code set. `try { $result->rent_proof=$filepath; } catch (\Exception $e) { dd( $result, $filepath ); }` now figure out which one is null? – Tharaka Dilshan Oct 04 '18 at 04:17
  • hello guys thanks for the patient time, after i dd my id. it returns me null. Long story short, the problem is actually reside on my view page within jquery. Which a super nob mistake because i trust that $id = $request['id']; is give me correct id. Thanks guys GBU all. Sorry not replying fast, i dont want to burden you guys too much – Mich ni Oct 04 '18 at 08:17

1 Answers1

1

Creating default object from empty value this error is because id which you have passed doesn't exists in your database.

if you are creating a new record with id and image path

then you should use

$result = new Rent;
$result->rent_proof=$filepath; //<--this line is giving me the error
$result->save();

If you are updating existed one then pass proper id which exists in database

Roshni hegde
  • 423
  • 3
  • 14
  • Hello, thanks for the feedback! the error comes from $id = $request['id']; because im using javascript to transfer data to myModal yet it returns null, so i make it work, sorry for the late reply and newbie mistakes. Thanks and GBU! – Mich ni Oct 04 '18 at 08:20