Eloquent ORM - update and delete not working

2019-07-23 19:45发布

问题:

I am working on CRUD for my first Laravel project. Displaying and showing items is working fine.

I tried to update the entry with Query to confirm that I can change values in the table and it worked:

DB::update("UPDATE seasons SET title = 'foo' WHERE ID = 1");

My Problem is that neither updating nor deleting entries will work.

<?php
class SeasonAdminController extends \BaseController
{ 
    // WORKS
    public function store()
    {
        $season = new Season;
        $season->title = Input::get('title');
        $season->save();

        Session::flash('message', 'success!');
        return Redirect::to('backend/season');
    }

    // NOT WORKING
    public function update($id)
    {
        $season = Season::find($id);
        $season->title = Input::get('title');
        $season->save();

        Session::flash('message', 'success!');
        return Redirect::to('backend/season');
    }

    // NOT WORKING
    public function destroy($id)
    {
        Season::destroy($id);

        Session::flash('message', 'success!');
        return Redirect::to('backend/season/');
    }
}

My Route is the following:

Route::resource('backend/season', 'SeasonAdminController');

The form-tag from the edit page:

{{ Form::model($season, array('route' => array('backend.season.update', $season->ID), 'method' => 'PUT')) }}

The form for deleting an entry:

{{ Form::open(array('url' => 'backend/season/' . $value->ID, 'class' => 'pull-right')) }}
    {{ Form::hidden('_method', 'DELETE') }}
    {{ Form::submit('Löschen', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}

What am I missing here. I appreciate you help, thank you!

回答1:

The error was that I had "ID" instead of "id" as a primary key in the database table. I am not quite sure why this should not work, but I guess it has to do with the default primary key from the Eloquent Model.



回答2:

public function update($id){
    $inputs = Input::only(array('title'));

    if (!$id->update($inputs)) {
        Session::flash('message', 'Error!');
    }else{
        Session::flash('message', 'success!');
    }
    return Redirect::to('backend/season');
}

public function destroy($id){
    if($id){
        if($id->delete()){
            Session::flash('message', 'Success: Deleted!');
        }else{
            Session::flash('message', 'Error: Not Deleted!');
        }
    }
    return Redirect::to('backend/season');
}

Try it out. By the way, the $id is not the season id, can't use find($id) on it because it's an object.

Edit: You should follow this tutorial https://www.packtpub.com/books/content/laravel-4-creating-simple-crud-application-hours

Because you do not yet understand how to use models in routes. Take special attention on how forms are built.



回答3:

Check your model

class Session extends Model{
    protected $table = 'session '; //DB table name
    protected $primaryKey = 'Id';  //Primary key Name... some time ORM cant identify the primary key 
}