1

I'm not sure why I'm getting null in return when trying to test if my endpoint works.

The table name's correct but for some reason, it isn't recognizing my file. My table as has more than one column despite my code and was created with php artisan. (I'm just trying to populate filePath column, I'll work on others later). Below's an image for reference
enter image description here

If I get rid of the dd($filePath); which's in my controller - the Postman errors returns:

Integrity constraint violation: 1048 Column 'file_path' cannot be null (SQL: insert into photos (file_path) values (?))

Note: My Laravel code base and React.js code base are completely separated. Anyone have an idea as to why this is happening?

Here's my frontend code:

fileSelectedHandler = event => {
    this.setState({
        selectedFile: event.target.files[0]
    })
}

fileUploadHandler = () => {
    const fd = new FormData();
    fd.append('image', this.state.selectedFile, this.state.selectedFile.name);
    axios.post('http://myendpoint/api/auth/wall-of-fame', fd)
        .then(res => {
            console.log(res);
        })
}

<form action="http://myendpoint/api/auth/wall-of-fame" encType='multipart/form-data' id="login-form" className="form">
            <input type="file" name="file" onChange={this.fileSelectedHandler}/>
            <button onClick={this.fileUploadHandler}>Upload</button>
        </form>

Here's my php controller:

public function store(Request $request){
    $filePath = $request->input('file')->getClientOriginalName();
    $data=array('file_path'=>$filePath);

    // dd($filePath);

    DB::table('photos')->insert($data);
    echo "Record inserted successfully.<br/>";
    echo '<a href = "/insert">Click Here</a> to go back.';
}
cloudyday
  • 183
  • 6
  • 1
    Not sure about the react code but normally `
    ` wouldn't submit a file. https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean would be needed
    – user3783243 Jul 04 '20 at 12:30
  • Have a look at this: https://stackoverflow.com/questions/37161505/laravel-get-name-of-file. – Salim Ibrohimi Jul 04 '20 at 12:34
  • @SalimIbrogimov thanks for sharing. I tried what's suggested but now I'm getting `Call to a member function getClientOriginalName() on null in file`. I'm really not sure what's going on. I've updated my controller code. – cloudyday Jul 04 '20 at 12:45
  • Your form is missing this attr: enctype="multipart/form-data". – Salim Ibrohimi Jul 04 '20 at 12:49
  • @SalimIbrogimov forgot to mention I've tried that as well but still get the same result :( – cloudyday Jul 04 '20 at 12:53
  • I assume you want to upload a file and insert the file's name in DB? Please, check this article: https://www.tutorialspoint.com/laravel/laravel_file_uploading.htm. – Salim Ibrohimi Jul 04 '20 at 12:55
  • Change `$request->input('file')` to `$request->file('file')` – STA Jul 04 '20 at 12:56
  • @cloudyday when you did dd() on filePath does it return the expected result? – jewishmoses Jul 04 '20 at 12:58

1 Answers1

0

When retrive a file from Request, use the file method instead of input method :

$filePath = $request->file('file')->getClientOriginalName();

You may determine if a file is present on the request using the hasFile method:

if ($request->hasFile('file')) {
    $filePath = $request->file('file')->getClientOriginalName();
}

See the official documentation here

STA
  • 30,729
  • 8
  • 45
  • 59