简体   繁体   English

Laravel表单模型绑定,相关表值

[英]Laravel form model binding, related table value

The thing is about an input field and retrieving related data using Form::model() binding. 事情是关于输入字段,并使用Form::model()绑定检索相关数据。 How can I do that? 我怎样才能做到这一点? Binding results are empty on text input qty . 文本输入qty上的绑定结果为空。 I'm thinking into do the hack thing from model... is possible from model() ? 我正在考虑是否可以通过model() ...来进行hack工作?

Form (app/views/products/edit.blade.php) 表格(app / views / products / edit.blade.php)

{{ Form::model($product, array(
  'method' => 'PATCH', 
  'route' => array('products.update', $product->id),
  'class' => 'form-inline'
)) }}
{{ Form::label('name', 'Name:') }}
{{ Form::text('name') }}
{{ Form::label('qty', 'Price:') }}
{{ Form::text('qty') }} <!-- here's da thing! -->
{{ Form::submit('Update', array('class' => 'btn btn-info')) }}
{{ Form::close() }}

app/controllers/ProductsController.php 应用程序/控制器/ ProductsController.php

class ProductsController extends BaseController {
  public function edit($id) {
    $product = Product::find($id);
    if (is_null($product)) return Redirect::route('products.index');
    return View::make('products.edit', compact('product'));
  }
}

app/models/ 应用程序/模型/

class Product extends Eloquent {
  public $timestamps = false;
  protected $fillable = array('name');

  public function prices() {
    return $this->hasMany('Price');
  }
  public function images() {
    return $this->morphMany('Image', 'imageable');
  }
}
class Price extends Eloquent {
  protected $table = 'product_prices';
  public $timestamps = true;
  protected $fillable = array('qty');

  public static $rules = array(
    'qty' => 'required|numeric'
  );

  public function product() {
    return $this->belongsTo('Product');
  }

}

Are you sure your $product has data on it? 您确定您的$product有数据吗?

Route::get('/test', function() {

    $user = new User;
    $user->email = 'me@mydomain.com';
    return View::make('test', compact('user'));

});

View (test.blade.php): 查看(test.blade.php):

{{ Form::model($user, array(
  'method' => 'PATCH', 
)) }}
{{ Form::label('email', 'E-mail:') }}
{{ Form::text('email') }} <!-- here's da thing! -->
{{ Form::submit('Update', array('class' => 'btn btn-info')) }}
{{ Form::close() }}

Result: 结果:

在此处输入图片说明

Laravel form model binding, related table value: Laravel表单模型绑定,相关表值:

Route::patch('dashboard/product/{id}', [
    'uses' => 'Dashboard\Product\ProductController@update',
    'as' => 'dashboard.products.update'
]);

in display form edit: 在显示形式中编辑:

{{ Form::model($product, array(
                      'method' => 'PATCH', 
                      'route' => array('dashboard.products.update', $product->id),
                    )) }}
    //enter code here
{{ Form::close() }}

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

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