dongyou7739 2014-01-25 08:25
浏览 42
已采纳

我们可以在没有Eloquent ORM的情况下使用Laravel Form Binding吗?

I am not using Eloquent ORM for Laravel 4 and willing to bind my Model with Edit Form using model binding. My code is given below:

Routes.php

Route::get('/', 'HomeController@index');
Route::get('exchanges', 'Exchange@index');
//Route::get('details/{exchangeID}', 'Exchange@details');

//add question to make the parameter option
Route::get('details/{exchangeID?}', function($exchangeID = 0)
{
    return App::make('Exchange')->details($exchangeID);
});

//Admin Dashboard
Route::get('admin', 'Exchange@dashboard');
//Route::get('admin/exchange/edit/{exchangeID?}', function($exchangeID = 0)
//{
//    return App::make('Exchange')->edit($exchangeID);
//});
Route::model('exchange', 'ExchangeModel');
Route::get('admin/exchange/edit/{exchange}', array('uses' => 'Exchange@edit', 'as' => 'exchange.edit'));

Controller Method

public function edit(ExchangeModel $exchange)
    {
             return View::make('exchangeedit')->with('exchange', $exchange);

VIew

{{ Form::model($exchange, array('route' => array('exchange.edit', $exchange->id))) }}
        {{ Form::close() }}

Model

public static function all()
    {
        return DB::select('select * from Exchanges');
    }

However When I visit: http://localhost/path/site/public/admin/exchange/edit/4 It considers it a 404 and redirects to a 404 page. Where am I going wrong?

  • 写回答

2条回答 默认 最新

  • doukan5332 2014-01-26 01:00
    关注

    The 404 error is being generated by your Route model binding Route::model(). If you are not using Eloquent, route model binding won't function as expected without additional work (mainly making sure your desired class has a find() method, which will return the record you need).

    You do not need to use route model binding in order to use Form model binding. These are two different features, neither of which depend on the either.

    Form model binding does NOT require an Eloquent model instance. It can be any array or object with the correct properties / keys. Here's an excerpt from Illuminate\Html\FormBuilder.php that is responsible for finding a model attribute:

    protected function getModelValueAttribute($name)
    {
        if (is_object($this->model))
        {
            return object_get($this->model, $this->transformKey($name));
        }
        elseif (is_array($this->model))
        {
            return array_get($this->model, $this->transformKey($name));
        }
    }
    

    object_get() and array_get() are Laravel helper functions, if you want to look into them further.

    So you do not need an Eloquent model to use form model binding, but you will probably want to use it when using route model binding. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