0

I am working over a basic system where blogs retrieve the image from other database table but it shows the posts not the photos here is my controller:

$bloginfo=bloginfo::where('blog_name','=',$name)->first();
    $blogposts=pageposts::where('p_id','=',$bloginfo->id)->orderBy('id','desc')->get();
    foreach($blogposts as $posts){
        $photoscount=pagephotos::where('b_id','=',$posts->id)->count();
        $blogphotos=pagephotos::where('b_id','=',$posts->id)->get();

    }
    return View::make('myblog')
    ->with('bloginfo',$bloginfo)
    ->with('blogposts',$blogposts)
    ->with('blogphotos',$blogphotos)
    ->with('photoscount',$photoscount);

and here is my view:

@foreach($blogposts as $posts)
                    <div class="panel panel-default">    
                        <div class="panel-body">
                            @if($photoscount==0)
                                {{ null }}
                            @elseif($photoscount==1)
                           //a bunch of code here
                              @else

                               //a bunch of code here
                              @endif
                            <h4><a href="">{{ $posts->title }}</a></h4>
                            <p>{{ $posts->description }}</p>

                        </div>
                    </div>
                    @endforeach
4
  • Are you getting photos in your controller? Have you checked using print_r ? Commented Nov 3, 2014 at 10:49
  • I am only retriving the phots name based on the post. Means when i post title description and more than one images in a post so thr title and drscription will be inserted in a table than the photos names will be posted in the other table with the same blog id(b_id) now i want to retrive images with the blogs. Commented Nov 3, 2014 at 10:55
  • Are you getting image url ? or image name? if image name then are these images saved on the same server. Or you got some path to access them. Commented Nov 3, 2014 at 11:01
  • I want to get the image name and by the way thank you i found my answer. I should get the results in the view not at controller thabk you Commented Nov 3, 2014 at 11:03

1 Answer 1

1

Might i suggest you looking into to relationships, this could also be your answer.

You can eager load all the data within one query.

bloginfo::with('posts', 'posts.images')->where('blog_name','=',$name)->first();

This will load the blog posts with all the information that relates to it, i.e. the posts, and their images. Look here for setting them up. HERE

As your current problem is within your for loop you overwrite the variables you are setting n times, i.e. by the amount of posts there are. So potentially you will always have different results.

Sign up to request clarification or add additional context in comments.

Comments

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.