2017-03-31 128 views
-1

這是create_users_table.php當創建在我nerd_table一個書呆子,我得到以下錯誤

public function up() 
    { 
     Schema::table('users', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('email')->unique(); 
     $table->string('password'); 
     $table->rememberToken(); 
     $table->timestamps(); 
     $table->integer('nerd_level'); 
     }); 
    } 

QueryException in Connection.php line 647: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nerd_level' in 'field list' (SQL: insert into users (name , email , nerd_level , updated_at , created_at) values (dasdasd, [email protected], 1, 2017-03-31 15:41:51, 2017-03-31 15:41:51))

我的代碼,當我嘗試編輯一個書呆子,我得到了以下錯誤:

ErrorException in 17afaeb4afd6b4276e6af6ff12921ab25d74a9cc.php line 22: Trying to get property of non-object (View: /var/www/vhosts/vincentspage.tk/httpdocs/Laravel/resources/views/nerds/edit.blade.php)

這是edit.blade.php我的代碼:

 <!DOCTYPE html> 
    <html> 
    <head> 
     <title>Look! I'm CRUDding</title> 
     <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap 
     /3.0.0 /css/bootstrap.min.css"> 
</head> 
<body> 
<div class="container"> 

    <nav class="navbar navbar-inverse"> 
    <div class="navbar-header"> 
    <a class="navbar-brand" href="{{ URL::to('nerds') }}">Nerd Alert</a> 
    </div> 
    <ul class="nav navbar-nav"> 
    <li><a href="{{ URL::to('nerds') }}">View All Nerds</a></li> 
    <li><a href="{{ URL::to('nerds/create') }}">Create a Nerd</a> 
    </ul> 
</nav> 

<h1>Edit {{ $nerd->name }}</h1> 

<!-- if there are creation errors, they will show here --> 
    {{ HTML::ul($errors->all()) }} 

{{ Form::model($nerd, array('route' => array('nerds.update', 
    $nerd->id), 'method' => 'PUT')) }} 

<div class="form-group"> 
    {{ Form::label('name', 'Name') }} 
    {{ Form::text('name', null, array('class' => 'form-control')) }} 
</div> 

<div class="form-group"> 
    {{ Form::label('email', 'Email') }} 
    {{ Form::email('email', null, array('class' => 'form-control')) }} 
</div> 

<div class="form-group"> 
    {{ Form::label('nerd_level', 'Nerd Level') }} 
    {{ Form::select('nerd_level', array('0' => 'Select a Level', '1' => 
    'Sees Sunlight', '2' => 'Foosball Fanatic', '3' => 'Basement 
    Dweller'), null, array('class' => 'form-control')) }} 
</div> 

{{ Form::submit('Edit the Nerd!', array('class' => 'btn btn-primary')) }} 

{{ Form::close() }} 

</div> 
</body> 
</html> 

當我嘗試刪除一個書呆子這是錯誤:

FatalErrorException in NerdController.php line 128: Call to a member function delete() on null

回報重定向是php線128

public function destroy($id) 
{ 
    // delete 
    $nerd = User::find($id); 
    $nerd->delete(); 

    // redirect 
    Session::flash('message', 'Successfully deleted the nerd!'); 
    return Redirect::to('nerds'); 
} 

此錯誤可能是有利可圖的其他人誰也有類似的問題,讓他們知道什麼改變他們的代碼。或者不要向誰先讀它的人發生類似的錯誤。

任何人都可以幫助我嗎?

親切的問候,

文森特

+0

你能在NerdController.php上顯示第128行嗎?此外,無論刪除被調用,你可以顯示一切,看看如何變量爲空? – Goose

+1

創建表之後,請驗證nerd_level是否以列的形式存在。 – mkaatman

+0

沒有看到你的php代碼和你的sql表的基本結構,這使得它很難幫助,你也使用什麼變種的SQL? – Jpsh

回答

3

,而不是Schema::table使用Schema::create創建新表。您正在使用Schema::table,並且應用程序期望您嘗試修改現有的表。

所以它會是這樣的create_users_table

public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('email')->unique(); 
     $table->string('password'); 
     $table->rememberToken(); 
     $table->timestamps(); 
     $table->integer('nerd_level'); 
     }); 
    } 

信息

QueryException in Connection.php line 647: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nerd_level' in 'field list' (SQL: insert into users (name, email, nerd_level, updated_at, created_at) values (dasdasd, [email protected], 1, 2017-03-31 15:41:51, 2017-03-31 15:41:51))

這個錯誤是因爲你試圖保存記錄到不表有nerd_level字段。所以如果你已經創建了一個遷移並且首先運行它,然後當你添加該字段時它不會工作。因此您需要在對文件進行任何更改之前回滾遷移,或者應該創建一個新遷移,以便將nerd_level字段添加到現有表中。

第一方法

  • 回滾遷移運行下面的命令,直到create_users_table回滾或聲明的步驟。看到文檔 Rolling Back Migrations

    PHP工匠遷移:回滾

  • 帶來所需的更改中添加下列代碼。

    $ table-> integer('nerd_level');

  • 運行遷移再次

    PHP人員遷移

第二方法(優選方式)。見文檔Modifying Columns

  • 離開現有create_users_table,因爲它是不$table->integer('nerd_level');
  • 創建一個新的單獨遷移只需添加新列。

ErrorException in 17afaeb4afd6b4276e6af6ff12921ab25d74a9cc.php line 22: Trying to get property of non-object (View: /var/www/vhosts/vincentspage.tk/httpdocs/Laravel/resources/views/nerds/edit.blade.php)

這個錯誤是因爲錯誤地描述你所試圖訪問空對象的屬性,這意味着你首先需要有一個用戶(模型)存在於表能夠訪問其屬性。

相關問題