Laravel assigning the ID to a Zurb Foundation modal - laravel

I'm currently making a CMS, for a local charity (free of charge) and I'm using Laravel 5.6 along with Zurb foundation 6.4. It's my first time using Laravel, but I've really enjoyed it and I've almost finished.
However, I have a couple of little niggles, that I need to fix, before I deploy it and a little guidance would help, a lot. I'm stuck with assigning a dynamic ID to my modal, to update. My controller, route and model are fine. I can update the first record, from the table, with the modal and that works as expected.
The problem is, it is always the first record that is shown, irrespective of whether I click the trigger, on an unrelated row. I've tried to pass the category_id in, using Blade, but that doesn't work, I either get the same result or an error. I can see what the problem is, but I don't know how to fix it. I think I need some jQuery and/or Ajax and I have tried, to no avail (I'm rubbish with JS).
As I said, my MVC is sound, I can update the first row and all I need is to be able to dynamically assign the category_id to the data-open attribute. I'll post my code, below and thanks for taking time to read this.
#foreach ($categories as $category)
<tr>
<td>{{ $category->name }}</td>
<td>{{ $category->posts->count() }}</td>
<td align="center"><a data-open="editModal"><i class="fas fa-edit fa-lg" style="color: green"></i></a></td>
<td align="center">
<i class="fas fa-trash-alt fa-lg" style="color: red"></i>
</td>
</tr>
<div class="reveal" id="editModal" data-reveal>
<h3>Edit: {{ $category->name }}</h3>
<form action="{{ route('category.update', ['id' => $category->id]) }}" method="post">
{{ csrf_field() }}
<label for="name">Category Name</label>
<input type="text" name="name" value="{{ $category->name }}">
<button class="button expanded success fullRadiusButton" type="submit">Update Category</button>
<i class="fas fa-window-close"></i>
</form>
</div>
#endforeach

An element is unique in the document based on the id property but you're using the same id (editModal) multiple times. You're instructing the Reveal Modal library to open the modal with an id of editModal so it finds the first one (which it expects to be the only one) and opens it. An id should only ever appear once in a document, or you'll experience unexpected behaviour like this.
id - unique to a single element
class - not unique to an element
You can fix this by giving each edit modal a unique id, something like this:
<div class="reveal" id="editModal_{{ $category->id }}" data-reveal>
Then in your trigger reference that unique id:
<a data-open="editModal_{{ $category->id }}">

Related

Why does a link with route set to posts.destroy in practice routes to posts.show

#extends('layouts.app')
#section('content')
<ul>
#foreach ($posts as $post)
<li>
<a href="{{route('posts.show', $post->id)}}">
{{$post->title}}
</a>
 
<a href="{{route('posts.edit', $post->id)}}">
Edit
</a>
 
<a href="{{route('posts.destroy', $post->id)}}">
Delete
</a>
#endforeach
</ul>
#endsection
I am new to laravel and in the process of learning. In the code above i have made a simple Unordered list of posts that is in the database. Next to each post is a edit and delete links. The edit link works just fine and invokes edit method in posts controller. But the delete link doesn't work. I think it runs the show method instead of destroy method in posts controller. Why is this the case?
To be more clear i have already somewhat solved the problem with the code bellow inside the foreach directive
<form action="/posts/{{$post->id}}" method="post">
#csrf
<input type="hidden" name="_method" value=" DELETE">
<input type="submit" name="delete" value="Delete" id="">
</form>
But why do this in the first place. I understand when we use form since a html form doesn't support DELETE method i had to add the hidden input. But why doesn't the hyper link tag work. This is the list of routes:
Links always perform GET method by default.
You can mimic DELETE method behavior using some javascript.

Laravel | More than one CubePortfolio gallery in blade's view

