madsynn
91
34
Laravel

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->increments('id');
            $table->string('status')->nullable();
            $table->string('availability')->default('Available');
            $table->string('slug')->nullable();
            $table->string('name')->nullable();
            $table->string('subtitle')->nullable();
            $table->string('manufacturer')->default('The Grace Company');
            $table->longText('details');
            $table->longText('description');

            $table->string('thumbnail')->nullable();
            $table->string('photo_album')->nullable();
            $table->dateTime('pubished_at')->index();
            $table->string('lang', 255);
            $table->timestamps();
            $table->softDeletes();
        });
  1. prices
    Schema::create('prices', function (Blueprint $table)
        {
            $table->increments('id');
            $table->integer('product_id')->unsigned()->index();
            $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->bigInteger('quantity')->default('99');
            $table->string('alt_details');
            $table->timestamps();
            $table->softDeletes();
            $table->engine = 'InnoDB';
            $table->foreign('product_id')->references('id')->on('products')->onUpdate('cascade')->onDelete('cascade');

        });

I have the following relationships product.php

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

price.php

 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.

Reply to

Use Markdown with GitHub-flavored code blocks.