mardi 2 janvier 2018

Create Entity with one to many relationship Laravel

I have a Product class and a Image class. Each product has many Images. I believe I have set up the relationship correctly, but when creating a Product, I'm having trouble creating the Images. I have a function that takes in a request that contains a JSON object containing the Product details, including an array of Images. I convert the JSON into a variable, but if I just save the variable into the database, it just saves the product details, excluding the images. I looked to trid using the following, but the $product variable isn't recognized as a Product type, so therefore, doesn't recognize the images() on the Product class.

$product->images()->save($images)

This is the function used to create the Product:

public function api_create(Request $request)
{
    try {
        $product = new Product;
        $product = json_decode($request->getContent(), true);
        $images = $product['images'];
        return Product::create($product);
    } catch (Exception $exception) {
        return "error";
    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2DTk938
via IFTTT

Aucun commentaire:

Enregistrer un commentaire