我查看一段代码:
@forelse ($questions as $question)
<a href="#" class="list-group-item">
<h4>{{$question->title}}</h4>
<p>{{Str::limit($question->body, 100)}}</p>
<span>Asked {{$question->created}} by {{$question->user_name}}</span>
{{json_encode($question)}}
</a>
@empty
<h2>No questions!</h2>
@endforelse
我得到这样的 html('...' 没问题,这是有效的,但值很长):
<a href="#" class="list-group-item">
<h4>...</h4>
<p>...</p>
<span>Asked by </span>
<br>
{"id":1,"title":"...","path":"...","body":"...","created":"2 hours ago","user_name":"Nash Bogisich","user_id":11}
</a>
如您所见,在 htmlcreated中null,还有user_name. 但是在 json 中,这些 props 有它们的值。我不明白这是怎么可能的,请帮忙。
QuestionResource 完全简单:
class QuestionResource extends JsonResource
{
public function toArray($request)
{
return [
'id' =>$this->id,
'title' =>$this->title,
'path' =>$this->path,
'body' =>$this->body,
'created' => $this->created_at->diffForHumans(),
// 'modified' =>$this->modified_at,
'user_name' =>$this->user->name,
'user_id' =>$this->user_id,
];
}
}
我也查了一下,created是真的null。
if ($question->created==null){
$created = 'null value';
}
是的,null价值。
$questions 不是真正的数组(我认为),它是一个 Laravel 集合。
控制器(我有两个,一个用于api,一个用于web.Web使用api。):
应用程序接口:
public function index()
{
// now each transform of question use QuestionResource
return QuestionResource::collection(Question::latest()->paginate(15));
}
网站:
public function index()
{
$questions = $this->apiController->index();
// return $questions;
return View::make('question.index')->with('questions', $questions);
}
素胚勾勒不出你
MYYA
叮当猫咪