我有一些像这样的cotroller:
Route::get('/article/create', 'ArticlesController@create');
这是我的ArticlesController @ create:
public function create(){
return view('articles.create',
[
'title'=>'Add Artikel',
'username'=>'Whatever Myname',
'status' => 'Offline'
]
);
}
当我试图访问blog.dev/article/create时,我遇到了这个奇怪的错误:
"Trying to get property of non-object (View: E:\xampp\htdocs\blog\resources\views\articles\single.blade.php)"
当我的视图指向articles.create时,我怎么会遇到这种错误,但是错误发生在single.blade.php,它假设为ArticlesController @ view?
这就是create.blade.php中的内容:
@extends('admin.layout')
@section('content')
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Horizontal Form</h3>
</div>
@include('admin.formerrors')
<form method="post" class="form-horizontal" action="/articles">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input class="form-control" id="title" name="title" placeholder="Judul Artikel">
</div>
</div>
<div class="form-group">
<label for="content" class="col-sm-2 control-label">Content</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="content" name="content"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
@endsection
,这是我的single.blade.php:
@extends('admin.layout')
@section('content')
<h1>{{ $article->title }}</h1>
<p>{{ $article->content }}</p>
<hr>
@foreach($article->comments as $comment)
<blockquote>
<p>{{ $comment->comment }}</p>
<small>{{ $comment->created_at->diffForHumans() }}</small>
</blockquote>
@endforeach
@include('admin.formerrors')
<form method="post" class="form-horizontal" action="/article/{{ $article->id }}/comment">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="comment" class="col-sm-2 control-label">Comment</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="comment" name="comment"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
@endsection
这是我的路由器:
Route::get('/articles', 'ArticlesController@index')->name('home');
Route::post('/articles', 'ArticlesController@save');
Route::get('/article/{id}', 'ArticlesController@view');
Route::get('/article/create', 'ArticlesController@create');
Route::post('/article/{article}/comment', 'CommentsController@save');
Route::get('/register', 'RegistrationController@create');
Route::post('/register', 'RegistrationController@store');
我尝试通过指向另一个视图来更改创建函数,但仍然显示相同的错误并指向single.blade视图。
我删除了single.blade中的所有代码并写了'test'文本,我没有收到任何错误。但我指着我的控制器查看create.blade而不是sigle.blade
答案 0 :(得分:2)
在看到你的路线之后,问题出现在以下两条路线中:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slider">
<img src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" alt="" class="active">
<img src="https://www.w3schools.com/css/img_lights.jpg" alt="">
<img src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image2.jpg" alt="">
<img src="http://images.all-free-download.com/images/graphiclarge/a_london_cityscape_515129.jpg" alt="">
<img src="https://image.jimcdn.com/app/cms/image/transf/dimension=1920x400:format=jpg/path/s86d6d6c688ca86fa/image/ie4265a3cd27b2997/version/1451246087/image.jpg" alt="">
</div>
<div class="back">Back</div>
<div class="next">Next</div>
在这种情况下,Laravel会将您的路径Route::get('/article/{id}', 'ArticlesController@view');
Route::get('/article/create', 'ArticlesController@create');
中的创建视为此处视图路由的blog.dev/article/create
参数=&gt; id
。
因此,作为解决方案,您应该简单地反转两条路线:
/article/{id}
答案 1 :(得分:0)
您需要通过阅读不仅仅是该行来查找文件中的内容。也许您在articles.create中包含该文件?也许您在访问加载页面时不存在的变量。编辑你的答案,更多的错误和两个刀片内部的内容,我们可以找出错误。