Relationship not returning fields

Posted 2 years ago by madsynn

Hello everyone, Hope you can help me.

Windows 10 / Homestead Laravel 5.1

I have a ecommerce project i am building and here is my problem.

I have the following tables:

  1. products
 Schema::create('products', function (Blueprint $table)
            $table->string('manufacturer')->default('The Grace Company');

            $table->string('lang', 255);
  1. prices
    Schema::create('prices', function (Blueprint $table)
            $table->decimal('price', 11, 2)->default('0.00');
            $table->string('model', 12)->nullable();
            $table->string('sku', 12)->default('000000');
            $table->string('upc', 13)->default('000000');
            $table->engine = 'InnoDB';


I have the following relationships product.php

    public function productPrice() {      return $this->hasMany(Price::class);    }


 public function product()    {        return $this->BelongsToMany(Product::class);    }

My problem is i cannot get my pricing to store to the alternate table pricing here is my controller store function.

public function store(Request $request) {
$this->validate($request, [
'name' => 'required',
$dest = 'testing/';
$name  = str_random(4) . '_' . $request->file('thumbnail')->getClientOriginalName();
$request->file('thumbnail')->move($dest, $name);
$product = $request->all();
$product['thumbnail'] = '/' . $dest . $name;
$product = Product::create($product);

if ($request->has('productPrices')) {
foreach ($request->productPrices as $price){
if (!empty($price['price'])){
$price = Price::create($request->all());

foreach ($request->categories as $category_id)
            CategoryProduct::create(['category_id' => $category_id, 'product_id' => $product->id]);

FlashAlert()->success('Success!', 'The Product Was Successfully Added');

        return \Redirect(getLang() . '/admin/products');

I can store the product just fine but i cannot get it to store the product pricing table data.

any help would be greatly appreciated.

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.