use nested foreach for showing posts and comments in laravel - laravel

how can i use nested foreach for showing posts and comments in laravel ?
this is my Route :
Route::get('/showpost', function () {
$showpost = Posts::all();
$comment = Comment::all();
return view('showpost', compact('showpost', 'comment'));
});
and here it's my view :
<h1>Posts :</h1>
#foreach($showpost as $showpost)
<h1>{{ $showpost->Title }}</h1>
<h5>{{ $showpost->Content }}</h5>
{{ Form::open(array('url'=>'showpost','method'=>'post')) }}
<h5>{{ Form::label('Comment : ') }} {{ Form::text('Comment') }}</h5>
{{ Form::hidden('invisible', $showpost->ID) }}
{{ Form::submit('Send Comment') }}
{{ Form::close() }}
<hr style='margin-top: 10%'>
#endforeach
<hr>
<h1>Comments :</h1>
#foreach($comment as $comment)
{{ $comment->Comment }} -> {{ $comment->PostID }} <br>
#endforeach
Posts show here and a textbox under it show too and we can add comment
i want to show comments about the posts under each posts
now i can't use nested foreach!!!

You should use eloquent relationship(). In your post model-
public function comments()
{
return $this->hasMany('App\Comment');
}
And then in your controller -
$showposts = Posts::with('comments')->get();
return view('showpost')->with('showposts', $showposts);
And in your view-
<h1>Posts :</h1>
#foreach($showposts as $showpost)
<h1>{{ $showpost->Title }}</h1>
<h5>{{ $showpost->Content }}</h5>
{{ Form::open(array('url'=>'showpost','method'=>'post')) }}
<h5>{{ Form::label('Comment : ') }} {{ Form::text('Comment') }}</h5>
{{ Form::hidden('invisible', $showpost->ID) }}
{{ Form::submit('Send Comment') }}
{{ Form::close() }}
<hr style='margin-top: 10%'>
<hr>
<h1>Comments :</h1>
#foreach($showpost->comments as $comment)
{{ $comment->Comment }} -> {{ $comment->PostID }} <br>
#endforeach
#endforeach
This way you will get comments associate with your posts.

Related

If condition inside foreach loop in Laravel

I'm new to laravel. How can i put if condition inside the foreach loop in blade template
my code is:
enter code here#foreach ($data as $data)
question: {{ $data->qid }}
Your answer: {{ $data->answer }}
Correct answer: {{ $data->anskey }}
#endforeach
#foreach($data as $data)
Question: {{ $data->qid }}
Your answer: {{ $data->answer }}
#if($data->anskey === $data->answer)
You correct
#else
You incorrect
#endif
#endforeach
####Ols
#foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
#endforeach
####new syntix for and if
#forelse ($users as $user)
<li>{{ $user->name }}</li>
#empty
<p>No users</p>
#endforelse

how to access properties from Laravel collections within foreach

I have a Laravel collection with relations returned to a view and I need to access it's properties;
controller
$project = Project::with('pulls.builds')->where('id',$id)->get();
view
#foreach ($project as $project->pulls)
{{ $project->pulls }}
#endforeach
output
{"id":2,"created_at":null,"updated_at":null,"name":"Project Name","pulls":[{"id":2,"created_at":null,"updated_at":null,"branch":".fixes","project_id":2,"builds":[{"id":3,"created_at":null,"updated_at":null,"description":"43662-4768-456","pull_id":2}]},{"id":3,"created_at":null,"updated_at":null,"branch":".test","project_id":2,"builds":[{"id":4,"created_at":null,"updated_at":null,"description":"build 1","pull_id":3}]}]}
I have all the data that I want, now I need to make it look like this when viewed by the user:
Project Name
.fixes
43662-4768-456
.test
build 1
$projects = Project::with('pulls.builds')->where('id',$id)->get();
Given the structure of the json, to echo out every property you'd need to do something like this
#foreach($projects as $project)
{{ $project->id }}
{{ $project->created_at }}
{{ $project->updated_at }}
{{ $project->name }}
#foreach($project->pulls as $pull)
{{ $pull->id }}
{{ $pull->created_at }}
{{ $pull->updated_at }}
{{ $pull->branch }}
{{ $pull->project_id }}
#foreach($pull->builds as $build)
{{ $build->id }}
{{ $build->created_at }}
{{ $build->updated_at }}
{{ $build->description }}
{{ $build->pull_id }}
#endforeach
#endforeach
#endforeach
So taking only what you need, and adding some html, it could end up looking like this:
<p>Projects:</p>
#foreach($projects as $project)
<p>Project Name: <span>{{ $project->name }}</span></p>
<p>Pulls:</p>
#foreach($project->pulls as $pull)
<p>Branch: <span>{{ $pull->branch }}</span></p>
<p>Builds:</p>
#foreach($pull->builds as $build)
<p>Description: <span>{{ $build->description }}</span></p>
#endforeach
#endforeach
#endforeach
If you only want a single project, instead of using ->where('id', $id)->get(), use ->find($id) and you can remove the first #foreach.

