How can I get images under a blog for a user












0














What I actually want is, for a specific user, I'm trying to show every image under a single blog. What I'm getting is a single blog post images for every blog.



Controller



$user_id = Session::get('id');
$user = Users::find($user_id);
$blogs = Blog::where('user_id', $user_id)->paginate(10);
$blogImage = BlogImage::where('blog_id', $blogs->pluck('id'))->get();


return view('Users.userlayout', compact('user', 'blogCat', 'blogs', 'username', 'blogImage'));




View Page



@foreach($blogs as $blog)
<div class="post">

@foreach($blogImage as $img)
<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image"
class="img-responsive">
@endforeach

<p>
<?php $str = $blog->blog_desc; ?>
{{str_limit($str, 250, "...")}}
</p>
<a href="{{URL::to('/blog-details/'.$blog->id)}}" target="_blank" class="btn_1">
Read more
</a>
</div>
<hr>
@endforeach









share|improve this question
























  • Can you the code you in your models?
    – Peter Sowah
    Nov 21 '18 at 19:07










  • In those modes, I just added the tables name, nothing else. Cause I'm not that good to use models function like hasMany, belongsTo.
    – Eyakub
    Nov 21 '18 at 21:32
















0














What I actually want is, for a specific user, I'm trying to show every image under a single blog. What I'm getting is a single blog post images for every blog.



Controller



$user_id = Session::get('id');
$user = Users::find($user_id);
$blogs = Blog::where('user_id', $user_id)->paginate(10);
$blogImage = BlogImage::where('blog_id', $blogs->pluck('id'))->get();


return view('Users.userlayout', compact('user', 'blogCat', 'blogs', 'username', 'blogImage'));




View Page



@foreach($blogs as $blog)
<div class="post">

@foreach($blogImage as $img)
<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image"
class="img-responsive">
@endforeach

<p>
<?php $str = $blog->blog_desc; ?>
{{str_limit($str, 250, "...")}}
</p>
<a href="{{URL::to('/blog-details/'.$blog->id)}}" target="_blank" class="btn_1">
Read more
</a>
</div>
<hr>
@endforeach









share|improve this question
























  • Can you the code you in your models?
    – Peter Sowah
    Nov 21 '18 at 19:07










  • In those modes, I just added the tables name, nothing else. Cause I'm not that good to use models function like hasMany, belongsTo.
    – Eyakub
    Nov 21 '18 at 21:32














0












0








0







What I actually want is, for a specific user, I'm trying to show every image under a single blog. What I'm getting is a single blog post images for every blog.



Controller



$user_id = Session::get('id');
$user = Users::find($user_id);
$blogs = Blog::where('user_id', $user_id)->paginate(10);
$blogImage = BlogImage::where('blog_id', $blogs->pluck('id'))->get();


return view('Users.userlayout', compact('user', 'blogCat', 'blogs', 'username', 'blogImage'));




View Page



@foreach($blogs as $blog)
<div class="post">

@foreach($blogImage as $img)
<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image"
class="img-responsive">
@endforeach

<p>
<?php $str = $blog->blog_desc; ?>
{{str_limit($str, 250, "...")}}
</p>
<a href="{{URL::to('/blog-details/'.$blog->id)}}" target="_blank" class="btn_1">
Read more
</a>
</div>
<hr>
@endforeach









share|improve this question















What I actually want is, for a specific user, I'm trying to show every image under a single blog. What I'm getting is a single blog post images for every blog.



Controller



$user_id = Session::get('id');
$user = Users::find($user_id);
$blogs = Blog::where('user_id', $user_id)->paginate(10);
$blogImage = BlogImage::where('blog_id', $blogs->pluck('id'))->get();


return view('Users.userlayout', compact('user', 'blogCat', 'blogs', 'username', 'blogImage'));




View Page



@foreach($blogs as $blog)
<div class="post">

@foreach($blogImage as $img)
<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image"
class="img-responsive">
@endforeach

<p>
<?php $str = $blog->blog_desc; ?>
{{str_limit($str, 250, "...")}}
</p>
<a href="{{URL::to('/blog-details/'.$blog->id)}}" target="_blank" class="btn_1">
Read more
</a>
</div>
<hr>
@endforeach






