2017-10-03 3 views
0

を持つ属性を持たせますアヤックス私はアクセサを作ってみましたが、それは属性を作成していない「テンプレート」getTemplateAttribute($値)ので、私は(「テンプレート」)で作られたと私は呼ばれているとき、私のモデルに属性テンプレートを作成するように見えることはできません。laravelモデルは、私は、テンプレートおよびフィールドテンプレートと呼ばれるモデルを持っている価値

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Template extends Model 
{ 
    protected $table = 'templates'; 

    protected $fillable = ['title', 'directory', 'filename', 'created_at', 'updated_at']; 

    public function template() { 
     $this->attributes['template'] = 'test'; 
    } 
} 

//とそれを呼び出す

public function show(Template $template) 
{ 
    $template = Template::findOrFail($template->id)->with('template'); 
    return $template; 
} 
+0

あなたが任意のエラーを取得してください

class Template extends Model { protected $table = 'templates'; protected $fillable = ['title', 'directory', 'filename', 'created_at', 'updated_at']; protected appends = ['template']; public function getTemplateAttribute() { return "test"; } } 

..正確な名前でなければなりませんか? – Jonjie

+0

モデルでカスタム属性を宣言する必要があります。 は$が= [「テンプレート」]を追加し、保護して、カスタム属性を設定する必要が –

答えて

1

使用appendsモデルで...当時)(AppendType属性を取得使用します。値が追加としてAppendTypeはその後

$template = Template::findOrFail($template->id); 
return $template->template; 
+1

ありがとう、私は、今日、それはそれはあなたを助けている場合..あなたはこの答えをマークすることができます助け嬉しい歓声 – ONYX

+0

イムを何か新しいことを学びました。 :) – ZeroOne

関連する問題