How can I use an array index as form name in foreach loop?

I am trying to generate a list of forms using a foreach loop. The forms are just item names that use POST to pass their id to my controller so they can be updated. I am using onclick="form.submit()" to get the action.
This works as expected when there is only one item in list.
#foreach($items as $item)
{{ Form::open(['url'=>'item/view', 'name'=>'itemView']) }}
{{ Form::hidden('id', $item->id ) }}
<li>{{ $item->name }}</li>
{{ Form::close() }}
#endforeach
However, as soon as the are >1 items I get a 'itemView.submt is not a function' error. I susoect this is because there are now duplicate form names.
QUESTION: How do I use the array index to make the form names unique?
I have tried this with no luck.
#foreach($items as $item)
{{ Form::open(['url'=>'item/view', 'name'=>'itemView[]']) }}
{{ Form::hidden('id', $item->id ) }}
<li>{{ $item->name }}</li>
{{ Form::close() }}
#endforeach
I am sure it's possible, not sure how.
Thanks!
You should use function with onclick, for example:
#foreach($items as $key => $item)
{{ Form::open(['url'=>'item/view', 'name'=>'itemView[]']) }}
{{ Form::hidden('id', $item->id ) }}
<li>{{ $item->name }}</li>
{{ Form::close() }}
#endforeach
But a better approach would be keeping all JS in JS files and to not mix PHP and JS as I'm showing in my repo:
{{ $item->name }}</li>
And JS could look like this:
$('.js_link').on('click', function(e) {
$(this).closest('form').submit();
});

How to remove comma from last array in Laravel 5?

I have this view :
<p style="text-align: center;"><strong>
#foreach($journal->user as $item)
{{ $item->name }},
#endforeach
</strong></p>
I wanted to remove comma after the last {{ $item->name }} string. How to do it in Laravel 5.3 blade ?
If you're using 5.3, you can use $loop variable for this:
#foreach($journal->user as $item)
{{ $loop->first ? '' : ', ' }}
{{ $item->name }}
#endforeach
The code is from similar question.
Try this
#foreach($journal->user as $item)
{{ $item->name }}
#if (!$loop->last)
,
#endif
#endforeach

Why do i have a form url point at thesame location but not at a specific file that will insert my data into database

My LoginUser.blade.php
<h1>Login</h1>
{{-- Form start comment --}}
{{ Form::open(array('url' => 'user/loginUser')) }}
<p>
{{ Form::label('email', 'E-Mail Address') }}
{{ Form::email('email', null, array("placeholder" => "Enter your email")) }}
</p>
<p>
{{ Form::label('password', 'Password') }}
{{ Form::password('password', array("placeholder"=>"Enter your password")) }}
</p>
<p>
{{ Form::submit('Sign In') }}
</p>
{{ Form::close() }}
{{-- Form end comment --}}
So my Question is why should my URL points to my current LoginUser.blade.php which contains the HTML forms?? Please am new to laravel am using laravel 5.2

Resources