Laravel" save"总是创造新纪录

时间:2015-03-04 05:58:55

标签: laravel eloquent

我正在使用路由/模型绑定来为博客提供一篇简单的文章。表单正确加载,但是当我尝试编辑现有文章并保存表单时,始终会创建一个新记录。我有dd($article)并且看到+ exists总是设置为false。在我的网址中,“id”显示在uri admin/write/14"

以下是我的相关代码:

public function write(Article $article) {

    if (Request::isMethod('post')) {
        $article = self::handleArticleFile($article);
        self::createOrUpdate($article, Input::all());
        return redirect()->route('admin')->with('message', 'Article Created');
    }

    $data = array(
        'article' => $article,
        'editor' => $this->user->id,
        'user' => $this->user,
    );

    return View::make('admin.writeArticle', $data);
}

public function handleArticleFile(Article $article) {

        if (Request::hasFile('article_image')) {
                $file = Request::file('article_image');
                $file->move(base_path() . '/public/images/', $file->getClientOriginalName());
                $article->article_image = 'images/' . $file->getClientOriginalName();
        }
        return $article;
} 

public function createOrUpdate(Article $article, $input) {

    if ($article) {
        $article->fill($input);
        dd($article);
        $article->save();
    } else {
        $article = Article::create($input);
    }
}

我的相关路线:

Route::model('article', 'App\Article');
Route::resource('article', 'App\Article');

Route::get('/', ['as' => 'homepage', 'uses' => 'LandingController@index']);
Route::get('/contact', ['as' => 'contact', 'uses' => 'LandingController@contact']);
Route::post('/send_contact', ['as' => 'sendContact', 'uses' => 'LandingController@sendContactRequest']);
Route::get('/article/{article}', ['as' => 'article', 'uses' => 'ArticleController@index']);

Route::group(['middleware' => 'auth'], function () {

    Route::group(['prefix' => 'admin'], function () {
        Route::get('/', ['as' => 'admin', 'uses' => 'AdminController@index']);
        Route::any('/write/{article?}', ['as' => 'writeArticle', 'uses' => 'AdminController@write']);
        Route::any('newUser', ['as' => 'newUser', 'uses' => 'AdminController@register']);
        Route::get('/logout', ['as' => 'logout', 'uses' => 'AdminController@logout']);
    });
});

我的相关模特:

use Illuminate\Database\Eloquent\Model;

class Article extends Model {

    protected $fillable = array(
        'id',
        'title',
        'author',
        'created_at',
        'content',
        'subtitle',
        'article_image',
    );

    protected $table = "articles";

    protected $primaryKey = "id";

    public function articleAuthor() {
        return $this->belongsTo('App\User', 'author');
    }
}

以下是dd($article)的输出:

Article {#195 ▼
  #fillable: array:7 [▶]
  #table: "articles"
  #primaryKey: "id"
  #connection: null
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:4 [▶]
  #original: []
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [▶]
  #dates: []
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: false
}

fill()搞砸了吗?

1 个答案:

答案 0 :(得分:1)

我假设您正在使用Laravel 5? 5有方法注入,所以在请求文章时,如果在数据库中找不到匹配的文章,laravel会向你发送一个新的空白实例。

以下内容可以解决您的问题。如果模型中存在模型,则存在属性是一种雄辩的东西,通过使用它,您应该能够确定文章是否需要更新或创建。

if ($article->exists)
{
    $article->update($input);
}
else
{
    $article = Article::create($input);
}