2014-10-16 110 views
0

我有這樣的代碼,當我嘗試訪問laravel.dev/cats/create出現一個頁面插入表:廣東話使用laravel

Route::model('cat', 'Cat'); 

Route::get('cats/create', function() { 
    $cat = new Cat; 
    return View::make('cats.edit') 
     ->with('cat', $cat) 
     ->with('method', 'post'); 
}); 

Route::post('cats', function(){ 
    $cat = Cat::create(Input::all()); 
    return Redirect::to('cats/' . $cat->id) 
     ->with('message', 'Successfully created page!'); 
}); 
Route::get('cats/{id}', function($id) { 
    $cat = Cat::find($id); 
    return View::make('cats.single') 
     ->with('cat', $cat); 
}); 

,這是我的貓/ edit.blade.php代碼:

@extends('master') 
@section('header') 
<a href="{{('cats/'.$cat->id.'')}}">&larr; Cancel </a> 
    <h2> 
    @if($method == 'post') 
     Add a new cat 
    @elseif($method == 'delete') 
     Delete {{$cat->name}}? 
    @else 
     Edit {{$cat->name}} 
    @endif 
</h2> 
@stop 

@section('content') 
    {{Form::model($cat, array('method' => $method, 'url'=> 
    'cats/'.$cat->id))}} 

@unless($method == 'delete') 
    <div class="form-group"> 
     {{Form::label('Name')}} 
     {{Form::text('name')}} 
    </div> 
    <div class="form-group"> 
     {{Form::label('Date of birth')}} 
     {{Form::text('date_of_birth')}} 
    </div> 
    <div class="form-group"> 
     {{Form::label('Breed')}} 
     {{Form::select('breed_id', $breed_options)}} 
    </div> 
    {{Form::submit("Save", array("class"=>"btn btn-default"))}} 
@else 
    {{Form::submit("Delete", array("class"=>"btn btn-default"))}} 
@endif 
    {{Form::close()}} 
@stop 

我現在不哪裏出了問題,我有一個編輯相同的代碼刪除但這些傢伙正常工作,但這一個不!

這是一個調試器報告的錯誤:

Trying to get property of non-object (View: D:\Xampp\htdocs\laravel\app\views\cats\single.blade.php) 

<?php echo e($cat->name); ?> 

錯誤提及該行。

感謝您的幫助:))))

編輯: 這是single.blade.php:

@extends('master') 
@section('header') 
<?php// dd($cat); ?> 
<a href="{{url('/')}}">Back to overview</a> 
<h2> 
    {{{$cat->name}}} 
</h2> 
<a href="{{url('cats/'.$cat->id.'/edit')}}"> 
    <span class="glyphicon glyphicon-edit"></span> Edit 
</a> 
<a href="{{url('cats/'.$cat->id.'/delete')}}"> 
    <span class="glyphicon glyphicon-trash"></span> Delete 
</a> 
Last edited: {{$cat->updated_at}} 
@stop 
@section('content') 
    <p>Date of Birth: {{$cat->date_of_birth}} </p> 
    <p> 
     @if($cat->breed) 
      Breed: 
      {{link_to('cats/breeds/' . $cat->breed->name, 
      $cat->breed->name)}} 
     @endif 
    </p> 
@stop 

我不知道爲什麼,當我去的貓/創建我傳遞給single.blade.php我認爲這是我要去哪裏錯了,但不知道在哪裏代碼我這樣做

+1

打開'app/config/app.php'中的調試,你會得到一個有用的錯誤信息 – RMcLeod 2014-10-16 11:16:37

+0

謝謝你兄弟,非常有用 – Arman 2014-10-16 11:17:45

+0

你能發佈整個錯誤嗎? – Jerodev 2014-10-16 11:29:23

回答

0

的問題是:

Route::get('cats/{id}', function($id) { 
    $cat = Cat::find($id); 
    return View::make('cats.single') 
     ->with('cat', $cat); 
}); 

看來沒有找到與$id的記錄,所以$catnull在這裏,它不會在刀片中工作。

你應該檢查當前的URL是什麼(什麼是$ id值),並確保在表中有這樣的$ id的記錄)。

+0

,爲什麼調試器從single.blade.php中得到錯誤? – Arman 2014-10-16 11:41:34

+0

@Nabialek當我在第一個single.blade.php中使用'dd($ cat)'時,它只返回NULL,代碼的哪個部分我想要顯示你? – Arman 2014-10-16 11:45:53

+0

@ArmanKuroKy你還沒有展示這段代碼,你使用'View :: make('cats.single.blade.php')...' – 2014-10-16 11:50:26