![](/img/trans.png)
[英]Call to a member function diffForHumans() on string in laravel 5.4
[英]How to fix it Call to a member function diffForHumans() on string in laravel 5.3
為什么當我使用查詢生成器而不是函數diffForHumans()時出現錯誤,但如果我使用ELoquent ROM但沒有錯誤,有辦法克服它? (我該怎么辦呢)謝謝
這是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);
}
}
這是我的代碼,我怎么能解決它? 謝謝
我注意到你的代碼中有幾件事,
created_at
和updated_at
為日期,它們已經被轉換為Carbon
實例 $casts
來$casts
像live
這樣的簡單attributs post_on
日期添加mutator,因為您將其添加到$dates
created_at
而不是post_on
上設置了一個mutator,你使用SetCreatedAttribute
而不是setPostOnAttribute
substr($this->content,0,random_int(60,150))."..."
你可以使用str_limit
輔助函數,也可以將random_int
改為rand
=> 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'));
}
默認情況下,eloquent將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
,我使用Carbon
的parse()
方法包裝了該屬性。
echo Carbon\Carbon::parse(DB::table('users')->find(1)->created_at)->toCookieString();
這將以雄辯的方式給出理想的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.