PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 4, 2022

[FIXED] select id from post table where title=$title

 February 04, 2022     laravel, mysql, php     No comments   

Issue

i have post table and i have comment table i have made them with laravel i made a IndexController and it has a method that returns all of my posts by their title; i also return and compact comment of each post and show them under each post content but i have to pass the post id to comment table by myself for example i have a post with id=7 and i have to pass the 7 to post_id column of comment table i have to write a query something like this

select id from posts where title=$title

with this query i want to select post id by post title and pass it to post_id in my comment table

this my index controller

public function single($title)
{
    $posts = Post::where('title', $title)->get();

    $comments = Comment::where([
        ['post_id', 7],
        ['verify',1]
    ])->orderBy('id', 'desc')->get();
    return view('main.single', compact('posts', 'comments'));
}

this is comment controller

public function store(Request $request)
{
    Comment::create([
        'post_id' => $request['post_id'],
        'user_id' => Auth::user()->id,
        'message' => $request['message'],
    ]);
}

and also this my single blade file code

@foreach($comments as $comment)
            <div class="container">
                <div class="bg-secondary bg-opacity-10 p-3 mb-4">
                    <div>
                        <p class="fw-bold">{{$comment->user->first_name.' '.$comment->user->last_name}}
                            <br>
                            <span class="text-muted text-small-14 fw-light">{{$comment->created_at}}</span>
                        </p>

                    </div>
                    <div class="mt-0">

                    </div>
                    <div>
                        {{$comment->message}}
                    </div>
                </div>
            </div>
        @endforeach

and also i use laravel 8


Solution

You can do the following if you don't want to pass by yourself, assuming your relationships are correct. If not, feel free to include them in your current question, and I can help with those too.

public function single($title)
{
    $post = Post::with(['comments' => function ($query) {
        $query->where('verify', 1)->orderBy('id', 'desc');
    }])->where('title', $title)->first();

    return view('main.single', compact('post'));
}

This will allow you to pass just a post to your blade view.

Now instead of

@foreach($comments as $comment)

You can

@foreach($post->comments as $comment)


Answered By - Nicklas Kevin Frank
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing