I have 3 tables
products
pages
gallery - two foreign keys page_id, product_id
(are the foreign keys supposed to be mapped together UNIQUE, why?)
<?php
class Gallery extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'gallery';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
public static $rules = array(
// 'email'=>'required|email|unique:users',
// 'password'=>'required|alpha_num|between:6,12|confirmed',
// 'password_confirmation'=>'required|alpha_num|between:6,12'
);
public function pages() {
return $this->belongsTo('Pages', 'page_id');
}
public function products() {
return $this->belongsTo('Products', 'products_id');
}
}
Pages Model
<?php
class Pages extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'pages';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
public static $rules = array(
'title'=>'required',
'body'=>'required'
// 'email'=>'required|email|unique:users',
// 'password'=>'required|alpha_num|between:6,12|confirmed',
// 'password_confirmation'=>'required|alpha_num|between:6,12'
);
public function gallery() {
return $this->hasOne('Gallery');
}
}
Products Model
<?php
class Products extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'products';
// public $timestamps = false;
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
public static $rules = array(
'title'=>'required',
'body'=>'required'
// 'email'=>'required|email|unique:users',
// 'password'=>'required|alpha_num|between:6,12|confirmed',
// 'password_confirmation'=>'required|alpha_num|between:6,12'
);
public function gallery()
{
return $this->hasOne('Gallery');
}
}
I'm doing this in PagesController:
$page = Pages::find($page_id);
$page->gallery throws this error Call to undefined method Gallery::newQuery();
- I created the Gallery table and foreign keys using migrations. Why do i have to create relationships between the models, these relationships are defined in the database? (le noob question)
2.Read a lot about what could be the cause of this and it's not namespacing. Been stuck on this for 3 days now, any help is greatly appreciated. I think is has something to do with my model relationships.
=============================== UPDATE =========================
Okay, so i did $gallery = new Gallery; in my PageController, and this seems to worky But now i get this funky error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (spaggo
.gallery
, CONSTRAINT gallery_product_id_foreign
FOREIGN KEY (product_id
) REFERENCES products
(id
) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into gallery
(page_id
, updated_at
, created_at
) values (6, 2015-06-21 15:24:51, 2015-06-21 15:24:51))
First, What's your Laravel version? It actually might be due to namespacing in spite of your assumption.
Secondly, as a convention I recommend you name your models singular such as Product and Page.
To answer your other question: Yes, your database may contain constrains for these relations, but unfortunately Eloquent ORM can't make use of them. You're still required to define these relations at the model level.