Laravel,无法获得财产的财产

时间:2020-01-27 10:58:12

标签: php laravel eloquent

我有我的Form obj的Collection表单,而Iam试图让所有页面都属于该表单, 当我$survey = Forms::find(68)->with('pages')->get();收到此邮件时:

Illuminate\Database\Eloquent\Collection {#522 ▼
  #items: array:18 [▼
    0 => App\Models\Forms {#562 ▼
      #table: "forms"
      #fillable: array:4 [▶]
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:9 [▶]
      #original: array:9 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "pages" => Illuminate\Database\Eloquent\Collection {#596 ▼
          #items: array:5 [▼
            0 => App\Models\Pages {#628 ▶}
            1 => App\Models\Pages {#632 ▶}
            2 => App\Models\Pages {#633 ▶}
            3 => App\Models\Pages {#634 ▶}
            4 => App\Models\Pages {#635 ▶}
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
   ]
}

但是当我执行$ form-> pages Iam却为null时却无法获得关系的属性 它可以很好地处理其他财产,例如问题(关系相同)

这是我的模特:

class Forms extends Model
{
    protected $table = "forms";
    protected $fillable = array('name', 'description', 'status', 'token');


    public function pages()
    {
        return $this->hasMany('App\Models\Pages');
    }
   public function questions()
    {
        return $this->hasMany('App\Models\Question');
    }
}

class Pages extends Model
{
    protected $table = "pages";
    public $timestamps = false;
    protected $fillable = array('name', 'description' ,'priority', 'token');

    public function form()
    {
        return $this->belongsTo('App\Models\Forms');
    }

}

最后,我尝试获取结果的方法:

   public function index()
    {

        $survey = Forms::with('pages')->find(68);//Did update regarding to sugestion

        dd($survey);


        return view("pages.surveys", compact('survey'));
    }

感谢您的帮助。 格雷格

4 个答案:

答案 0 :(得分:2)

我认为您应该颠倒 <div className="Box"> <img src={(list.description.images && list.description.images[0]) ? list.description.images[0].url : ''} alt="pics" style={{ width: "270px", height: "270px" }} /> </div> with()函数的顺序。

find()

请注意,$survey = Forms::with('pages')->find(68); 实际上会执行查询,因此您不必再次使用find()。根据经验:如果只需要从查询中获得一个结果,则应使用get()first()(后者也可以用于检索集合),否则请使用{{1} }或find()来获取结果集合。

答案 1 :(得分:1)

$form = Forms::find(68);
dd($form->pages);

OR

$form = Form::find(68)->pages->get(); //It will return all the pages having relation

尝试此操作,它将首先找到ID为“ 68”的表单,然后根据给定的关系将其提取记录

答案 2 :(得分:0)

当我做dd($ survey);

   App\Models\Forms {#513 ▼
  #table: "forms"
  #fillable: array:4 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:9 [▶]
  #original: array:9 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [▼
    "pages" => Illuminate\Database\Eloquent\Collection {#521 ▼
      #items: array:6 [▼
        0 => App\Models\Pages {#552 ▼
          #table: "pages"
          +timestamps: false
          #fillable: array:4 [▶]
          #connection: "mysql"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:6 [▶]
          #original: array:6 [▶]
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          #hidden: []
          #visible: []
          #guarded: array:1 [▶]
        }
        1 => App\Models\Pages {#553 ▶}
        2 => App\Models\Pages {#554 ▶}
        3 => App\Models\Pages {#555 ▶}
        4 => App\Models\Pages {#556 ▶}
        5 => App\Models\Pages {#557 ▶}
      ]
    }
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

答案 3 :(得分:0)

似乎您的几乎所有代码都可以。这仅是您的外键可能有问题。在表单的页面关系中,laravel将期望页面表中的外键为“ form_id”或“ forms_id”,因为您没有在页面关系中传递第二个参数。确保在页面迁移中,您的外键也设置为form_id