laravel image blogs laravel-5.6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 20:16









Ross Wilson

15.7k22539




15.7k22539










asked Nov 21 '18 at 18:33









EyakubEyakub

53




53












  • Can you the code you in your models?
    – Peter Sowah
    Nov 21 '18 at 19:07










  • In those modes, I just added the tables name, nothing else. Cause I'm not that good to use models function like hasMany, belongsTo.
    – Eyakub
    Nov 21 '18 at 21:32


















  • Can you the code you in your models?
    – Peter Sowah
    Nov 21 '18 at 19:07










  • In those modes, I just added the tables name, nothing else. Cause I'm not that good to use models function like hasMany, belongsTo.
    – Eyakub
    Nov 21 '18 at 21:32
















Can you the code you in your models?
– Peter Sowah
Nov 21 '18 at 19:07




Can you the code you in your models?
– Peter Sowah
Nov 21 '18 at 19:07












In those modes, I just added the tables name, nothing else. Cause I'm not that good to use models function like hasMany, belongsTo.
– Eyakub
Nov 21 '18 at 21:32




In those modes, I just added the tables name, nothing else. Cause I'm not that good to use models function like hasMany, belongsTo.
– Eyakub
Nov 21 '18 at 21:32












1 Answer
1






active

oldest

votes


















1














This is because you're using where instead of whereIn.



If you try and pass an array or a collection to where it will only use the first value.



$blogImage = BlogImage::whereIn('blog_id', $blogs->pluck('id'))->get();


Since this will return all of the BlogImage's associated with the Blog's the in the paginated list I would imagine you'll need to do a check to make sure you're only displaying the images that are associated with the specific Blog. One way you can do this is by using `@continue():



@foreach($blogImage as $img)

@continue($blogImage->blog_id !== $blog->id)

<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image" class="img-responsive">

@endforeach




All of that being said I would recommend using a one-to-many relationship between Blog and BlogImage:



Blog



public function images()
{
return $this->hasMany(BlogImage::class);
}


BlogImage



public function blog()
{
return $this->belongTo(Blog::class);
}


Then in your controller you can Eager load the images and have something like:



$blogs = Blog::with('images')->where('user_id', $user_id)->paginate(10);


And your blade file would have:



@foreach($blog->images as $image)

<img src="{{asset('storage/blog_img/'.$image->blog_img)}}" alt="Image" class="img-responsive">

@endforeach


You could then apply the same one-to-many relationship logic between User and Blog as well.






share|improve this answer























  • sorry for a silly question, do I have to use this @foreach($blog->images as $image) under @foreach($blogs as $blog)?
    – Eyakub
    Nov 21 '18 at 21:13












  • btw, Thanks, I guess I solved it with your help, thanks for your valuable time. btw in Blog::with('images') is that the function that i declared? or just passing the variable?
    – Eyakub
    Nov 21 '18 at 21:34










  • @Eyakub Yes, you would need to put @foreach($blog->images as $image) under @foreach($blogs as $blog). The with() method allows you to eager load your relationships so the with('images') will load the relationship defined in the images() method in your Blog class.
    – Ross Wilson
    Nov 21 '18 at 23:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418501%2fhow-can-i-get-images-under-a-blog-for-a-user%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This is because you're using where instead of whereIn.



If you try and pass an array or a collection to where it will only use the first value.



$blogImage = BlogImage::whereIn('blog_id', $blogs->pluck('id'))->get();


Since this will return all of the BlogImage's associated with the Blog's the in the paginated list I would imagine you'll need to do a check to make sure you're only displaying the images that are associated with the specific Blog. One way you can do this is by using `@continue():



@foreach($blogImage as $img)

@continue($blogImage->blog_id !== $blog->id)

<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image" class="img-responsive">

@endforeach




All of that being said I would recommend using a one-to-many relationship between Blog and BlogImage:



Blog



public function images()
{
return $this->hasMany(BlogImage::class);
}


BlogImage



public function blog()
{
return $this->belongTo(Blog::class);
}


