Php Laravel 4一对多关系插入

Php Laravel 4一对多关系插入,php,laravel-4,eloquent,Php,Laravel 4,Eloquent,我用的是拉威尔4。我有一个名为Shipping的表,它与许多项目相关 这是我的密码 装运控制 class ShipmentControl extends BaseController{ public function init(){ $places=Places::lists('place','id'); return View::make('Shipment.shipmentadd') ->with('places',$places); } public

我用的是拉威尔4。我有一个名为Shipping的表,它与许多项目相关

这是我的密码

装运控制

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是什么?

试试
$shipping->id,而不是
$shipping->id()。这将从模型返回id属性。谢谢你,罗伯特。它起作用了。。