2015-05-06 29 views
0

嘿,我不想回應與belongsTo的東西,但輸出給我一些奇怪的人物回來。Laravel回聲奇怪的字符

class Leviews extends Entity 
{ 
public $table = 'reviews'; 


public function ravatar() 
{ 
    return $this->belongsTo('User', 'author', 'username')->select('avatar'); 

} 

現在,當我回聲{{$用品 - > 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 

{{$用品 - >體}}是不奇怪的字符

編輯 這裏的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

你能告訴你的控制器和視圖代碼? –

+0

可以在視圖中的foreach之前添加'{{dd($ lreviews)}}'並粘貼輸出嗎? –

+0

您可以將dd移動到foreach後面,然後執行'{{dd($ lreviews-> toArray())}}'並重新粘貼輸出。對不起,要求這麼多的東西,但我看不到在模型上設置的屬性。 –

回答

1

$item->ravatar是模型,而不是隻是一個字符串。你看到的是該模型的JSON表示。爲了得到實際的路徑使用:

如果有可能,並非每一個審查有一個頭像,請查看if語句或使用:

{{ $item->ravatar->avatar or 'avatar/default.jpg' }} 
+0

ErrorException嘗試如果使用這種方法獲取非對象的屬性 – Clayman

+0

您是否嘗試過第二段代碼? – lukasgeiter

+0

不,我沒有,但現在,它的工作感謝幫助我 – Clayman