简体   繁体   English

如何以同一形式将数据提交到多个表

[英]How to submit data to multiple tables in the same form

I have 2 tables: listings and listings_specifications 我有2个表:listings和listings_specifications

Listings table 列表表

  • id ID
  • type 类型
  • status 状态
  • location 位置
  • specifications_id specifications_id

Listings_specifications table Listings_specifications表

  • id ID
  • listing_id listing_id
  • swimming_pool 游泳池
  • water_well 水井

I need to select the specifications (checkboxes) on the same form with which I add a listing. 我需要在添加列表的同一表格上选择规格(复选框)。 I have created all the forms, views, models, controllers but I think I got some logic wrong. 我已经创建了所有的表单,视图,模型,控制器,但我认为我弄错了一些逻辑。

Listing.php model Listing.php模型

protected $table = 'listings'; protected $ table ='列表';

public function contact()
{
   return $this->BelongsTo('contacts');
}
public function specifications()
{
   return $this->BelongsTo('listings_specifications');
}

Specification.php model Specification.php模型

protected $table = 'listings_specifications'; protected $ table ='listings_specifications';

public function listings()
{
   return $this->BelongsTo('listings');
}

ListingsController.php (where the data gets saved in the database) ListingsController.php(将数据保存在数据库中)

$listing = new Listing;
                $contact = new Contact;
                $listing->status = Input::get('status');
                $listing->listingfor = Input::get('listingfor');
                $listing->propertystatus = Input::get('propertystatus');
                $listing->propertytype = Input::get('propertytype');
                $listing->userid = Auth::user()->id;
                $listing->location = Input::get('location');
                $listing->contact_id = $contact_id;

                $listing->save();

                $specifications = Specification::find($id);

                if( $listings->save() ) {
                        $specifications = new Specification;
                        $specifications->listing_id = $id;
                        $specifications->swimming_pool       = Input::get('swimming_pool');
                        $specifications->water_front      = Input::get('water_front');
                        $specifications->save();
                }

I'm getting this error: Undefined variable: id 我收到此错误:未定义的变量:id

Where did I go wrong? 我哪里做错了?

Thank you 谢谢

You use $id in the line $specifications = Specification::find($id); 您可以在$specifications = Specification::find($id);行中使用$id $specifications = Specification::find($id); but you don't define it before. 但是您之前没有定义它。

It looks like you have some logic errors. 看来您有一些逻辑错误。

First of all, you are never setting $id anywhere, but that's okay because you really don't need it. 首先,您永远不会在任何地方设置$id ,但这没关系,因为您确实不需要它。

Remove the $specifications = Specification::find($id); 删除$specifications = Specification::find($id); line because that's not doing anything. 行,因为它什么也没做。

Then change your last section to something like this... 然后将您的最后一部分更改为这样的内容...

            if( $listings->save() ) {
                    $specifications = new Specification;
                    $specifications->swimming_pool       = Input::get('swimming_pool');
                    $specifications->water_front      = Input::get('water_front');
                    $listing->specifications()->save($specifications);
            }

$listing->specifications()->save($specifications); will automatically save the new specification with the correct listing_id for you. 会自动为您保存带有正确的listing_id的新规范。

Modify your Listing model's specifications relationship to... 修改Listing模型的specifications关系以...

public function specifications()
{
   return $this->hasMany('Specification');
}

I'm assuming here one listing can have many specifications. 我假设这里的清单可以有很多规格。 If not, you can easily just change that to a hasOne . 如果没有,您可以轻松地将其更改为hasOne

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM