我有模特:
public function up()
{
Schema::create('tourist_objects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 255);
$table->text('description');
$table->bigInteger('city_id')->unsigned();
$table->foreign('city_id')->references('id')->on('cities')->onDelete('cascade');
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->engine = "InnoDB";
});
}
我要使用此组件https://github.com/kristijanhusak/laravel-form-builder并创建表格。 如何创建此表单:
<form method="POST" action="http://mydomain.pl/forms">
<input name="_token" type="hidden" value="xxxxxxxxxxxxxx">
<div class="form-group">
<label for="name" max-length="255" class="control-label">Name</label>
<input type="text" class="form-control" id="name" value="Name from model">
</div>
<div class="form-group">
<label for="lyrics" class="control-label">Description</label>
<textarea name="lyrics" class="form-control">description value from data base</textarea>
</div>
<input type="submit" value="Save form">
</form>
?
我可以问一个有关我的数据的例子吗?
答案 0 :(得分:0)
您可以使用命令行创建一个表单类:
php artisan make:form Forms/SongForm --fields="name:text, description:textarea, city_id:number, user_id:number"
如果您要用相关模型中的数据填充city_id和user_id字段,则可以使用选择字段:http://kristijanhusak.github.io/laravel-form-builder/field/choice.html
答案 1 :(得分:0)
如果要使用城市模型填充下拉菜单,则应使用实体类型。这是一个示例,假设您具有模型App \ Models \ City
$this->add('city_id', 'entity', [
'class' => 'App\Models\City',
]);
如果要自定义查询:
$this->add('city_id', 'entity', [
'class' => 'App\Models\City',
'query_builder' => function (App\Models\City $city) {
return $city->where('active', 1);
}
]);
更多信息,请访问http://kristijanhusak.github.io/laravel-form-builder/field/entity.html