0

I have a problem that I couldn't deal with it. I have a News table includes TitleID, TextID, ImageID. And three more tables Titles, Texts, Images. I want to get all of them in one model. But when I try, got result like array in array . But I want it like:

[ News: [ { ID, Title, Text, Image } ] ]

Eloquent ORM responds it like:

[ News: [ { ID, Title: [ID, Title], Text: [ID, Text], Image: [ID, Image] } ] ]

Database Structure

News =>

+-----------------+-------------+------+-----+---------+----------------+
| Field           | Type        | Null | Key | Default | Extra          |
+-----------------+-------------+------+-----+---------+----------------+
| ID              | int(11)     | NO   | PRI | NULL    | auto_increment |
| TitleID         | int(11)     | NO   |     | NULL    |                |
| TextID          | int(11)     | NO   |     | NULL    |                |
| ImageID         | int(11)     | NO   |     | NULL    |                |
| CatID           | int(11)     | NO   |     | NULL    |                |
| OlusturmaZamani | datetime    | NO   |     | NULL    |                |
| YayinZamani     | datetime    | NO   |     | NULL    |                |
| DuzenlemeZamani | datetime    | YES  |     | NULL    |                |
| Onay            | tinyint(11) | NO   |     | NULL    |                |
| Hit             | int(11)     | YES  |     | NULL    |                |
+-----------------+-------------+------+-----+---------+----------------+

Titles =>

+------------+---------+------+-----+---------+----------------+
| Field      | Type    | Null | Key | Default | Extra          |
+------------+---------+------+-----+---------+----------------+
| ID         | int(11) | NO   | PRI | NULL    | auto_increment |
| TitleText  | text    | NO   |     | NULL    |                |
+------------+---------+------+-----+---------+----------------+

the other tables like title table.

Enes Yurtlu
  • 63
  • 1
  • 5
  • Can you show the code please? – Hedegare Oct 31 '17 at 16:20
  • Seems like you need a classic SQL join to me – DevK Oct 31 '17 at 16:23
  • I think you need to declare the relationships between your News model and the rest of your models (Title, Text and Image). Then you will need to [append](https://laravel.com/docs/5.5/eloquent-serialization#appending-values-to-json) the `title`, `text` and `image` attributes to your News model. – Camilo Oct 31 '17 at 16:24
  • To start with, why would you separate title or text from news table? Do you think many news can have the same text or title? – lchachurski Oct 31 '17 at 18:21

3 Answers3

1

Is there a reason you've extracted Title and Text to their own table?

A solution for you would be something like what you see in this post:

Add a custom attribute to a Laravel / Eloquent model on load?

Tweaked for your example:

Assume you changed the relationShip to "titleTable", just to avoid collision

class News extends Eloquent {

    protected $appends = array('title');

    public function getTitleAttribute()
    {
        return $this->titleTable->TitleText;  
    }
}
Chris Phillips
  • 673
  • 5
  • 10
0

Try eager loading.

If you have created relationships among News, Titles, Text and Image tables, you can load the relationship models by eager loading.

$news = News::with('title','text','image)->find(1);
Abid Raza
  • 745
  • 8
  • 15
0

Sounds like that ORM "over-normalizes". There is no reasonable reason for news attributes such as title, text, and image to be put in separate tables JOINed by an "id".

"Eager loading" sounds like a kludge to make it feel like the columns are in the News table when they aren't. This leads to unnecessary inefficiencies.

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • Thanks for your answer. I agree with you now. It will give inefficiencies. – Enes Yurtlu Nov 05 '17 at 22:24
  • The ORM doesn't make you overnormalize. That was a design decision made by the person that wrote the code.. – Chris Phillips Nov 23 '17 at 08:58
  • @ChrisPhillips - Thanks for the comment. Can you give some keywords that characterize the decision? That way I can say "..." instead of "over-normalize". (This is not the only Question like this.) – Rick James Nov 23 '17 at 15:40
  • @RickJames Oh, I think "over-normalizes" would be a good description. The table structure is "over-normalized", not the ORM itself. "eager loading" is something you'd use if a record has an associated model you know you are going to be looking at in the results (eg user::with('profile')). It allows the ORM to make a more efficient query up front. – Chris Phillips Nov 24 '17 at 03:55