我正在尝试显示一段代码,同时显示您可以对这些片段进行的注释(我称之为片段代码)。但我无法查看发表每条评论的用户的用户名。我希望在打电话时有每个用户,with(array('user', 'comments', 'comments.user'))
但我只是得到一个未处理的异常。
//Models
class Code extends Eloquent
{
public function user() {
return $this->belongs_to('User');
}
public function comments() {
return $this->has_many('Comment');
}
}
class Comment extends Eloquent
{
public function user() {
$this->belongs_to('User');
}
public function code() {
$this->belongs_to('Code');
}
}
class User extends Eloquent
{
public function codes() {
return $this->has_many('Code');
}
public function comments() {
return $this->has_many('Comment');
}
}
//Controller
public function get_show($id)
{
//$code = Code::with(array('user', 'comments'))->where_slug($id)->first();
//$comments = Comment::with(array('user'))->where_code_id($id)->get();
$code = Code::with(array('user', 'comments', 'comments.user'))->where_slug($id)->first();
dd($code);
//return View::make('code.show')->with(array('code' => $code));
}
//View
<h2>{{ $code->title }}</h2>
@include('code.controls')
<p>Syntax: {{ $code->syntax}} </p>
<p><pre class="prettyprint linenums"><code class="language-{{ $code->syntax }}">{{ $code->content }}</code></pre></p>
@foreach($code->comments as $comment)
<div class="comment">
<p><a href="">#{{ $comment->id }}</a></p>
{{ User::find($comment->user_id)->first()->name }}
<p>{{ $comment->body }}</p>
</div>
@endforeach
//Message when trying to view
Unhandled Exception
Message:
Creating default object from empty value
Location:
/home/victor/Projects/check-my-code/laravel/database/eloquent/query.php on line 167
Stack Trace:
#0 /home/victor/Projects/check-my-code/laravel/laravel.php(40): Laravel\Error::native(2, 'Creating defaul...', '/home/victor/Pr...', 167)
#1 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(167): Laravel\{closure}(2, 'Creating defaul...', '/home/victor/Pr...', 167, Array)
#2 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(140): Laravel\Database\Eloquent\Query->load(Array, 'user', NULL)
#3 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(74): Laravel\Database\Eloquent\Query->hydrate(Object(Comment), Array)
#4 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(186): Laravel\Database\Eloquent\Query->get()
#5 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(140): Laravel\Database\Eloquent\Query->load(Array, 'comments', NULL)
#6 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(74): Laravel\Database\Eloquent\Query->hydrate(Object(Code), Array)
#7 /home/victor/Projects/check-my-code/application/controllers/codes.php(31): Laravel\Database\Eloquent\Query->get()
#8 [internal function]: Codes_Controller->get_show('testar-snippets')
#9 /home/victor/Projects/check-my-code/laravel/routing/controller.php(325): call_user_func_array(Array, Array)
#10 /home/victor/Projects/check-my-code/laravel/routing/controller.php(285): Laravel\Routing\Controller->response('show', Array)
#11 /home/victor/Projects/check-my-code/laravel/routing/controller.php(165): Laravel\Routing\Controller->execute('show', Array)
#12 /home/victor/Projects/check-my-code/laravel/routing/route.php(153): Laravel\Routing\Controller::call('codes@show', Array)
#13 /home/victor/Projects/check-my-code/laravel/routing/route.php(124): Laravel\Routing\Route->response()
#14 /home/victor/Projects/check-my-code/laravel/laravel.php(125): Laravel\Routing\Route->call()
#15 /home/victor/Projects/check-my-code/public/index.php(34): require('/home/victor/Pr...')
#16 {main}