我是laravel的新手,并使用twitter bootstrap的一些帮助创建了一个迷你程序。
我正在尝试访问此路线:
http://127.0.0.1:8000/posts/create
我得到了这些错误:
72427cb411f683691ba00d0846f7eda3c61bff74.php第4行中的2/2 ErrorException: 试图获取非对象的属性(查看:C:\ xampp \ htdocs \ laravel \ alquirozlaravel \ resources \ views \ show-solo.blade.php)
in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4
at CompilerEngine->handleViewException(object(ErrorException), 1) in PhpEngine.php line 44
at PhpEngine->evaluatePath('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\storage\\framework\\views/73427cb411f683691ba00d0846f7eda3c61bff74.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null)) in CompilerEngine.php line 59
at CompilerEngine->get('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\resources\\views/show-solo.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null)) in View.php line 137
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 38
at Response->setContent(object(View)) in Response.php line 225
at Response->__construct(object(View)) in Router.php line 602
at Router->prepareResponse(object(Request), object(View)) in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
at require_once('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\public\\index.php') in server.php line 21
73427cb411f683691ba00d0846f7eda3c61bff74.php第4行中的1/2 ErrorException: 试图获得非对象的属性
in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4
at HandleExceptions->handleError(8, 'Trying to get property of non-object', 'C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\storage\\framework\\views\\73427cb411f683691ba00d0846f7eda3c61bff74.php', 4, array('__path' => 'C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\storage\\framework\\views/73427cb411f683691ba00d0846f7eda3c61bff74.php', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null), 'obLevel' => 1, '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null)) in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4
at include('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\storage\\framework\\views\\73427cb411f683691ba00d0846f7eda3c61bff74.php') in PhpEngine.php line 42
at PhpEngine->evaluatePath('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\storage\\framework\\views/73427cb411f683691ba00d0846f7eda3c61bff74.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null)) in CompilerEngine.php line 59
at CompilerEngine->get('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\resources\\views/show-solo.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'post' => null)) in View.php line 137
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 38
at Response->setContent(object(View)) in Response.php line 225
at Response->__construct(object(View)) in Router.php line 602
at Router->prepareResponse(object(Request), object(View)) in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
at require_once('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\public\\index.php') in server.php line 21
目前我的路线:
Route::get('/posts', 'PostController@index');
Route::get('/posts/{id}', 'PostController@showById');
Route::get('/posts/create', 'PostController@showForm');
Route::post('/posts', 'PostController@store');
Route::post('/posts/{post}/comments', 'CommentController@store');
我的控制器: (PostController中)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PostController extends Controller
{
public function index() {
$posts = Post::latest()->get();
return view('index', compact('posts'));
}
public function showById($id)
{
$post = Post::find($id);
return view('show-solo', compact('post'));
}
public function showForm()
{
return view('new-post');
}
public function store(Request $request) {
$this->validate(request(), [
'title' => 'required|min:2',
'body' => 'required|min:10'
]);
Post::create([
'title' => $request->title,
'body' => $request->body
]);
return redirect('/posts');
}
}
(CommentController)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\Comment;
class CommentController extends Controller
{
// Convention to POST ADD
public function store(Post $post)
{
Comment::create([
'body' => request('body'),
'post_id' => $post->id
]);
// return back() works too
return back();
}
}
Blades 我认为: (显示-solo.blade.php)
@extends('master')
@section('content')
<div class="col-sm-8 blog-main">
<div class="blog-post">
<h2>Post number: {{ $post->id }}</h2>
<h2 class="blog-post-title">
<a class="title-link" href="/posts/{{ $post->id }}">{{ $post->title }}</a>
</h2>
<!-- {{ $post->created_at->toFormattedDateString() }} -->
<p class="blog-post-meta">{{ $post->created_at->diffForHumans() }} by <a href="#">Mark</a></p>
{{ $post->body }}
<hr />
@include('partials.error')
@include('partials.post-comment')
</div>
</div>
@endsection
(新-post.blade.php)
@extends('master')
@section('content')
<div class="col-sm-8 blog-main">
<h1>Publish a Post<hr /></h1>
<form method="POST" action="/posts">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title:</label>
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea name="body" id="body" class="form-control" cols="30" rows="10"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Publish</button>
</div>
@include('partials.error')
</form>
</div>
@endsection
(master.blade.php)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Blog Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="{{ URL::asset('css/bootstrap.min.css') }}">
<!-- Custom styles for this template -->
<link rel="stylesheet" href="{{ URL::asset('css/blog.css') }}">
<link rel="stylesheet" href="{{ URL::asset('css/style.css') }}">
</head>
<body>
@include('partials.nav')
<div class="container">
<div class="row">
@yield('content')
@include('partials.sidebar')
</div><!-- /.row -->
</div><!-- /.container -->
@include('partials.footer')
</body>
</html>
以下是我的模型,如果它有帮助: (post.php中)
<?php
namespace App;
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}
(Comment.php)
<?php
namespace App;
class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);
}
}
在帖子/创建路线工作正常之前,现在不会。当我尝试删除路由Route :: get('/ posts / {id}','PostController @ showById'); post / create路由再次正常工作。我错过了什么?非常感谢任何帮助,谢谢。
答案 0 :(得分:4)
问题是您的create
路线是在一般posts/{id}
路线之后列出的。 Laravel处理/ posts /后的每个段作为id并忽略“特殊”create
路由。
交换这两行:
Route::get('/posts/create', 'PostController@showForm');
Route::get('/posts/{id}', 'PostController@showById');
此外,我建议您使用resource controllers,这样可以让您的路线变得更轻松!
答案 1 :(得分:1)
更改路线
这
Route::get('/posts/{id}', 'PostController@showById');
要
Route::get('/posts/{id}', 'PostController@showById')->where('id', '[0-9]+');
答案 2 :(得分:0)
如果您使用resource controllers但仍有相同的问题,则应检查数据库,因为列中的某些内容可能为NULL。