how can I add more CubePortfolio gallery in my view. My code:
#foreach($page->subpages as $subpage)
<div id="js-grid-lightbox-gallery" class="cbp ">
#foreach($subpage->photos as $photo)
<div class="cbp-item {{ $subpage->id }}">
<a href="/project/storage/app/{{ $photo->filename }}" class="cbp-caption cbp-lightbox" data-title="" rel="nofollow">
<div class="cbp-caption-defaultWrap">
<img src="/project/storage/app/{{ $photo->filename }}" alt="">
</div>
</a>
</div>
#endforeach
</div>
#endforeach
currently only shows one gallery to me, and should display multiple galleries. One is ok, the other revolves around the loader.
I understand that there cannot be two galleries with the same ID? How can I solve this problem? Thanks for the help.
Append an identifier from the loop to the ID, the same way you do that with the class
<div id="js-grid-lightbox-gallery-{{ $subpage->id }}" class="cbp">
Then initialize the element like so (example from jQuery)
$('[id^=js-grid-lightbox-gallery]').cubeportfolio({
Hope this helps

Laravel - Trying to get property 'name' of non-object

Im making a view that shows the users and the companies they work for.
In my User model i return the view like this:
return view('users.index', ['users' => $model->all()]);
The model $model->all() is my User model.
Then in my index.blade file i loop through the data like this:
#foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>
{{ $user->email }}
</td>
<td>{{ $user->created_at->format('d/m/Y H:i') }}</td>
<td>{{ $user->company->name }}</td>
<td class="text-right">
<div class="dropdown">
<a class="btn btn-sm btn-icon-only text-light" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-ellipsis-v"></i>
</a>
<div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">
#if ($user->id != auth()->id())
<form action="{{ route('user.destroy', $user) }}" method="post">
#csrf
#method('delete')
<a class="dropdown-item" href="{{ route('user.edit', $user) }}">{{ __('Edit') }}</a>
<button type="button" class="dropdown-item" onclick="confirm('{{ __("Are you sure you want to delete this user?") }}') ? this.parentElement.submit() : ''">
{{ __('Delete') }}
</button>
</form>
#else
<a class="dropdown-item" href="{{ route('profile.edit') }}">{{ __('Edit') }}</a>
#endif
</div>
</div>
</td>
</tr>
#endforeach
But when i try to show $user->company->name i get the following error:
Trying to get property 'name' of non-object
I tried debugging by using dd($user->company) and it showed all the company information of the company connected to the user. Then i tried dd($user->company->name) and it showed the company name.
I dont understand why i cant just show $user-company->name without getting the error but when i dd($user->company->name) it shows the company name.
Can someone point me in the right direction.
It probably shows an error because one of your users doesn't have a company. The first one of your loop has one, that's why when you dd($user->company), you see a company. But it doesn't mean the second or third has one. You can do a if statement in order to catch any null company. Something like
if($user->company == null){
dd($user);
}
Alternatively, you can just do dd($users->pluck('company','id'));, which will show you a collection with the user id as key and related company (or null) as value.
You can use optional() helper to solve this. There are users who don't have companies. SO you can display them like this optional($user->company)->name and you will not get an error if a user does not have a company
If you are trying to get relationship object value might be chances are there you will get this type of error. Due to there is some user who doesn't have company detail.
Just try below code.
{{ #$user->company->name }}

Laravel wizard system getting data from db

I am creating wizard systeem i create taks and wizard. but i want to show the take as stap not all the taks in one stap.
<div class="slide" id="slide-2" style="display: none">
#foreach($tasks as $task)
<div class="form-group">
<label for="task[{{ $loop->index }}]">{{ $task->name }</label>
<input type="checkbox" name="task[]" id="task[{{ $loop->index }}]" value="{{ $task->id }}">
</div>
#endforeach
</div>
But now I get all task in one time, how can I get one by one?
$tasks is an array and you can get array value one by one by key or index
for example:
$tasks[0]->name display first index of $tasks name or $tasks[0]->id
and $tasks[1]->name to get second and ...
of course there are many ways to get values , it depends on how do you want to use it
for loop or switch or other loops with conditions

pass multiple id's from checkboxes to my controller with Laravel

I have a products table in my database. I'm getting the name of the products from my DB and print theire names in a bootstrap table on my blade. Currently there is a edit button on every row line. If I'm pressing the button I'm passing the ID of this product to my controller edit function. Now I want to add checkboxes for every product in this table and if I set for example two ticks ( so two different products ) I want to pass both ID's at the same time to my controller.
Looks like this:
{!! Form::open(['action' => 'ProductController#tags']) !!}
#foreach($products as $product)
<tr>
<td>
{!! Form::checkbox('check', $product->id) !!}
</td>
<td>
{{ $product->name }}
</td>
<td>
#foreach($product->tags as $tag)
{{$tag->name}},
#endforeach
</td>
<td>
{{ $product->created_at->format('d.m.y') }}
</td>
<td>
<a href="{{ action('ProductController#edit', ['id' => $product->id]) }}">
<button class="btn btn-default" style="float: right;">Edit</button>
</a>
</td>
</tr>
#endforeach
{!! Form::close() !!}
As you see, I've set the product id as a seccond parameter for every checkbox, but this haven't worked well of course. Can someone tell me a way how I can give every checkbox the ID of the product and pass their ID's to my controller at the same time, if they are marked with a tick ?
Thanks for taking the time :)
Have you tried accepting multiple checkbox values as array by adding '[]' to the checkbox name?
{!! Form::checkbox('check[]', $product->id) !!}
I believe that this Form facade was from Laravel 4, i never used it, but i think that this will generate an input, with checkbox type, a name of 'check' and the id as the value, right?
Did you tried to set the name to 'check[]'? This way all checked ids will be in one array 'check' in your request. In Laravel 5 at least, you can try to dump the request['check'] and this should return an array os checked ids

Resources