J
J
Jedi2020-06-23 07:17:46
Laravel
Jedi, 2020-06-23 07:17:46

How can I implement a bunch of two models?

public function showPost(Post $post) { return $post };


public function showPostWithContent(Post $post, Content $content) { return $content };


Each Post has its own unique slug .
The content also has its own slug , but not a unique one. There is also a post_id .

The showPost method shows the post, also a list of content.
The showPostWithContent method returns both the Post and the Content "inherited" from this Post (which is what I'm trying to implement).

1. It can be implemented so that Content has a unique slug , which solves the task at once.
2. You can write Middleware that will look for content in the collectionpost .
3. The second method, only inside the method without Middleware .

This is what comes to mind...

How would you implement it?
How to implement it correctly?)

Thank you!

Addition:

Relationships - implemented. Everything is correct and it works!

Posts may vary. The types are as follows - Article, Video, etc.

Posts table: title, description, article_id (nullable), video_id (nullable), question_id (nullable).

It turns out that the post is dynamic in terms of content.

The content, meanwhile, is tightly linked to the Post. Foreign, on delete cascade.

But if we pick up the following route... /posts/{post = slug}/{content = content_slug} we don't actually check if the given content belongs to the current post. What a hole - if we take the content slug from another post, which id = 2 (Post: id = 2), and write /posts/{post: id = 1}/content_slug (inheriting from post: id =1), we we'll get it. It should not be.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vism, 2020-06-23
@PHPjedi

But if we pick up the following route... /posts/{post = slug}/{content = content_slug} we don't actually check if the given content belongs to the current post. What a hole - if we take the content slug from another post, which id = 2 (Post: id = 2), and write /posts/{post: id = 1}/content_slug (inheriting from post: id =1), we we'll get it. It should not be.

this is where you need to check that everything is fine and issue a response (for example, 404 if the user entered an invalid URL)

J
jazzus, 2020-06-23
@jazzus

Unique slug and think about the structure of the database and relationships

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question