0

I am trying to fetch records from tow tables with an existing values.

Sample table data:

tbl1
id   |   name      |    tbl2_id
-----+-------------+--------------
 1   | file.jpg    |  1
 2   | flower.jpg  |  5
 3   | project.zip |  3
 4   | cat.jpg     |  2
 5   | flower.jpg  |  1
 6   | file.php    |  4


tbl2
id   |   name      |    parent_id
-----+-------------+---------------
 1   |  Pictures   |  null
 2   |  Samples    |  1
 3   |  Files      |  null
 4   |  Samples    |  3
 5   |  mypics     |  2

the tbl1.tbls2_id is a field to store tbl2.id for making relation between these tables. tbl2.parent_id for store parents from tbl2

Now I have this string :

Pictures/Sample/mypics/flower.jpg

Is there any way to write a Model or everything else to return an array like this :

[
   'file_id' => 2,
   'directories' => [
        1, 2, 5
   ]

]
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
MajAfy
  • 3,007
  • 10
  • 47
  • 83
  • 2
    You are expected to try to **write the code yourself**. After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Apr 25 '17 at 12:17
  • Take a look at Eloquent relationships. https://laravel.com/docs/5.4/eloquent-relationships – Sapnesh Naik Apr 25 '17 at 12:18
  • i think the second table is not huge. It's fuster to load all it in memory and biuld `directory` array without query database – splash58 Apr 25 '17 at 12:19
  • @JayBlanchard Thanks for your guide, you right, but I don't know what should I do exactly, maybe my database structure have problem – MajAfy Apr 25 '17 at 12:20
  • Possible duplicate of [MySQL - Recursing a tree structure](http://stackoverflow.com/questions/2378678/mysql-recursing-a-tree-structure) – Alex Blex Apr 25 '17 at 12:34
  • With the information you provided. I can think of a solution by taking your tables data into an array. Then explode the String. take file name id from table one and other data from table two. – Ajay Apr 25 '17 at 12:35

0 Answers0