mercredi 4 septembre 2019

Belongs To Many with pimary key relation returns null

In my app I want to have a relation between addons and products, where a product can have many addons,

I've created a addons table, product table and addon_product table to save both ids,

Have defined the relations in the models and even gone as far as to create a model addonsProducts to insert the keys in my controller

I want the addons to be shown in my Api resources response when I call my products.

Addon Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Addon extends Model
{
    protected $fillable = ['name','price', 'icon', 'description'];
    protected $table = 'addon_products';

      public function products()
     {
       return $this->belongsToMany(Product::class);
     }
}

Addon Migration

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Addons extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('addons', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('price');
            $table->string('icon');
            $table->string('description');
            $table->timestamps();
        });    
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('addons');
    }
}

Product Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{

  protected $fillable = ['name','brand', 'type', 'icon', 'price', 'description'];
// protected $table = 'products';

  public function stores()
 {
   return $this->belongsToMany(Store::class);
 }
  public function orders()
  {
    return $this->belongsToMany(Order::class);
  }
  public function addons()
    {
      return $this->hasMany(Addon::class);
    }

}

Product Migration

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
      Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('icon');
            $table->string('brand');
            $table->string('type');
            $table->string('price');
            $table->string('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

addonProduct Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AddonProducts extends Model
{
    protected $fillable = ['addon_id','product_ids', 'active'];
    //protected $table = 'addon_product';

      public function products()
     {
       return $this->belongsToMany(Product::class);
     }
     public function addons()
     {
      return $this->hasMany(Product::class);
    }
}

addonProduct Migration

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddonProduct extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('addon_product', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('addon_id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->integer('active')->unsigned();
            $table->timestamps();
        });    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('addon_product');
    }
}

Product resource

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ProductResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
      return [
     'id' => $this->id,
     'productname' => $this->productname,
     'icon' => $this->icon,
     'brand' => $this->brand,
     'type' => $this->type,
     'price' => $this->price,
     'addons' => $this->addon,
     'created_at' => (string) $this->created_at,
     'updated_at' => (string) $this->updated_at,

   ];
    }
}

Addon resource

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class AddonResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'description' => $this->description,
            'price' => $this->price,
            'icon' => $this->icon,
          ];
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire