2017-08-21 1 views
0

Modell 1:Wie füge ich eine Tabelle hinzu, deren Fremdschlüsselwert sich auf eine andere Tabelle in Laravel bezieht?

namespace App; 
use Illuminate\Database\Eloquent\Model; 

class productDescription extends Model 
{ 
    protected $table="ProductDescription"; 
    protected $connection="mysql"; 

    public function productPricing() 
    { 
     return $this->belongsTo(priceInfo::class); 
    } 
    public function salesPackage() 
    { 
     return $this->hasMany(packageModel::class); 
    } 
} 

Model2:

class packageModel extends Model 
{ 
    // 
    protected $table="subSalesPackage"; 
    protected $connection="mysql"; 

    public function product_description(){ 
     return $this->belongsTo(productDescription::class); 
    } 
} 

Controller:

public function addProductDetails(Request $formdescription,$dataId) 
{ 
    $description=new productDescription; 
    $description->deviceCategoryId=$dataId; 
    $description->productdescriptionid=$this->getproductDescriptionId(); 
    $description->modelName=$formdescription->input('mname'); 

    $description->batteryType=$formdescription->input('batteryType'); 
    //$description->salesPackage =$formdescription->input('package'); 
    $description->skillSet =$formdescription->input('skillSet'); 
    $description->Colour=$formdescription->input('colour'); 
    $description->Material =$formdescription->input('material'); 
    $description->maxAge=$formdescription->input('maxage'); 
    $description->minAge =$formdescription->input('minage'); 

    //$product->productPricing()-save($priceInfo); 
    //$product->productDetails()->save($description); 
    $description->save(); 

    $salesPackage=new packageModel; 
    $salesPackage->salesPackage=$formdescription->input('package'); 
    **$salesPackage->product_description()->associate($description);** 
    $salesPackage->save(); 
    //echo("success"); 

    return response()->json([ 
     'modelName' => $formdescription->mname, 
     'colour' => $formdescription->colour, 
     'rechargable' => $formdescription->rechargable, 
     'batteryType' => $formdescription->batteryType 
    ]); 

    //$description->product()->associate($priceInfo); 
} 

Migrations-> Produktbeschreibung:

public function up() 
{ 
    // 
    Schema::create('ProductDescription', function (Blueprint $table) { 
     $table->engine='InnoDB'; 
     $table->string('productdescriptionid')->primary(); 
     $table->string('product_id'); 

      $table->string('salesPackage'); 
     $table->timestamps(); 
     $table->index(['productDescriptionId']); 

    }); 
} 

Dies ist meine Migration für erste Tabelle (Modell) .Es hat den Primärschlüssel as'productdescriptionid‘.

Migrations-> subSalespackage

public function up() 
{ 
    // 
    Schema::create('subSalesPackage', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('product_description_id'); 
     $table->string('salesPackage'); 
     $table->foreign('product_description_id')- 
    >references('productdescriptionid')->on('ProductDescription'); 
     $table->timestamps(); 
     $table->index(['id']); 
    }); 
} 

Hier habe ich die productdescriptionid als Fremd key.And bezeichnet, wenn ich diese salespackage Tabelle hinzufügen, sollten die Werte mit dem Wert von productdescriptionid(productDescription) hinzugefügt bekommen.

Aber der Fehler, den ich bekomme, ist nicht in der Lage, eine untergeordnete Zeile hinzuzufügen oder zu aktualisieren.

Antwort

1

Sie sollten dies versuchen:

return response()->json([ 
    'SKUID' => $priceInfo->SKUID, 
    'listingStatus' => $priceInfo->listingStatus, 
    'MRP' => $priceInfo->MRP, 
    'sellingPrice' => $priceInfo->sellingPrice, 
    'id' =>$this->getproductId() 
]); 

Hoffnung diese Arbeit für Sie !!!

Verwandte Themen