【问题标题】:Laravel get custom model attribute not workingLaravel获取自定义模型属性不起作用
【发布时间】:2016-06-02 15:11:50
【问题描述】:

完全相同的事情适用于应用程序中的其他模型,但不是这个,我不知道为什么。当我尝试访问 clientname 属性时,它是空白的。

型号

class Domain extends Model {

protected $guarded = [];

public function clients() {
    return $this->belongsTo('App\Client');
}

public function getClientNameAttribute() {
    return Client::where('id', $this->client_id)->value('name');
}

}

刀片

  <tbody>
            @foreach( $domains as $domain )
            {!! Form::open(array('class' => 'form-inline', 'method' => 'DELETE', 'route' => array('domains.index', $domain->id))) !!}
            <tr>
                <td><a href="{{ route('domains.show', $domain->id) }}">{{ $domain->id }}</a></td>
                <td>{{ $domain->name }}</td>
                <td>{{ $domain->clientname }}</td>
                <td>{{ $domain->expiry_date }}</td>
                <td>  {!! link_to_route('domains.edit', 'Edit', array($domain->id), array('class' => 'btn btn-info')) !!}
                    </td>
            </tr>
            {!! Form::close() !!}

            @endforeach
        </tbody>

已编辑以添加客户端/域架构

客户

    Schema::create('clients', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('kashflow_id')->unique();
        $table->string('name');
        $table->string('contact');
        $table->string('telephone');
        $table->string('mobile');
        $table->string('fax');
        $table->string('email');
        $table->string('address1');
        $table->string('address2');
        $table->string('address3');
        $table->string('address4');
        $table->string('postcode');
        $table->timestamps();
    });

域名

            Schema::create('domains', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients');
        $table->string('name');
        $table->date('expiry_date');
        $table->timestamps();
    });

【问题讨论】:

    标签: php laravel eloquent accessor


    【解决方案1】:

    你应该像这样定义这个访问器:

    public function getClientNameAttribute() 
    {
        return ($client = $this->clients) ? $client->name : '';
    }
    

    然后第二件事是,在 Blade 中你应该像这样使用它:

    $domain->client_name
    

    带蛇盒。

    显然也没有必要创建clients 关系。它应该有名称client

    【讨论】:

    • @ndev 所以你应该编辑你的问题并包括客户端和域的数据库架构
    • @ndev 在 Blade $domain-&gt;clients-&gt;name 中使用时会发生什么 - 它是否显示正确的值?您确定为域分配了客户端吗?
    • 嗨,有 100% 的客户端分配给域,当我签入我的数据库时,我可以看到 client_id 字段已填充,但在应用程序中没有显示任何内容。 $domain-&gt;clients-&gt;name 给了我这个错误:Trying to get property of non-object。我觉得奇怪的是完全相同的方法适用于我的其他模型,但不适用于这个域...
    【解决方案2】:

    你试过了吗?

    public function getClientNameAttribute() {
        return Client::where('id', $this->client_id)->first()->name;
    }
    

    【讨论】:

    • 谢谢,但不起作用,first() 在这里不应该有任何不同的效果,因为没有相同 id 的客户端
    • 但是,您的域模型看起来有很多客户端。对吗?
    • 正确 - 有许多具有域的客户端,但我正在通过作为主键的客户端 ID 搜索客户端,因此只会返回一个结果。
    • 但是为什么不简单地使用嵌套模型并使用 $domain->client->name 呢?
    【解决方案3】:

    您以错误的方式访问它。属性名称以蛇的情况访问。所以,你应该使用:

    $domain->client_name
    

    【讨论】:

    • 完全相同的事情(获取客户名称)正在与我的其他模型/视图一起使用?我也试过client_name 还是不行
    • 那行得通吗?其他模型中可能还有其他一些有用的东西。其中的方法名称可能是getClientnameAttribute。注意namen这里小
    • no & 方法名称在其他模型中也完全相同
    • 可以分享控制器的代码吗?我认为您没有在所选列中选择 client_id。
    • public function index() { $domains = Domain::all(); return view('domains.index', compact('domains')); }
    【解决方案4】:

    如果你的模型看起来像

    class Domain extends Model {
    
    
        public function client() {
            return $this->belongsTo('App\Client');
        }
    
        public function getClientNameAttribute()
        {
            $client = $this->client;
            return $client ? $client->name : '';
        }
    
    }
    

    下面的代码必须能正常工作(没有错误)

    <tbody>
        @foreach( $domains as $domain )
        {!! Form::open(array('class' => 'form-inline', 'method' => 'DELETE', 'route' => array('domains.index', $domain->id))) !!}
        <tr>
            <td><a href="{{ route('domains.show', $domain->id) }}">{{ $domain->id }}</a></td>
            <td>{{ $domain->name }}</td>
            <td>{{ $domain->client_name }}</td>
            <td>{{ $domain->expiry_date }}</td>
            <td>  {!! link_to_route('domains.edit', 'Edit', array($domain->id), array('class' => 'btn btn-info')) !!}
                </td>
        </tr>
        {!! Form::close() !!}
    
        @endforeach
    </tbody>
    

    如果与域 $domain-&gt;client_name 关联的任何客户端将返回其名称,否则将返回 ''

    【讨论】:

    • 我的错,试试$domain-&gt;client_name
    【解决方案5】:

    添加新评论已经很晚了,但我一直希望有人能提供帮助。

    我遇到了这个问题,我通过改变函数的签名来解决它,从一个没有参数的函数变成一个有单个参数的函数,即要格式化的列的数据。

    传递这个函数

    public function getRejectedAttribute()
    {
        return $this->rejected ? true : false;
    }
    

    到这个其他功能

    public function getRejectedAttribute($nRejected)
    {
        return $nRejected ? true : false;
    }
    

    【讨论】:

      【解决方案6】:

      我删除了我的表并进行了新的迁移,现在一切正常,感谢所有尝试提供帮助的人

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-01
        • 2016-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-28
        • 1970-01-01
        相关资源
        最近更新 更多