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:


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


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


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?

图片转代码服务由CSDN问答提供 功能建议

我没有使用Laravel 4的 Eloquent ORM ,并愿意将我的模型与Edit绑定 使用模型绑定进行表单。 我的代码如下:


  Route :: get('/',  'HomeController @ index'); 
Route :: get('transactions','Exchange @ index'); 
 // Route :: get('details / {exchangeID}','Exchange @ details'); 
Route :: get('details / {exchangeID?}',function($ exchangeID = 0)
返回App :: make('Exchange') - &gt  ; details($ exchangeID); 
Route :: get('admin','Exchange @ dashboard'); 
 //路由:: 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')); 


 < 代码>公共函数编辑(ExchangeModel $ exchange)
返回View :: make('exchangeedit') - &gt; with('exchange',$ exchange); 


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


  public static function all()
返回DB :: select('select * from Exchanges'); 

但是当我访问时: http:// localhost / path / site / public / admin / exchange / edit / 4 它认为是 404并重定向到404页面。 我哪里错了?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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. :)

    点赞 打赏 评论
  • dph58509 2014-01-25 12:44

    No, you can't use a model binding without Eloquent ORM because in Route::model() method there is something like this

    if ($model = with(new $class)->find($value))
        return $model;

    So, when you use following code to bind a model

    Route::model('exchange', 'ExchangeModel');

    The model method of the router class does something like this

    $model = new $class; // Here $class is your ExchangeModel
    $model->find(($value) // The id passed to the url {exchange}
    if($model) return $model;

    So, when you are not extending the Eloquent ORM in your model there is no such method like find is available because Eloquent is an alias of Illuminate\Database\Eloquent\Model class and find method belongs to this Model class.

    Update : As an alternative, you may try something like this. Add a find method in your model like this

    class Exchange {
        public static function find($id)
            $user = DB::table('users')->where('id', $id)->get();
            if($user) {
                $user = new \Illuminate\Database\Eloquent\Collection($user);
                return $user->first();
            return false; // or not found exception

    This way, you can still use Form::model() in your form. If you don't want use static method you can change it but can't use :: method call.

    BTW, you may also use something like this (can manually pass the model to the form)

    public function edit($id)
         // Manually load the model and pass to view
         $exchange = Exchange::find($id);
         return View::make('exchangeedit')->with('exchange', $exchange);

    So, in this case your route should be normally (without model binding)

    Route::get('admin/exchange/edit/{exchange}', ['uses' => 'Exchange@edit', 'as' => 'exchange.edit']);
    点赞 打赏 评论

相关推荐 更多相似问题