Laravel 4一对多关系插入

时间:2015-05-12 16:34:14

标签: php laravel-4 eloquent

我正在使用Laravel 4.我有一个名为货运的表,它与许多物品有关。

这是我的代码

装运控制

class ShipmentControl extends BaseController{

public function init(){
    $places=Places::lists('place','id');

    return View::make('Shipment.shipmentadd')
    ->with('places',$places);

}

public function save(){

    //echo "Im HERE";

 //$shipment=Input::all();

    $rules=array(
        'initials'=>'required',
        'surname'=>'required',
        'rece_initials'=>'required',
        'rece_surname'=>'required',
        'email'=>'email',
        'rece_email'=>'email',
        'nort_email'=>'email',
        );

    $v=Validator::make(Input::all(),$rules);

    if($v->fails()){

        return Redirect::to('shipment')->withInput()->withErrors($v);
    }
    $shipment=new Shipment;

    $shipment->initials=Input::get('initials',null);
    $shipment->surname=Input::get('surname',null);
    $shipment->addrsss=Input::get('address',null);
    $shipment->email=Input::get('email',null);
    $shipment->telephone=Input::get('telephone',null);
    $shipment->reciver_initials=Input::get('rece_initials',null);
    $shipment->reciver_surname=Input::get('rece_initials',null);
    $shipment->reciver_address=Input::get('rece_address',null);
    $shipment->reciver_email=Input::get('rece_email',null);
    $shipment->reciver_telephone=Input::get('rece_telephone',null);
    $shipment->notfy_name=Input::get('nort_name',null);
    $shipment->notify_mail=Input::get('nort_email',null);
    $shipment->notify_telephone=Input::get('nort_telephone',null);
    $shipment->port_of_loading=Input::get('port_of_loading',null);
    $shipment->vessle=Input::get('vessel',null);
    $shipment->eta=Input::get('eta',null);
    $shipment->place_of_collection=Input::get('place',null);

    if($shipment->save()){

        $items=Session::get('items');
        foreach ($items as $value) {
            # code...
            //array_unshift($value,$shipment->id());
            $Items=new Items;

            $Items->shipment_id=$shipment->id();
            $Items->type=$value['type'];
            $Items->qty=$value['qty'];
            $Items->height=$value['height'];
            $Items->weight=$value['width'];
            $Items->lenght=$value['length'];
            $Items->save();
        }

    }

    Session::flush();



 }
}

货件型号

lass Shipment extends Eloquent{

 protected $table = 'shipment';
 public $timestamps = false;

  public function places(){

    return $this->belongsTo('Places','place_of_collection');
 }

 public function items(){
    return $this->hasMany('items','shipment_id');
 }

}

和此项目模型

class Items extends Eloquent{

 protected $table = 'items';
 public $timestamps = false;

public function shipment(){
    return $this->belongsTo('Shipment','shipment_id');
 }
}

我正在使用会话来保存项目。所以,当我尝试插入细节时,它会向我显示此错误

Call to undefined method Illuminate\Database\Query\Builder::id()

但是发货细节会添加到表格中。我用谷歌搜索但我找不到解决方案。什么是Build :: id是..?

0 个答案:

没有答案