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
   ]

]
6
  • 2
    You are expected to try to write the code yourself. After doing more research 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. Read How to Ask a good question. Be sure to take the tour and read this. Commented Apr 25, 2017 at 12:17
  • Take a look at Eloquent relationships. laravel.com/docs/5.4/eloquent-relationships Commented Apr 25, 2017 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 Commented Apr 25, 2017 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 Commented Apr 25, 2017 at 12:20
  • Possible duplicate of MySQL - Recursing a tree structure Commented Apr 25, 2017 at 12:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.