Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

najmulcse's avatar

How to write php code in blade file ?

I need to write the code of model in a blade file . But how can possible to write? I have post.blade.php file where i showing all post . But i need to show the post contents (.pdf,.doc,.ppt etc) where these files are staying other table . I want to show these contents which are corresponding of individual post . For this , i'm checking these content by a model in the post.blade.php file . But its seem to wrong . How can solve the problem ?

0 likes
6 replies
MarioMacedo's avatar

I'm not sure if I understand your question, but what do you mean by "write the code of model" ?

Can you show us some code?

najmulcse's avatar

@MarioMacedo sorry ,it will be the " Controller code". I meant that I have a model named Content . I'm handling all of the posts within PostController class.

But in my blade file , i need to use some conditions . Where all posts may have file or not . I want to check which post contains file ,then i will show in the post.blade.php file

Here my post.blade.php file some portion of code are given bellow:

                                  <p>{{ $post->body }}</p>
                              @if($content=App\Content::where('post_id',$post->id))
                                      {{$content->content}}
                              @endif 
MarioMacedo's avatar
Level 13

@najmulcse First of all you can't do queries in your view.

If I understand correctly your Content can have a foreign key to a Post right?

If so, in your Post.php you should have a relation like this:

public function content() {
    return $this->hasOne(Post::class);
}

and in your view

    <p>{{ $post->body }}</p>
@if($post->content)
    {{$post->content->content}}
@endif 
1 like

Please or to participate in this conversation.