Then in your controller you can Eager load the images and have something like:



$blogs = Blog::with('images')->where('user_id', $user_id)->paginate(10);


And your blade file would have:



@foreach($blog->images as $image)

<img src="{{asset('storage/blog_img/'.$image->blog_img)}}" alt="Image" class="img-responsive">

@endforeach


You could then apply the same one-to-many relationship logic between User and Blog as well.






share|improve this answer























  • sorry for a silly question, do I have to use this @foreach($blog->images as $image) under @foreach($blogs as $blog)?
    – Eyakub
    Nov 21 '18 at 21:13












  • btw, Thanks, I guess I solved it with your help, thanks for your valuable time. btw in Blog::with('images') is that the function that i declared? or just passing the variable?
    – Eyakub
    Nov 21 '18 at 21:34










  • @Eyakub Yes, you would need to put @foreach($blog->images as $image) under @foreach($blogs as $blog). The with() method allows you to eager load your relationships so the with('images') will load the relationship defined in the images() method in your Blog class.
    – Ross Wilson
    Nov 21 '18 at 23:40
















1














This is because you're using where instead of whereIn.



If you try and pass an array or a collection to where it will only use the first value.



$blogImage = BlogImage::whereIn('blog_id', $blogs->pluck('id'))->get();


Since this will return all of the BlogImage's associated with the Blog's the in the paginated list I would imagine you'll need to do a check to make sure you're only displaying the images that are associated with the specific Blog. One way you can do this is by using `@continue():



@foreach($blogImage as $img)

@continue($blogImage->blog_id !== $blog->id)

<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image" class="img-responsive">

@endforeach




All of that being said I would recommend using a one-to-many relationship between Blog and BlogImage:



Blog



public function images()
{
return $this->hasMany(BlogImage::class);
}


BlogImage



public function blog()
{
return $this->belongTo(Blog::class);
}


Then in your controller you can Eager load the images and have something like:



$blogs = Blog::with('images')->where('user_id', $user_id)->paginate(10);


And your blade file would have:



@foreach($blog->images as $image)

<img src="{{asset('storage/blog_img/'.$image->blog_img)}}" alt="Image" class="img-responsive">

@endforeach


You could then apply the same one-to-many relationship logic between User and Blog as well.






share|improve this answer























  • sorry for a silly question, do I have to use this @foreach($blog->images as $image) under @foreach($blogs as $blog)?
    – Eyakub
    Nov 21 '18 at 21:13












  • btw, Thanks, I guess I solved it with your help, thanks for your valuable time. btw in Blog::with('images') is that the function that i declared? or just passing the variable?
    – Eyakub
    Nov 21 '18 at 21:34










  • @Eyakub Yes, you would need to put @foreach($blog->images as $image) under @foreach($blogs as $blog). The with() method allows you to eager load your relationships so the with('images') will load the relationship defined in the images() method in your Blog class.
    – Ross Wilson
    Nov 21 '18 at 23:40














1












1








1






This is because you're using where instead of whereIn.



If you try and pass an array or a collection to where it will only use the first value.



$blogImage = BlogImage::whereIn('blog_id', $blogs->pluck('id'))->get();


Since this will return all of the BlogImage's associated with the Blog's the in the paginated list I would imagine you'll need to do a check to make sure you're only displaying the images that are associated with the specific Blog. One way you can do this is by using `@continue():



@foreach($blogImage as $img)

@continue($blogImage->blog_id !== $blog->id)

<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image" class="img-responsive">

@endforeach




All of that being said I would recommend using a one-to-many relationship between Blog and BlogImage:



Blog



public function images()
{
return $this->hasMany(BlogImage::class);
}


BlogImage



public function blog()
{
return $this->belongTo(Blog::class);
}


Then in your controller you can Eager load the images and have something like:



$blogs = Blog::with('images')->where('user_id', $user_id)->paginate(10);


And your blade file would have:



@foreach($blog->images as $image)

<img src="{{asset('storage/blog_img/'.$image->blog_img)}}" alt="Image" class="img-responsive">

@endforeach


You could then apply the same one-to-many relationship logic between User and Blog as well.






share|improve this answer














This is because you're using where instead of whereIn.



If you try and pass an array or a collection to where it will only use the first value.



$blogImage = BlogImage::whereIn('blog_id', $blogs->pluck('id'))->get();


Since this will return all of the BlogImage's associated with the Blog's the in the paginated list I would imagine you'll need to do a check to make sure you're only displaying the images that are associated with the specific Blog. One way you can do this is by using `@continue():



@foreach($blogImage as $img)

@continue($blogImage->blog_id !== $blog->id)

<img src="{{asset('storage/blog_img/'.$img->blog_img)}}" alt="Image" class="img-responsive">

@endforeach




All of that being said I would recommend using a one-to-many relationship between Blog and BlogImage:



Blog



public function images()
{
return $this->hasMany(BlogImage::class);
}


BlogImage



public function blog()
{
return $this->belongTo(Blog::class);
}


Then in your controller you can Eager load the images and have something like:



$blogs = Blog::with('images')->where('user_id', $user_id)->paginate(10);


And your blade file would have:



@foreach($blog->images as $image)

<img src="{{asset('storage/blog_img/'.$image->blog_img)}}" alt="Image" class="img-responsive">

@endforeach


You could then apply the same one-to-many relationship logic between User and Blog as well.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 20:38

























answered Nov 21 '18 at 20:32









Ross WilsonRoss Wilson

15.7k22539




15.7k22539












  • sorry for a silly question, do I have to use this @foreach($blog->images as $image) under @foreach($blogs as $blog)?
    – Eyakub
    Nov 21 '18 at 21:13












  • btw, Thanks, I guess I solved it with your help, thanks for your valuable time. btw in Blog::with('images') is that the function that i declared? or just passing the variable?
    – Eyakub
    Nov 21 '18 at 21:34










  • @Eyakub Yes, you would need to put @foreach($blog->images as $image) under @foreach($blogs as $blog). The with() method allows you to eager load your relationships so the with('images') will load the relationship defined in the images() method in your Blog class.
    – Ross Wilson
    Nov 21 '18 at 23:40


















  • sorry for a silly question, do I have to use this @foreach($blog->images as $image) under @foreach($blogs as $blog)?
    – Eyakub
    Nov 21 '18 at 21:13












  • btw, Thanks, I guess I solved it with your help, thanks for your valuable time. btw in Blog::with('images') is that the function that i declared? or just passing the variable?
    – Eyakub
    Nov 21 '18 at 21:34










  • @Eyakub Yes, you would need to put @foreach($blog->images as $image) under @foreach($blogs as $blog). The with() method allows you to eager load your relationships so the with('images') will load the relationship defined in the images() method in your Blog class.
    – Ross Wilson
    Nov 21 '18 at 23:40
















sorry for a silly question, do I have to use this @foreach($blog->images as $image) under @foreach($blogs as $blog)?
– Eyakub
Nov 21 '18 at 21:13






sorry for a silly question, do I have to use this @foreach($blog->images as $image) under @foreach($blogs as $blog)?
– Eyakub
Nov 21 '18 at 21:13














btw, Thanks, I guess I solved it with your help, thanks for your valuable time. btw in Blog::with('images') is that the function that i declared? or just passing the variable?
– Eyakub
Nov 21 '18 at 21:34




btw, Thanks, I guess I solved it with your help, thanks for your valuable time. btw in Blog::with('images') is that the function that i declared? or just passing the variable?
– Eyakub
Nov 21 '18 at 21:34












@Eyakub Yes, you would need to put @foreach($blog->images as $image) under @foreach($blogs as $blog). The with() method allows you to eager load your relationships so the with('images') will load the relationship defined in the images() method in your Blog class.
– Ross Wilson
Nov 21 '18 at 23:40




@Eyakub Yes, you would need to put @foreach($blog->images as $image) under @foreach($blogs as $blog). The with() method allows you to eager load your relationships so the with('images') will load the relationship defined in the images() method in your Blog class.
– Ross Wilson
Nov 21 '18 at 23:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418501%2fhow-can-i-get-images-under-a-blog-for-a-user%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'