duangejian6657 2018-10-12 07:47
浏览 29
已采纳

表单更新数据库模型记录的关系

I want to know how to up date a model's relationship using a frontend form. I looked at the documents and found:

Alternatively you may set the relationship using the primary key, this is useful when working with HTML forms.

// Assign to author with ID of 3
$post->author = 3;

// Assign comments with IDs of 1, 2 and 3
$post->comments = [1, 2, 3];

$post->save();

the backend form to update the relationship works fine. This is my code and where I am getting ID as a value but it doesn't seem to affect the relationship field. Help would be great thanks!

    $project = new Projects(); 
    $project->name = Input::get('name');
    $project->price = Input::get('price');
    $project->work = Input::get('work');
    $project->client = Input::get('client');
    $project->slug = $slug;
    $project->save();
    Flash::success('Estimate Added!');
    return Redirect::refresh();

This is the Pivot Table:

public function up()
    {
        Schema::create('brandon_invoice_ip', function($table)
        {
            $table->engine = 'InnoDB';
            $table->integer('invoices_id');
            $table->integer('projects_id');
            $table->primary(['invoices_id','projects_id']);
        });
    }

    public function down()
    {
        Schema::dropIfExists('brandon_invoice_ip');
    }
}

Here is the model relationship:

public $hasOne = [
        'client' => 'Brandon\Invoice\Models\Clients'
    ];

This is the front end form: The values are correct according to their ID.

<div class="uk-margin uk-first-column">
    <label class="uk-form-label" for="client">Client</label>
    <div class="uk-form-controls">
        <select class="uk-select" name="client">
          <option value="1">Brandon</option>
          <option value="2">Sanitary Ostomy System</option>
        </select>
    </div>
</div>

Image of the relationship in the builder.

  • 写回答

2条回答 默认 最新

  • duandang2123 2018-10-12 23:52
    关注

    I think you need to add belongsTo relationship instead of hasOne

    public $belongsTo = [
        'client' => 'Brandon\Invoice\Models\Clients'
    ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改