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
Related
I'm trying to get the relationships for a model from the controller so be able to display the relationship not as a id but as that id's name or type or whatever it might be. In this case I'm trying to get information relating to a question, what response type it has (text, multiple, rank, yes or no) and also which section is belongs to (the name)
This is my controller code so far
public function index()
{
return Inertia::render('Question/Index', [
'survey_question' => SurveyQuestion::all(),
'survey_section' => SurveySection::all(),
'response_type' => ResponseType::all()
]);
}
The table in the vue
<el-table
:data="tableData">
<el-table-column
prop="question"
label="Pregunta">
</el-table-column>
<el-table-column
label="Seccion">
<template slot-scope="scope">
<p> {{ scope.row.survey_section.title }} </p>
</template>
</el-table-column>
<el-table-column
label="Tipo de Respuesta">
<template slot-scope="scope">
<p> {{ scope.row.response_type.type }} </p>
</template>
</el-table-column>
<el-table-column
prop="optional"
label="Opcional">
</el-table-column>
<el-table-column>
<template slot-scope="scope">
<div class="btn-link-edit action-button" #click="edit(scope.row)">
<i class="fas fa-pencil-alt"></i>
</div>
<div class="btn-link-delete action-button" #click="delete(scope.row)">
<i class="fas fa-trash"></i>
</div>
</template>
</el-table-column>
</el-table>
This brings the relationships I think, because when I create a new question I have a select option and it does show the names instead of the ids, but when I'm trying to display said name in a table I can only access the id.
I also would like to know how to show for the optional field instead of 0 or 1, yes or no. This field is a boolean in the table structure if that is important.
If I do {{ scope.row }} then I get the information but only of the question like this
{ "id": 1, "question": "asdfasdf", "survey_section_id": 1, "response_type_id": 1, "optional": 1 }
What I would like is that when I do {{ scope.row }} I also get from those ids another array with the information related to that id both from the section and the response type.
You should use with method in the eloquent model. For example:
$result = Model::with(['nameOfTheRelationShip'])->get();
As Josias says, you should use with method.
The best practice in laravel is to return the eloquent data then call it in defineProps to use with vue/inertia
Lets say you have a model for devices that belongs to clients, and you want to index all the devices with the client.name.
In your devices controller you would return all the devices with the client relationship as follows
public function index()
{
$devices = Device::with(['client'])->get();
return Inertia::render('Devices/Index', compact(['devices']));
}
Then in the vue Devices/Index you just have to declare the devices in the definedProps like
defineProps(['devices']);
then you can make a simple v-for statement
<script setup>
import { Head } from '#inertiajs/inertia-vue3';
import { Inertia } from "#inertiajs/inertia";
defineProps(['devices']);
</script>
<template>
<div>
<table>
<thead>
<th>
Client
</th>
<th>
Name
</th>
<th>
Model
</th>
<th>
Year
</th>
</thead>
<tbody>
<tr v-for="device in devices" :key="index">
<td>
{{ device.client.name }}
</td>
<td>
{{ device.name }}
</td>
<td>
{{ device.model }}
</td>
<td>
{{ device.year }}
</td>
</tr>
</tbody>
</table>
</div>
</template>
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 }}">
The problem is, is that when I sign a new user up (in my application a 'charity owner'), they are redirected to their dashboard.
On the dashboard, displays the all the charities that the user owns and any donations made to them in another table.
However, because the user has not added their charity to the database yet I am getting undefined variables.
#forelse($ownedCharities as $owned)
<td> {{ $owned->name }} </td>
<td> Not Available </td>
<td> <p id="desc"> {{ $owned->description }} </p> <p style="color: #000;" class="seeMore"> See more... </p> </td>
<a href="#"> <button class="btn btn-danger btnPopover"
data-toggle="popover" data-placement="top"> Delete </button> </a> </td>
<td> <button class="btn btn-warning"> Edit </button> </td>
</tr>
#empty
<p> No charities owned </p>
#endforelse
I am using Blade's #forelse
You can use php functions in blade like to see if they are or not defined or has some value
#isset($records)
// $records is defined and is not null
#endisset
#empty($records)
// $records is "empty"...
#endempty
You can use Null coalescing operator which is introduces in PHP 7
$records ?? ""
I want to detach some data from my database. I have a table on my view that shows some data with checkboxes. Every tag has it own checkbox. I want to pass all the tag_id's from the tags I've checked. To detach them I also need the domain_id. I've tried some things out but couldn't figure out how I can pass all the domain_id with the tag_id's I've marked.
<tbody>
{!! Form::open(['action' => 'DomainController#detach', 'method' => 'post']) !!}
#foreach($domains as $domain)
<tr>
<td>{{ $domain->name }}</td>
<td>{{ $domain->tld }}</td>
<td id="hello">
#foreach($domain->tags as $tag)
{{ $tag->name }},<br>
#endforeach
</td>
<td>
<input name="domain_id[]" type="hidden" value="{{ $domain->id }}">
#foreach($domain->tags as $tag)
<input type="checkbox" name="tag_id[]" value="{{$tag->id}}"><br>
#endforeach
</td>
</tr>
#endforeach
</tbody>
<div class="btn-group">
<button type="button" dropdown-toggle" data-toggle="dropdown" >Action<span class="caret"></span></button>
<ul class="dropdown-menu" role="menu">
<li><a>
<button>Send</button>
</a></li>
</ul>
</div>
{!! Form::close() !!}
Now I want to get every tag_id and the domain_id of the tags if I mark them with a tick.
As a output I need something like this:
[0] = "domain_id" : 5,
"tag_id" : [0] => 1,
[1] => 2,
[2] => 3,
[1] = "domain_id" : 6,
"tag_id" : [0] => 10,
[1] => 11,
[2] => 12,
currently.. this is what I get
{"domain_id":["5","6","10","13","15","18","19","22","23","24","28"],"tag_id":["273","286","285"]}
I've just marked tags from the domain with id 5, but it gave me all domain_id's and I couln't figure out why..
A form won't output values in the way you want as it cannot generate subarrays, so as mentioned in my comment, you will have to improvise. Your bottom line is you need to associate the domain_id with each tag_id in some way so you can database it, or whatever.
I suggest creating your tag checkbox like this in your view
<td>
#foreach($domain->tags as $tag)
<input type="checkbox" name="tag_id[]" value="{{$domain->id}}:{{$tag->id}}"><br>
#endforeach
</td>
when submitted, it should format the tag_id in the request object like this
[0] = "tag_id" : [0] => 5:1,
[1] => 5:2,
[2] => 5:3,
[3] => 6:10,
[4] => 6:11,
[5] => 6:12,
Now you can loop over the tag_id array and separate the domain_id and tag_id fields to do whatever you need to (your example didn't say what you wanted to to do with them)
foreach ($request->tag_id as $item) {
$arrItem = explode(':', $item);
// $arrItem[0] is your domain_id for the tag_id $arrItem[1]
// other logic here
}
first i use laravel blade for my input from, like this
{{ Form::open('practicums/'.$practicums->id, 'PUT') }}
<table align="center">
<tr>
<td>{{ Form::label('name', 'Practicum Name') }}</td>
<td width="75px"></td>
<td>{{ Form::text('name', $practicums->name) }}</td>
</tr>
</table>
i want to use parsley.js to validate the input, i add
data-validate="parsley"
as
{{ Form::open('practicums/'.$practicums->id, 'PUT', array('data-validate' => 'parsley')) }}
but when i add parsley.js parameters on input form (like: data-type,data-required, ets) it's error. then i use 'old' input form like
<input type="text" id="name" name="name" data-required="true"/>
it works.
How to use the parsley.js parameters in Laravel Blade?
Can i still use input form from Laravel Blade with parsley.js, or i should use the old method?
Thanks before.
maybe you should share what's the error it appear ?
i m using the parsley with laravel 4 without any problem, this is my sample code
{{ Form::text('name',Input::old('name'),array('id'=>'name','data-required'=>'true','data-required-message'=>'Name Required','placeholder'=>'Please enter name')) }}
You should do like this
{{ Form::open('practicums/'.$practicums->id, 'PUT', array('data' => 'parsley-validate')) }}
This might help you.
{!! Form::open(['route' => 'posts.store','data-parsley-validate'=>'']) !!}
{{Form :: label('title','Title:')}}
{{Form:: text('title',null,array('class'=>'form-control','required'=>'','max length'=>'255'))}}
{{Form :: label('slug','Slug:')}}
{{Form::text('slug',null,["class"=>'form-control','required'=>'','min length'=>'5','max length'=>'255'])}}
{{Form::label('body','Post Body:')}}
{{Form::textarea('body',null,array('class'=>'form-control','required'=>''))}}
{{Form::submit('Create Post',array('class'=>'btn btn-success btn-block','style'=>'margin-top:20px;'))}}
{!! Form::close() !!}