表单更新数据库模型记录的关系
Form Update Database Model Record's Relationship
我想知道如何使用前端表单更新模型的关系。我查看了文档,发现:
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();
更新关系的后端表单工作正常。这是我的代码,我在其中获取 ID 作为值,但它似乎并不影响关系字段。非常感谢帮助!
$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();
这是枢轴 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');
}
}
模型关系如下:
public $hasOne = [
'client' => 'Brandon\Invoice\Models\Clients'
];
这是前端表单:值根据其 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.
使用以下方式设置客户端后:
$project->client = Input::get('client');
您需要使用以下方式保存更改:
$project->save();
假设您的表格和模型已正确设置,以上应该有效。如果没有,您需要 post 表的结构和其余代码。
我认为你需要添加 belongsTo 关系而不是 hasOne
public $belongsTo = [
'client' => 'Brandon\Invoice\Models\Clients'
];
我想知道如何使用前端表单更新模型的关系。我查看了文档,发现:
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();
更新关系的后端表单工作正常。这是我的代码,我在其中获取 ID 作为值,但它似乎并不影响关系字段。非常感谢帮助!
$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();
这是枢轴 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');
}
}
模型关系如下:
public $hasOne = [
'client' => 'Brandon\Invoice\Models\Clients'
];
这是前端表单:值根据其 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.
使用以下方式设置客户端后:
$project->client = Input::get('client');
您需要使用以下方式保存更改:
$project->save();
假设您的表格和模型已正确设置,以上应该有效。如果没有,您需要 post 表的结构和其余代码。
我认为你需要添加 belongsTo 关系而不是 hasOne
public $belongsTo = [
'client' => 'Brandon\Invoice\Models\Clients'
];