0

I have Collection form my Form obj, and iam trying to get all pages belongs to that form, when I do $survey = Forms::find(68)->with('pages')->get(); Iam getting this:

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 [▶]
    }
   ]
}

but can't get property of relations, when I do $form->pages Iam getting null Its works fine for other property like questions (same relations)

here is my model:

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');
    }

}

and finally, method where Iam trying to get results:

   public function index()
    {

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

        dd($survey);


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

Thanks for any help. Greg

4 Answers4

2

I think you should reverse the order of the with() and find() functions.

$survey = Forms::with('pages')->find(68);

Note that find() actually executes the query, therefore you don't have to use get() again. As a rule of thumb: if you need to get only one result out of your query you should use first() or find() (the latter could also be used to retrieve collections), otherwise use all() or get() to fetch a collection of results.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mdexp
  • 3,492
  • 2
  • 9
  • 20
1
$form = Forms::find(68);
dd($form->pages);

OR

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

try this, It will first find form having id '68' then as per relation given it will fetch the records

Varsha Jadhav
  • 81
  • 1
  • 5
0

when i do 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 [▶]
}
  • so eloquent get relation right (Iam able to see it) but when i do $survey->pages , Iam getting null – Grzegorz Grzegorz Jan 27 '20 at 11:16
  • Try this one $form = Form::find(68)->pages; – Qonvex620 Jan 27 '20 at 11:29
  • getting null, ist onlly works when i do Form::find(68)->pages()->get(); – Grzegorz Grzegorz Jan 27 '20 at 11:30
  • why I can't get property if its listed on output for dd($survey) when I used '->with' method ?\ – Grzegorz Grzegorz Jan 27 '20 at 11:31
  • the reason why i don't want to use above is that I want to get more than one form (find by specific id is just a test) so if I would like to count pages for specific form I need to do many queries (for each loop) – Grzegorz Grzegorz Jan 27 '20 at 11:34
  • If you only need to count the number of pages for each form you could as well do: `$survey = Forms::withCount('pages')->get()`. Each result should then have a `pages_count` property that you should be able to access in a loop – mdexp Jan 27 '20 at 11:39
  • thx , that will help, but still wondering why I can't access $survey->pages. Alos I might need to get more info about each page too – Grzegorz Grzegorz Jan 27 '20 at 11:41
0

Seems almost of your codes are okay. This might be a problem only with your foreign key. In your pages relationship in your forms, laravel will expect a foreign key in your pages table namely 'form_id' or 'forms_id' since you are not passing a second argument in your pages relationship. Make sure that in your pages migration your foreign key is set to to form_id also

Qonvex620
  • 3,819
  • 1
  • 8
  • 15