Привязка модели формы Laravel, связанное значение таблицы

Речь идет о поле ввода и получении связанных данных с использованием привязки 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() }}

приложение/контроллеры/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');
  }

}

person quantme    schedule 23.10.2013    source источник


Ответы (2)


Вы уверены, что на вашем $product есть данные?

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

    $user = new User;
    $user->email = '[email protected]';
    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() }}

Результат:

введите здесь описание изображения

person Antonio Carlos Ribeiro    schedule 24.10.2013
comment
Извините за задержку; обдумывая идею API. Принимая курс и убеждает, попробуем ваш - person quantme; 21.05.2019

Привязка модели формы 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() }}
person Mas Hary    schedule 25.02.2017