【发布时间】:2014-12-11 17:41:22
【问题描述】:
我有这段代码,当我尝试访问 laravel.dev/cats/create 时出现一个页面:
Whoops, looks like something went wrong.
这是我的 route.php 代码(重要部分):
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.'')}}">← 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 我认为这是我出错的地方但不知道我在代码中的哪个地方这样做
【问题讨论】:
-
在
app/config/app.php打开调试,你会得到一个有用的错误信息 -
谢谢兄弟,很有用
-
你能发布整个错误吗?
-
查看你的数据,在视图的开头尝试 dd($cat)。看看什么是空的。
-
问题是你显示完全不同的文件。该消息是关于
single.blade.php而不是edit.blade.php文件。您可能在某处包含此single.blade.php文件。您应该将其内容添加到问题中
标签: php laravel laravel-4 eloquent blade