嘿,我不想用belongsTo回应一些东西,但输出给了我一些奇怪的角色。
class Leviews extends Entity
{
public $table = 'reviews';
public function ravatar()
{
return $this->belongsTo('User', 'author', 'username')->select('avatar');
}
现在,当我用{{$ item-> ravatar}}回复数据时,我得到了这个
{"avatar":"avatars\/1.jpg"}
它应该是
avatars/1.jpg
我做错了什么?
修改的 这是控制器
<?php
use Carbon\Carbon;
use Lib\Reviews\LeviewsRepository;
use Lib\Services\Scraping\Scraper;
use Lib\Services\Validation\LeviewsValidator;
class LeviewsController extends \BaseController {
/**
* Leviews repository instance.
*
* @var Lib\Leviews\LeviewsRepository
*/
protected $repo;
/**
* validator instance.
*
* @var Lib\Services\Validation\LeviewsCreateValidator
*/
private $validator;
/**
* Leviews scraper isntance.
*
* @var Lib\Services\Scraping\NewScraper;
*/
private $scraper;
public function __construct(LeviewsRepository $lreviews, LeviewsValidator $validator, Scraper $scraper)
{
$this->beforeFilter('csrf', array('on' => 'post'));
$this->beforeFilter('logged', array('except' => array('index', 'show', 'paginate')));
$this->beforeFilter('news:create', array('only' => array('create', 'store')));
$this->beforeFilter('news:edit', array('only' => array('edit', 'update')));
$this->beforeFilter('news:delete', array('only' => 'destroy'));
$this->beforeFilter('news:update', array('only' => 'updateFromExternal'));
$this->repo = $lreviews;
$this->scraper = $scraper;
$this->validator = $validator;
}
/**
* Display list of paginated news.
*
* @return View
*/
public function index()
{
return View::make('Leviews.Index');
}
/**
* Display form for creating new news items.
*
* @return View
*/
public function create()
{
return View::make('Leviews.Create');
}
/**
* Store a newly created news item.
*
* @return Redirect
*/
public function store()
{
$input = Input::except('_token');
if ( ! $this->validator->with($input)->passes())
{
return Redirect::back()->withErrors($this->validator->errors())->withInput($input);
}
//escape double qoutes
$input['title'] = htmlspecialchars($input['title']);
$this->repo->store($input);
return Redirect::back()->withSuccess( trans('main.news create success') );
}
/**
* Display single news items.
*
* @param int $id
* @return View
*/
public function show($id)
{
$lreviews = $this->repo->byId($id);
if ($lreviews->full_url && ! $lreviews->fully_scraped)
{
$lreviews = $this->repo->getFullLeviewsItem($lreviews);
}
return View::make('Leviews.Show')->with(compact('news'))->withRecent($this->repo->latest());
}
/**
* Displays form for editing news item.
*
* @param int $id
* @return View
*/
public function edit($id)
{
$lreviews = $this->repo->byId($id);
return View::make('Leviews.Edit')->withLeviews($lreviews);
}
/**
* Updates the news item.
*
* @param int $id
* @return Redirect
*/
public function update($id)
{
$input = Input::except('_token', '_method');
$lreviews = $this->repo->byId($id);
if ($lreviews->title === $input['title'])
{
//dont check for title uniqueness when updating if
//title was not updated.
$this->validator->rules['title'] = 'required|min:2|max:255';
}
if ( ! $this->validator->with($input)->passes())
{
return Redirect::back()->withErrors($this->validator->errors())->withInput($input);
}
//escape double qoutes
$input['title'] = htmlspecialchars($input['title']);
$this->repo->update($lreviews, $input);
return Redirect::back()->withSuccess( trans('main.news update success') );
}
/**
* Delete specified news item.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$this->repo->delete($id);
return Response::json(trans('main.news delete success'), 200);
}
/**
* Updates news from external sources.
*
* @return void
*/
public function updateFromExternal()
{
$this->scraper->updateLeviews();
Event::fire('Leviews.Updated', Carbon::now());
return Redirect::back()->withSuccess( trans('dash.updated news successfully') );
}
}
这里是视图
@if ($options->enableNews())
@foreach($lreviews as $k => $item)
{{ $item->body }}
{{ $item->ravatar }}
@endforeach
@endif
{{$ item-&gt; body}}没有奇怪的字符
修改的 这里是dd输出
object(Illuminate\Database\Eloquent\Collection)#634 (1) { ["items":protected]=> array(6) { [0]=> object(Leviews)#606 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(42) ["author"]=> string(7) "Clayman" ["source"]=> string(8) "Japanime" ["body"]=> string(182) "Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review " ["score"]=> int(9) ["link"]=> NULL ["title_id"]=> int(1655) ["created_at"]=> string(19) "2015-04-24 16:08:34" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(1) ["type"]=> string(4) "user" } ["original":protected]=> array(12) { ["id"]=> int(42) ["author"]=> string(7) "Clayman" ["source"]=> string(8) "Japanime" ["body"]=> string(182) "Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review " ["score"]=> int(9) ["link"]=> NULL ["title_id"]=> int(1655) ["created_at"]=> string(19) "2015-04-24 16:08:34" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(1) ["type"]=> string(4) "user" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } [1]=> object(Leviews)#635 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(1) ["author"]=> string(18) "James Berardinelli" ["source"]=> string(9) "ReelViews" ["body"]=> string(265) "The movie is pretty to look at in a "Transformers" sort of way and moves briskly enough that it never threatens to bore, but it's hard to feel much of anything about the characters and, when it's all over, there's a sense that everything that happens is obligatory." ["score"]=> int(63) ["link"]=> string(64) "http://www.reelviews.net/php_review_template.php?identifier=2687" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["original":protected]=> array(12) { ["id"]=> int(1) ["author"]=> string(18) "James Berardinelli" ["source"]=> string(9) "ReelViews" ["body"]=> string(265) "The movie is pretty to look at in a "Transformers" sort of way and moves briskly enough that it never threatens to bore, but it's hard to feel much of anything about the characters and, when it's all over, there's a sense that everything that happens is obligatory." ["score"]=> int(63) ["link"]=> string(64) "http://www.reelviews.net/php_review_template.php?identifier=2687" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } [2]=> object(Leviews)#629 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(2) ["author"]=> string(10) "Steven Rea" ["source"]=> string(21) "Philadelphia Inquirer" ["body"]=> string(345) "One of the problems with The Dark World is that its monsters and angry armies and visual effects are interchangeable with Peter Jackson's Tolkien pics, with Clash of the Titans, with The Avengers, with Man of Steel, and on and on. These superhero movies. These Middle Earth movies. These mythic god movies. It's getting hard to tell them apart." ["score"]=> int(63) ["link"]=> string(99) "http://www.philly.com/philly/entertainment/movies/20131108_Thor_s_back__more_generic_than_ever.html" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["original":protected]=> array(12) { ["id"]=> int(2) ["author"]=> string(10) "Steven Rea" ["source"]=> string(21) "Philadelphia Inquirer" ["body"]=> string(345) "One of the problems with The Dark World is that its monsters and angry armies and visual effects are interchangeable with Peter Jackson's Tolkien pics, with Clash of the Titans, with The Avengers, with Man of Steel, and on and on. These superhero movies. These Middle Earth movies. These mythic god movies. It's getting hard to tell them apart." ["score"]=> int(63) ["link"]=> string(99) "http://www.philly.com/philly/entertainment/movies/20131108_Thor_s_back__more_generic_than_ever.html" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } [3]=> object(Leviews)#630 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(3) ["author"]=> string(12) "Simon Abrams" ["source"]=> string(14) "RogerEbert.com" ["body"]=> string(133) "Thor: The Dark World's characters are often very charming, but they're only so much fun when they're stuck going through the motions." ["score"]=> int(63) ["link"]=> string(58) "http://www.rogerebert.com/reviews/thor-the-dark-world-2013" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["original":protected]=> array(12) { ["id"]=> int(3) ["author"]=> string(12) "Simon Abrams" ["source"]=> string(14) "RogerEbert.com" ["body"]=> string(133) "Thor: The Dark World's characters are often very charming, but they're only so much fun when they're stuck going through the motions." ["score"]=> int(63) ["link"]=> string(58) "http://www.rogerebert.com/reviews/thor-the-dark-world-2013" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } [4]=> object(Leviews)#641 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(4) ["author"]=> string(10) "Mike Scott" ["source"]=> string(26) "New Orleans Times-Picayune" ["body"]=> string(213) "None of that is to say that Thor: The Dark World is a bad movie, necessarily. I would never speak ill of a man with a giant, magical hammer. At the same time, hammer or no hammer, it doesn't quite nail it, either." ["score"]=> int(40) ["link"]=> string(95) "http://www.nola.com/movies/index.ssf/2013/11/thor_the_dark_world_movie_revi.html#incart_m-rpt-1" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["original":protected]=> array(12) { ["id"]=> int(4) ["author"]=> string(10) "Mike Scott" ["source"]=> string(26) "New Orleans Times-Picayune" ["body"]=> string(213) "None of that is to say that Thor: The Dark World is a bad movie, necessarily. I would never speak ill of a man with a giant, magical hammer. At the same time, hammer or no hammer, it doesn't quite nail it, either." ["score"]=> int(40) ["link"]=> string(95) "http://www.nola.com/movies/index.ssf/2013/11/thor_the_dark_world_movie_revi.html#incart_m-rpt-1" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } [5]=> object(Leviews)#640 (22) { ["table"]=> string(7) "reviews" ["defaultOrderColumn":protected]=> string(10) "created_at" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(5) ["author"]=> string(12) "Peter Rainer" ["source"]=> string(25) "Christian Science Monitor" ["body"]=> string(183) "My favorite moment in the movie: Astrophysicist Erik Selvig (Stellan Skarsgard) insisting on wearing only his underwear because he says he thinks better that way. Hey, whatever works." ["score"]=> int(58) ["link"]=> string(108) "http://www.csmonitor.com/The-Culture/Movies/2013/1108/Thor-The-Dark-World-has-lost-the-spark-of-the-original" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["original":protected]=> array(12) { ["id"]=> int(5) ["author"]=> string(12) "Peter Rainer" ["source"]=> string(25) "Christian Science Monitor" ["body"]=> string(183) "My favorite moment in the movie: Astrophysicist Erik Selvig (Stellan Skarsgard) insisting on wearing only his underwear because he says he thinks better that way. Hey, whatever works." ["score"]=> int(58) ["link"]=> string(108) "http://www.csmonitor.com/The-Culture/Movies/2013/1108/Thor-The-Dark-World-has-lost-the-spark-of-the-original" ["title_id"]=> int(87) ["created_at"]=> string(19) "2015-04-23 18:32:05" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) } } }
修改的
array(6) { [0]=> array(12) { ["id"]=> int(42) ["author"]=> string(7) "Clayman" ["source"]=> string(8) "Japanime" ["body"]=> string(182) "Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review Review " ["score"]=> int(9) ["link"]=> NULL ["title_id"]=> int(1655) ["created_at"]=> string(12) "Apr 24, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(1) ["type"]=> string(4) "user" } [1]=> array(12) { ["id"]=> int(1) ["author"]=> string(18) "James Berardinelli" ["source"]=> string(9) "ReelViews" ["body"]=> string(265) "The movie is pretty to look at in a "Transformers" sort of way and moves briskly enough that it never threatens to bore, but it's hard to feel much of anything about the characters and, when it's all over, there's a sense that everything that happens is obligatory." ["score"]=> int(63) ["link"]=> string(64) "http://www.reelviews.net/php_review_template.php?identifier=2687" ["title_id"]=> int(87) ["created_at"]=> string(12) "Apr 23, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } [2]=> array(12) { ["id"]=> int(2) ["author"]=> string(10) "Steven Rea" ["source"]=> string(21) "Philadelphia Inquirer" ["body"]=> string(345) "One of the problems with The Dark World is that its monsters and angry armies and visual effects are interchangeable with Peter Jackson's Tolkien pics, with Clash of the Titans, with The Avengers, with Man of Steel, and on and on. These superhero movies. These Middle Earth movies. These mythic god movies. It's getting hard to tell them apart." ["score"]=> int(63) ["link"]=> string(99) "http://www.philly.com/philly/entertainment/movies/20131108_Thor_s_back__more_generic_than_ever.html" ["title_id"]=> int(87) ["created_at"]=> string(12) "Apr 23, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } [3]=> array(12) { ["id"]=> int(3) ["author"]=> string(12) "Simon Abrams" ["source"]=> string(14) "RogerEbert.com" ["body"]=> string(133) "Thor: The Dark World's characters are often very charming, but they're only so much fun when they're stuck going through the motions." ["score"]=> int(63) ["link"]=> string(58) "http://www.rogerebert.com/reviews/thor-the-dark-world-2013" ["title_id"]=> int(87) ["created_at"]=> string(12) "Apr 23, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } [4]=> array(12) { ["id"]=> int(4) ["author"]=> string(10) "Mike Scott" ["source"]=> string(26) "New Orleans Times-Picayune" ["body"]=> string(213) "None of that is to say that Thor: The Dark World is a bad movie, necessarily. I would never speak ill of a man with a giant, magical hammer. At the same time, hammer or no hammer, it doesn't quite nail it, either." ["score"]=> int(40) ["link"]=> string(95) "http://www.nola.com/movies/index.ssf/2013/11/thor_the_dark_world_movie_revi.html#incart_m-rpt-1" ["title_id"]=> int(87) ["created_at"]=> string(12) "Apr 23, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } [5]=> array(12) { ["id"]=> int(5) ["author"]=> string(12) "Peter Rainer" ["source"]=> string(25) "Christian Science Monitor" ["body"]=> string(183) "My favorite moment in the movie: Astrophysicist Erik Selvig (Stellan Skarsgard) insisting on wearing only his underwear because he says he thinks better that way. Hey, whatever works." ["score"]=> int(58) ["link"]=> string(108) "http://www.csmonitor.com/The-Culture/Movies/2013/1108/Thor-The-Dark-World-has-lost-the-spark-of-the-original" ["title_id"]=> int(87) ["created_at"]=> string(12) "Apr 23, 2015" ["updated_at"]=> NULL ["temp_id"]=> NULL ["user_id"]=> int(0) ["type"]=> string(6) "critic" } }
答案 0 :(得分:1)
$item->ravatar
是模型,而不仅仅是一个字符串。你看到的是该模型的JSON表示。要获得实际路径使用:
{{ $item->ravatar->avatar }}
如果有可能不是每个评论都有头像,请使用if语句检查或使用此:
{{ $item->ravatar->avatar or 'avatar/default.jpg' }}