2017-08-10 45 views
0

爲什麼當我使用查詢生成器而不是函數diffForHumans()時出現錯誤,但是如果我使用偶爾ROM但沒有錯誤,如何克服它? (如何解決它)謝謝如何修復它調用成員函數diffForHumans()在laravel 5.3中的字符串

diffForHumans()

這是ArticlesController.php

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\Article; 
use Carbon\Carbon; 
use Auth; 
use DB; 

class ArticlesController extends Controller 
{ 

    public function index() 
    { 
     $articles =DB::table('articles')->get(); 
     $articles = ['articles'=>$articles]; 
     return view('articles.index',$articles); 
    } 
} 

這是模型Article.php

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use Carbon\Carbon; 
use Illuminate\Database\Eloquent\SoftDeletes; 

class Article extends Model 
{ 
    // 
    use SoftDeletes ; 

    protected $fillable = [ 
     'user_id','content','live','post_on', 
    ]; 

    protected $dates =[ 
     'post_on','deleted_at','created_at','updated_at', 
    ]; 

    public function setLiveAttribute($value) 
    { 
     $this->attributes['live'] = (boolean)($value); 
    } 

    public function getShortContentAttribute() 
    { 
     return substr($this->content,0,random_int(60,150))."..."; 
    } 

    public function setPostOnAttribute($value) 
    { 
     $this->attributes['post_on'] = Carbon::parse($value); 
    } 

    public function setCreatedAtAttribute($value) 
    { 
     $this->attributes['post_on'] = Carbon::parse($value); 
    } 



} 

,這是我的代碼,我該如何修復它? 謝謝

回答

3

我注意到有兩件事情在你的代碼,

  • 沒有必要投created_atupdated_at到日期,他們已經鑄造而成,是Carbon
  • 情況下,你可以使用物業$casts投簡單attributs像live
  • 無需添加增變了post_on日期,因爲你把它添加到$dates
  • 還你created_at設置賦值函數,而不是post_on,您使用SetCreatedAttribute代替setPostOnAttribute
  • 而不是substr($this->content,0,random_int(60,150))."..."可以使用Laravel的str_limit助手功能,也改變random_intrand =>int rand (int $min , int $max)

查詢建設者返回dates作爲字符串,你需要解析之前使用它們,否則你會得到像這樣的錯誤PHP error: Call to a member function on string,只是這樣做︰

\Carbon\Carbon::parse(\DB::table('articles')->first()->post_on)->diffForHumans() 

你可以讓你的模型簡單,像這樣:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use Illuminate\Database\Eloquent\SoftDeletes; 

class Article extends Model 
{ 
    use SoftDeletes ; 

    protected $fillable = [ 
     'user_id','content','live','post_on', 
    ]; 

    protected $dates = [ 
     'post_on','deleted_at' 
    ]; 

    protected $casts = [ 
     'live' => 'boolean' 
    ]; 

    public function getShortContentAttribute() 
    { 
     return str_limit($this->content, rand(60,150)); 
    } 
} 

也可以簡化你index方法是這樣的:

public function index() 
{ 
    $articles = DB::table('articles')->get(); 

    return view('articles.index', compact('articles')); 
} 
+0

ouh謝謝你,這是很好的建議,當我嘗試你的建議,它的作品 –

4

默認情況下雄辯回報date/time領域爲Carbon例如,在查詢生成器別。因此,如果您想在查詢構建器的返回屬性上使用Carbon,則必須使用Carbon::parse()方法包裝該屬性。

例如,我可以在口才結果使用Carbon的方法toCookieString()之一如

echo App\User::find(1)->created_at->toCookieString(); 

這將給這樣Thursday, 10-Aug-2017 17:59:53 UTC的響應。但是,如果使用查詢構建器,而不是如

echo DB::table('users')->find(1)->created_at->toCookieString(); 

那麼它會在字符串給出錯誤

調用一個成員函數toCookieString()

要在這裏使用Carbon我用Carbonparse()方法包裝財產。

echo Carbon\Carbon::parse(DB::table('users')->find(1)->created_at)->toCookieString(); 

這會給人想要的結果作爲雄辯。

相關問題