質問

I have a little issue with Laravel. I'm totally new with it, and am trying to create a multilang model for some content I have.

This is my configuration :

Database :

Table appartements : 1 id int(10) 2 address varchar(255)
3 postcode int(11) 4 city varchar(255)
5 country varchar(255)
6 created_at timestamp 7 updated_at timestamp

Table appartement_langs 1 id int(10)
2 language_id int(11) 3 appartement_id int(11) 4 title varchar(255)
5 subtitle varchar(255) 6 description text
7 created_at timestamp 8 updated_at timestamp

Table languages 1 id int(10)
2 locale varchar(255)
3 created_at timestamp
4 updated_at timestamp

So basically I have the language_id and the appartement_id.

In my Appartement model I have the following :

    <?php

    class Appartement extends Eloquent {
        protected $guarded = array();

        public static $rules = array();

        public function gallery()
        {
            return $this->hasMany('Gallery');
        }

        public function languages()
        {
            return $this->hasMany('AppartementLang', 'language_id');
        }

        public function lang()
        {
            return $this->hasOne('AppartementLang');
        }
    }

When I try to get the field $appartement->lang()->title I get nothing.

Can you give me a hint ? Because I really don't know how to handle this :).

Thanks a lot.

役に立ちましたか?

解決

When you use lang() you are actually returning the query partially built (so you can add on other conditions, etc..)

If you want to retrieve the language model you only have to use lang.

So:

$appartement->lang->title

If you var_dump() the lang() you will see for yourself.

In the Laravel documentation you will see their relationship example calls:

$phone = User::find(1)->phone;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top