【问题标题】:Laravel form model binding, related table valueLaravel 表单模型绑定,相关表值
【发布时间】:2013-11-02 11:29:26
【问题描述】:

事情是关于输入字段和使用Form::model() 绑定检索相关数据。我怎样才能做到这一点?文本输入 qty 上的绑定结果为空。我正在考虑从模型中进行黑客攻击...可以从model() 开始吗?

表单(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

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'));
  }
}

应用程序/模型/

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');
  }

}

【问题讨论】:

    标签: php model laravel laravel-4 eloquent


    【解决方案1】:

    你确定你的$product 有数据吗?

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

    查看(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() }}
    

    结果:

    【讨论】:

    • 抱歉耽搁了;思考一个 API 的想法。参加课程并说服,会尝试你的
    【解决方案2】:

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

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

    在显示表单编辑中:

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

    【讨论】:

      猜你喜欢
      • 2013-05-28
      • 1970-01-01
      • 2015-04-06
      • 2023-04-03
      • 1970-01-01
      • 2016-06-11
      • 2013-07-13
      • 2014-12-02
      • 2016-08-25
      相关资源
      最近更新 更多