Printing tasks per task list with Laravel 4 Eloquent - laravel

I'm trying to display several task lists in one page with their respective tasks. In my controller I'm using the following:
public function index()
{
$tasks_lists = Task_List::all();
$tasks = Task_List::find(1)->tasks;
return View::make('tasks.index', array(
'tasks' => $tasks,
'tasks_lists' => $tasks_lists
));
}
Using
$tasks = Task_List::find(1)->tasks;
Will return for all tasks lists the tasks appointed to task list 1. So I'm guessing here is my problem, what type of clause should I be using to achieve my goal?
My foreach in my view:
#foreach ($tasks_lists as $task_list)
<h2>{{ $task_list->title }}</h2>
<ul>
#foreach ($tasks as $task)
<li>
{{ Form::open() }}
<input type="checkbox" name="task" value="{{ $task->id }}" />{{ $task->name }}
{{ Form::close() }}
</li>
#endforeach
</ul>
#endforeach

Instead of fetching the tasks for one of the Task_List models (in your case the one with id 1) you should get them for every task_list.
Do this in your view:
#foreach ($tasks_lists as $task_list)
<h2>{{ $task_list->title }}</h2>
<ul>
#foreach ($task_list->tasks as $task)
{{-- etc --}
#endforeach
</ul>
#endforeach
Attention: if you leave it that way it will make a query on the db every time you call ->tasks. You need to use eager loading. This is done by adding with('relationship-name')
$tasks_lists = Task_List::with('tasks')->get();
return View::make('tasks.index', array(
'tasks_lists' => $tasks_lists
));

You don't need to look for the tasks in the controller, so that can be simply removed:
public function index()
{
$tasks_lists = Task_List::all();
$tasks = Task_List::find(1)->tasks;
return View::make('tasks.index', array(
'tasks' => $tasks,
'tasks_lists' => $tasks_lists
));
}
Then, you'll need to get the tasks for each task list in the view, like this:
#foreach ($tasks_lists as $task_list)
<h2>{{ $task_list->title }}</h2>
<ul>
#foreach ($task_list->tasks as $task)
<li>
{{ Form::open() }}
<input type="checkbox" name="task" value="{{ $task->id }}" />{{ $task->name }}
{{ Form::close() }}
</li>
#endforeach
</ul>
#endforeach
And that should be enough. :)

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

Laravel + Vue, How to do ->where() in vue-for?

I'm listing projects and those projects have statuses. First I'm listing the statuses and inside them projects, but this returns all the projects no matter what the status.
With Laravel I could just:
foreach ($projects->where($project->status_id == $status->id) as $project)
but how to do that with Vue?
<div v-for="status in statuses">
{{ status.title }}
<div v-for="project in projects">
{{ project.title }}
</div>
</div>
I also tried
<div v-for="status in statuses">
{{ status.title }}
<div v-for="project in filteredprojects">
{{ project.title }}
</div>
</div>
computed:{
filteredProjects() {
return this.projects.filter(function(project) {
return project.status_id == status.id;
})
}
},
But no luck
Try like this:
<div v-for="status in statuses">
{{ status.title }}
<div v-for="project in projects">
<div v-if="status.id === project.status_id">
{{ project.title }}
</div>
</div>
</div>
If you have set up a relationship between status and projects (as you should since you're using Laravel), you can load the relationship before it ever gets to the Vue component.
Controller
$statuses = Status::where(...your conditions...)->with('projects')->get();
// EDIT: or get all statuses without conditions
$statuses = Status::with('projects')->get();
// EDIT 2: get all statuses with conditional projects
$statuses = Status::with(['projects' => function($query) {
// add your conditions for projects
$query->where('completed', 0);
}])->get();
Vue
<div v-for="status in statuses">
{{ status.title }}
<div v-for="project in status.projects">
{{ project.title }}
</div>
</div>

Laravel checkbox $request issue

I am trying to pass 2 array of checkboxes, to my controller: roles and permissions to insert and/or patch in DB.
in my controller I am passing the collections to display on blade:
$roles = Role::all();
$permissions = Permission::all();
and all works fine as it displays the name and the checkbox.
Yet 2 issues when submitting:
$request is showing a simple array for each without the name of the role/permission.
I do not get an "off" for non checked.
http://prntscr.com/jq61p6
I am expecting to insert/patch roles and permissions for a specific $user->id.
I am assuming the problem is in blade and in my checkbox inputs:
<form method="POST" action="{{ route('roles-permissions',['id' =>$user->id]) }}">
{{ csrf_field() }}
<h5>User Roles</h5>
<div class="col-xs-12">
#foreach ($roles as $role)
{{ Form::checkbox('roles[]', null, true, ['class' => 'name'] ) }}
{{ Form::label($role->name, ucfirst($role->name)) }}<br>
#endforeach
</div>
<div class="break-20"></div>
<h5>User Permissions</h5>
<div class="col-xs-12">
#foreach ($permissions as $permission)
{{ Form::checkbox('permissions[]', null , false, ['class' => 'name']) }}
{{ Form::label($permission->name, ucfirst($permission->name)) }}<br>
#endforeach
</div>
{!! Form::submit('Save Roles and Permissions', ['class=', '"btn btn-primary width-250 mt-20"']); !!}
</form>
How can I fix this so I can see the role and permission name so I can pick up the right permissions/roles by my controller and save them into my roles/user and permissions/user tables?
Thanks.
3rd argument will decide whether it will be checked or not. Use null instead of true & let's check whether it works or not.

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();
});

use nested foreach for showing posts and comments in 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.

Resources