Php 雄辩地展示对象
您好,我正在学习一个教程,我遇到了一个关于在视图中显示一个名为listing.blade.php的对象的问题Php 雄辩地展示对象,php,mysql,laravel,eloquent,blade,Php,Mysql,Laravel,Eloquent,Blade,您好,我正在学习一个教程,我遇到了一个关于在视图中显示一个名为listing.blade.php的对象的问题 @extends('layouts.default') @section('content') @foreach($posts as $post) <h1>{{{$post->title}}} By {{{$post->user->email }}}</h1> @endforeach @stop 之所以会出现这个
@extends('layouts.default')
@section('content')
@foreach($posts as $post)
<h1>{{{$post->title}}} By {{{$post->user->email }}}</h1>
@endforeach
@stop
之所以会出现这个错误,是因为变量$post是一个数组
因此,该代码确实有效:
<h1>{{{$post->title}}} By {{{$post->user['email'] }}}</h1>
{{{$post->title}}{{{{$post->user['email']}}}}
但我不想在上面使用代码符号。我想用这个:
<h1>{{{$post->title}}} By {{{$post->user->email }}}</h1>
{{{{$post->title}}由{{{{{$post->user->email}}}
以下是我的控制器PostController.php的代码:
<?php
class PostController extends BaseController {
public function listing(){
$posts = Post::all();
return View::make('post/listing', compact('posts'));
}
}
我总是使用三元运算符:
{{{ $post->title ?: 'No Title' }}}
这相当于:
isset($post->title) ? $post->title : 'No Title';
这样可以添加一层安全保护层,以防遗漏某些内容。它应该作为一个对象工作。是的,我知道,但出于某种原因,它没有。我说的是实话。如果你愿意,我可以在我的浏览器上记录下我自己显示的代码和输出,如果你愿意的话。我猜其中一篇文章
没有相关的用户
Jarek你是对的xD。我在数据库中的一篇文章没有用户。在posts表中,有一个post的用户id为0作为外键。但是在我的users表中没有用户id为0的用户。这就解释了。无论如何,谢谢你们的回复,伙计们laravel Blade
提供{{{{{$post->标题或“无标题”}}
。
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function posts(){
return $this->hasMany('Post');
}
}
{{{ $post->title ?: 'No Title' }}}
isset($post->title) ? $post->title : 'No Title';