2 years ago

#58701

test-img

Kaka

Error: Call to a member function store() on null in file

I am adding basic CRUD functionality to this app. Where i use fetch PUT method to update data in the table . I also have an Image for my products to save as well. it give back error:

{message: "Call to a member function store() on null", exception: "Error",…}

exception: "Error"
line: 67
message: "Call to a member function store() on null"`

Note:this is almost similar code i used for adding products/entries into the table and it was working. when i go to line 67 in ProductController

The code is :

    function updateProduct(Request $req) {

    $product = Product::find($req->id);
    $product->name= $req->input('name');
    $product->price= $req->input('price');
    $product->description= $req->input('description');
    $product->file_path= $req->file('file')->store('products');
    $result = $product->save();

    if ($result) {
        return ["result" => "Product has been updated!"];
    }
    else {
        return ["result" => "Update operation failed!"];

    }

}

and the api route is :

Route::post('update', [ProductController::class, 'updateProduct']);

i used this same code to add products to the table and it was working fine and adding entries as well as saving images to products folder.

add product code is :

function addProduct(Request $req) {
    // check validations
    // return nothing (i.e. don't save) if validations fail
    $product = new Product;
    $product->name= $req->input('name');
    $product->price= $req->input('price');
    $product->description= $req->input('description');
    $product->file_path= $req->file('file')->store('products');
    $product->save();

    return $product;
}

And of course my fetch code is:

let $item = {id, name, price, file_path, description }

console.warn($item);
console.warn("that thing up there was item")
console.warn("file_path", file_path);
console.warn("id", id);
 

   
//
console.warn($item);

var requestOptions = {
    method: 'PUT',
    headers:{
        'Accept' : 'application/json',
        'Content-Type' : 'application/json'
    },
    body: JSON.stringify($item)
    
};

that console.warn($item) give back :

{id: '16', name: 'Cream cake', price: '500', file_path: 'products/RkAXPrh09xTATDkjcm12YgNUKKpddHx5BSMa8CtL.jpg', description: 'Good product.'}

which proves that item is not null and it is storing legit data .

I am just stuck with it for whole day by now and didn't figure out a way arround.

php

reactjs

react-fullstack

0 Answers

Your Answer

Accepted video resources