2017-04-18 192 views
0

我使用Laravel 5.4,似乎无法添加新的属性,这里是我的代码,其中包括一个注释部分,显示不会返回我添加的新属性的输出称为Url。动态地添加一个新的属性到模型(Laravel)

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Exercice extends Model 
{ 
    protected $table = 'exercices'; 
    protected $connection = 'DB_V2'; 
    protected $appends = ['Url']; 


    public function getUrlAttribute() { 
     return 'custom'; 
    } 

    /* 
    $exercise = App\Exercice::where("idExercice", 1)->get(); 
    dd($exercise); 

      #attributes: array:15 [▼ 
     "idExercice" => 1 
     "image1" => "160_A.jpg" 
     "image2" => "a.jpg" 
     "thumb1" => "v.jpg" 
     "thumb2" => "c.jpg" 
     "video" => "fliqz|077fzc4f478142cea8a73e586617f8a\r\n" 
     "draw1" => "" 
     "draw2" => "" 
     "drawthumb1" => "" 
     "drawthumb2" => "" 
     "licno" => 1000 
     "idusager" => 0 
     "code_exercice" => "XAMP160" 
     "shared" => "Y" 
     "rank" => 99999999 
     ] 
     */ 

} 

回答

0

值不会在物体本身的dd()展现出来,因为属性实际上不存在的$attributes财产。

如果dd()数组或JSON输出,你会看到你的价值:

$exercise = App\Exercice::where("idExercice", 1)->get(); 
dd($exercise->toArray());