php - Laravel not adding data to foreign key column value in the database -


i'm not getting error message this, laravel 5.1 not assign values foreign key column in orderdetails table. i've tried multiple different calls , can't figure out what's wrong.

    public function orderproduct(request $request){         //try , find current incomplete order. if find fails, create new order.         $order;         try{             $order = orderheader::where("userid","=", auth::user()->id)             ->where('ordercompleted', '=', false)->firstorfail();         }catch(\illuminate\database\eloquent\modelnotfoundexception $e){             $order = new orderheader;             //find user             $id = auth::user()->id;             $user = user::find($id);             //associate user             $order->user()->associate($user);             //mark order incomplete             $order->ordercompleted = false;             $order->ordersshipped = false;             $order->save();         }         //find matching or create new order detail. update or add details , ade order.         /**         *somewhere in here problem(s)         *the 4 or lines i've commented out different attempts         *to value in database.         */         $orderdetail;         $productid = $request->input("pid");         try{             $orderdetail = $order->orderdetails()->where("productid", "=", $productid)->firstorfail();              //add feilds assignment         }catch(\illuminate\database\eloquent\modelnotfoundexception $e){             $orderdetail = new orderdetail; //  $orderdetail->orderheader()->associate(orderheader::find($order->orderid));             $orderdetail->qtyordered = $request->input("qty");             $orderdetail->product()->associate(product::find($productid)); //          $orderdetail->orderheaderid = $order->orderid; //          $orderdetail->orderheader()->associate($order);         //  $orderdetail->save();         //$order->orderdetails()->save($orderdetail);             //$orderdetail = orderdetail::create(['orderheaderid' => $order->orderid,'productid'=> $request->input("pid"), 'qtyordered' => $request->input("qty")]);         }         return $orderdetail;     //  productcontroller::cartview($order);     } 

the relationship bewtween orderheader , orderdetails follows.

<?php  namespace app;  use illuminate\database\eloquent\model;  class orderdetail extends model {     //     protected $table = "orderdetails";     protected $primarykey = "orderdetails";     protected $fillable = array('orderheaderid, productid, qtyordered');      public function orderheader(){         return $this->belongsto('app\orderheader', 'orderheaderid', 'orderid');     }     public function product(){         return $this->belongsto('app\product', 'productid', 'pid');     } } 

and:

<?php  namespace app;  use illuminate\database\eloquent\model; use illuminate\database\eloquent\relations\hasmany;  class orderheader extends model {     //     protected $table = 'orderheader';     protected $primarykey = 'orderid';     protected $fillable = array('shippingid');        public function orderdetails(){         return $this->hasmany('app\orderdetail','orderheaderid','orderid');     }     public function shippingaddress(){         return $this->belongsto("app\shipping", 'shippingid', 'shippingid');     }     public function user(){         return $this->belongsto("app\user", 'userid');     } } 

edit: using dd($orderdetails) see varible contents without attempting set orderheaderid ejects following:

orderdetail {#172 ▼   #table: "orderdetails"   #primarykey: "orderdetails"   #fillable: array:1 [▼     0 => "orderheaderid, productid, qtyordered"   ]   #connection: null   #perpage: 15   +incrementing: true   +timestamps: true   #attributes: array:2 [▼     "qtyordered" => "1"     "productid" => "7"   ]   #original: []   #relations: array:1 [▼     "product" => product {#187 ▼       #table: "product"       #primarykey: "pid"       #connection: null       #perpage: 15       +incrementing: true       +timestamps: true       #attributes: array:8 [▼         "pid" => "7"         "pname" => "p1"         "pbrand" => "pbrand"         "pcurrenttype" => "ac"         "pvoltage" => "4.5"         "pprice" => "5.5"         "pstock" => "6"         "imagepath" => null       ]       #original: array:8 [▼         "pid" => "7"         "pname" => "p1"         "pbrand" => "pbrand"         "pcurrenttype" => "ac"         "pvoltage" => "4.5"         "pprice" => "5.5"         "pstock" => "6"         "imagepath" => null       ]       #relations: []       #hidden: []       #visible: []       #appends: []       #fillable: []       #guarded: array:1 [▼         0 => "*"       ]       #dates: []       #dateformat: null       #casts: []       #touches: []       #observables: []       #with: []       #morphclass: null       +exists: true       +wasrecentlycreated: false     }   ]   #hidden: []   #visible: []   #appends: []   #guarded: array:1 [▼     0 => "*"   ]   #dates: []   #dateformat: null   #casts: []   #touches: []   #observables: []   #with: []   #morphclass: null   +exists: false   +wasrecentlycreated: false } 

using dd($orderdetail) on $orderdetail->orderheader()->associate(orderheader::find($order->orderid));

orderdetail {#172 ▼   #table: "orderdetails"   #primarykey: "orderdetails"   #fillable: array:1 [▼     0 => "orderheaderid, productid, qtyordered"   ]   #connection: null   #perpage: 15   +incrementing: true   +timestamps: true   #attributes: array:3 [▼     "orderheaderid" => null     "qtyordered" => "11"     "productid" => "7"   ]   #original: []   #relations: array:1 [▼     "product" => product {#187 ▼       #table: "product"       #primarykey: "pid"       #connection: null       #perpage: 15       +incrementing: true       +timestamps: true       #attributes: array:8 [▶]       #original: array:8 [▶]       #relations: []       #hidden: []       #visible: []       #appends: []       #fillable: []       #guarded: array:1 [▶]       #dates: []       #dateformat: null       #casts: []       #touches: []       #observables: []       #with: []       #morphclass: null       +exists: true       +wasrecentlycreated: false     }   ]   #hidden: []   #visible: []   #appends: []   #guarded: array:1 [▼     0 => "*"   ]   #dates: []   #dateformat: null   #casts: []   #touches: []   #observables: []   #with: []   #morphclass: null   +exists: false   +wasrecentlycreated: false } 

using on $orderdetail->orderheaderid = $order->orderid;

    orderdetail {#172 ▼   #table: "orderdetails"   #primarykey: "orderdetails"   #fillable: array:1 [▼     0 => "orderheaderid, productid, qtyordered"   ]   #connection: null   #perpage: 15   +incrementing: true   +timestamps: true   #attributes: array:6 [▼     "qtyordered" => "1"     "productid" => "7"     "orderheaderid" => null     "updated_at" => "2016-04-20 12:53:02.000"     "created_at" => "2016-04-20 12:53:02.000"     "orderdetails" => 13   ]   #original: array:6 [▼     "qtyordered" => "1"     "productid" => "7"     "orderheaderid" => null     "updated_at" => "2016-04-20 12:53:02.000"     "created_at" => "2016-04-20 12:53:02.000"     "orderdetails" => 13   ]   #relations: array:1 [▼     "product" => product {#187 ▶}   ]   #hidden: []   #visible: []   #appends: []   #guarded: array:1 [▼     0 => "*"   ]   #dates: []   #dateformat: null   #casts: []   #touches: []   #observables: []   #with: []   #morphclass: null   +exists: true   +wasrecentlycreated: true } 

using on $orderdetail->orderheader()->associate($order);

    orderdetail {#172 ▼       #table: "orderdetails"       #primarykey: "orderdetails"       #fillable: array:1 [▼         0 => "orderheaderid, productid, qtyordered"       ]       #connection: null       #perpage: 15       +incrementing: true       +timestamps: true       #attributes: array:6 [▼         "qtyordered" => "1"         "productid" => "7"         "orderheaderid" => null         "updated_at" => "2016-04-20 13:00:32.000"         "created_at" => "2016-04-20 13:00:32.000"         "orderdetails" => 15       ]       #original: array:6 [▼         "qtyordered" => "1"         "productid" => "7"         "orderheaderid" => null         "updated_at" => "2016-04-20 13:00:32.000"         "created_at" => "2016-04-20 13:00:32.000"         "orderdetails" => 15       ]       #relations: array:2 [▼         "product" => product {#187 ▼           #table: "product"           #primarykey: "pid"           #connection: null           #perpage: 15           +incrementing: true           +timestamps: true           #attributes: array:8 [▶]           #original: array:8 [▶]           #relations: []           #hidden: []           #visible: []           #appends: []           #fillable: []           #guarded: array:1 [▶]           #dates: []           #dateformat: null           #casts: []           #touches: []           #observables: []           #with: []           #morphclass: null           +exists: true           +wasrecentlycreated: false         }         "orderheader" => orderheader {#177 ▼           #table: "orderheader"           #primarykey: "orderid"           #fillable: array:1 [▼             0 => "shippingid"           ]           #connection: null           #perpage: 15           +incrementing: true           +timestamps: true           #attributes: array:10 [▼             "orderid" => "4"             "userid" => "1"             "shippingid" => null             "ordercompleted" => "0"             "ordersshipped" => "0"             "totalprice" => null             "created_at" => "2016-04-11 14:57:25.000"             "updated_at" => "2016-04-11 14:57:25.000"             "creditcard" => null             "verficationcode" => null           ]           #original: array:10 [▼             "orderid" => "4"             "userid" => "1"             "shippingid" => null             "ordercompleted" => "0"             "ordersshipped" => "0"             "totalprice" => null             "created_at" => "2016-04-11 14:57:25.000"             "updated_at" => "2016-04-11 14:57:25.000"             "creditcard" => null             "verficationcode" => null           ]           #relations: []           #hidden: []           #visible: []           #appends: []           #guarded: array:1 [▼             0 => "*"           ]           #dates: []           #dateformat: null           #casts: []           #touches: []           #observables: []           #with: []           #morphclass: null           +exists: true           +wasrecentlycreated: false         }       ]       #hidden: []       #visible: []       #appends: []       #guarded: array:1 [▶]       #dates: []       #dateformat: null       #casts: []       #touches: []       #observables: []       #with: []       #morphclass: null       +exists: true       +wasrecentlycreated: true     }  using on '$order->orderdetails()->save($orderdetail);' orderdetail {#172 ▼   #table: "orderdetails"   #primarykey: "orderdetails"   #fillable: array:1 [▼     0 => "orderheaderid, productid, qtyordered"   ]   #connection: null   #perpage: 15   +incrementing: true   +timestamps: true   #attributes: array:6 [▼     "qtyordered" => "1"     "productid" => "7"     "orderheaderid" => null     "updated_at" => "2016-04-20 13:17:18.000"     "created_at" => "2016-04-20 13:17:18.000"     "orderdetails" => 16   ]   #original: array:6 [▼     "qtyordered" => "1"     "productid" => "7"     "orderheaderid" => null     "updated_at" => "2016-04-20 13:17:18.000"     "created_at" => "2016-04-20 13:17:18.000"     "orderdetails" => 16   ]   #relations: array:1 [▼     "product" => product {#187 ▶}   ]   #hidden: []   #visible: []   #appends: []   #guarded: array:1 [▼     0 => "*"   ]   #dates: []   #dateformat: null   #casts: []   #touches: []   #observables: []   #with: []   #morphclass: null   +exists: true   +wasrecentlycreated: true } 

edit: adding details on table structure in case helps debugging

create table [dbo].[orderdetails] (     [orderdetailid] int      identity (1, 1) not null,     [orderheaderid] int      null,     [productid]     int      null,     [qtyordered]    int      null,     [created_at]    datetime not null,     [updated_at]    datetime not null,     primary key clustered ([orderdetailid] asc),     constraint [orderdetails_orderheaderid_foreign] foreign key ([orderheaderid]) references [dbo].[orderheader] ([orderid]),     constraint [orderdetails_productid_foreign] foreign key ([productid]) references [dbo].[product] ([pid]) );  create table [dbo].[orderheader] (     [orderid]         int           identity (1, 1) not null,     [userid]          int           not null,     [shippingid]      int           null,     [ordercompleted]  bit           not null,     [ordersshipped]   bit           not null,     [totalprice]      float (53)    null,     [created_at]      datetime      not null,     [updated_at]      datetime      not null,     [creditcard]      nvarchar (50) null,     [verficationcode] nchar (3)     null,     primary key clustered ([orderid] asc),     constraint [orderheader_userid_foreign] foreign key ([userid]) references [dbo].[users] ([id]),     constraint [fk_ordershipping] foreign key ([shippingid]) references [dbo].[shipping] ([shippingid]) ); 

try this:

public function orderproduct(request $request) {     //try , find current incomplete order. if find fails, create new order.     var $order = null;     try{         $order = orderheader::where("userid","=", auth::user()->id)->where('ordercompleted', '=', false)->firstorfail();     }catch(\illuminate\database\eloquent\modelnotfoundexception $e){         $order = new orderheader;         //associate user         $order->userid = auth::user()->id;         //mark order incomplete         $order->ordercompleted = false;         $order->ordersshipped = false;         $order->save();     }     //find matching or create new order detail. update or add details , ade order.     /**      *somewhere in here problem(s)      *the 4 or lines i've commented out different attempts      *to value in database.      */     var $orderdetail = null;     $productid = $request->input("pid");     try{         $orderdetail = $order->orderdetails()->where("productid", "=", $productid)->firstorfail();         //add feilds assignment     }catch(\illuminate\database\eloquent\modelnotfoundexception $e){         $orderdetail = new orderdetail;         $orderdetail->orderheaderid = $order->orderid;         $orderdetail->qtyordered = $request->input("qty");         $orderdetail->productid = $productid;         $orderdetail->save();         //$order->orderdetails()->save($orderdetail);         //$orderdetail = orderdetail::create(['orderheaderid' => $order->orderid,'productid'=> $request->input("pid"), 'qtyordered' => $request->input("qty")]);     }     return $orderdetail;     //productcontroller::cartview($order); } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -