handling empty table when displaying view of data - laravel

I have a laravel project. it has a table that I want to fill with data from a form. Currently it populates the form with any data that may exist in the table.
I'm using #foreach to iterate through the data object. This works fine when there is data in the table, but when there's no data obviously the #foreach data object will be empty.. How do folk handle this so that the form still shows with default values?
#foreach($pricing as $price)
<div class="form-row align-items-center">
<div class="col-12">
<p><strong>Rate</strong></p>
</div>
<div class="col-12">
<div class="row">
<div class="col-md-2">
<label for="hirePrice" class="">Hire Price</label>
</div>
<div class="col-md-4">
<input type="text" name="hire" class="form-control" placeholder="1200" aria-label="hireprice" id="hirePrice" value="{{ $price->hire}}">
Currently, if I empty the table, the page just displays the header and none of the html above, including the form fields.
edit: The table will always only have 1 ROW !! not sure if this makes it any easier.

You can use
#isset($pricing)
#foreach($pricing as $price)
<div class="form-row align-items-center">
<div class="col-12">
<p><strong>Rate</strong></p>
.................................
//your code
#endforeach
#endisset
Or, You can use if else condition like this:
#if($pricing != Null)
#foreach($pricing as $price)
<div class="form-row align-items-center">
<div class="col-12">
<p><strong>Rate</strong></p>
.....................
//code
#endforeach
#else
<div>No data</div>
#endif

Though i can not get your actual scenario,i can give some advice.
You can add one line condition inside of input tag like this:
HTML:
<input type="text" #if($data->variable) value="{{ $data->variable }}" #else value=" " #endif >
Otherwise, You can put same code within foreach loop into else condition. So, when data exists in the table, it shows the data in the form otherwise same form with empty value will appear.

Related

Filtering page results Laravel many to many, many ids to find one entry

I have a page that displays all the records from the 'Receita' table like this:
public function mostrarTodos()
{
$receitas = Receita::all()->toArray();
$ingredientes = Ingrediente::all()->toArray();
return view('home', compact('receitas', 'ingredientes'));
}
on the top of the page there is a form with many checkboxes that will filter the results displayed on the front page:
<div class="col-span-2 border p-2">
<form action="filtro" class="grid grid-cols-4" method="GET">
#foreach ($ingredientes as $ingrediente)
<div>
<label for="{{ $ingrediente['id'] }}">{{ $ingrediente['nome'] }}</label>
<input type="checkbox" name="ingrediente[]" id="{{ $ingrediente['id'] }}" value="{{ $ingrediente['id'] }}">
</div>
#endforeach
<button class="rounded p-2 bg-purple-200 col-span-4" type="submit">Filtrar</button>
</form>
</div>
The question is, how can I use that form to filter the results that are displayed on the page since each entry can have one or many of those checkboxes? In other words, show me only the entries that have the checked checkboxes.

How insert checkbox to database mysql - laravel

Hey anyone can you tell me the mistake??
this checkbox i show it from my table genre
<div class="row form-group">
<div class="col col-md-3"><label class=" form-control-label">Genre Film</label>
</div>
<div class="col col-md-9">
#foreach($genre as $gr)
<div class="form-check">
<div class="checkbox">
<label for="checkbox1" class="form-check-label ">
<input type="checkbox" name="genre[]" value="{{ $gr->nama_genre }}"> {{ $gr->nama_genre }}
</label>
</div>
</div>
#endforeach
</div>
and then this is the form
enter image description here
controller
public function tambah_movlist(Request $request)
{
$movielist = new Movielist();
$movielist->poster = $poster;
$movielist->judul =$request->judul;
$movielist->tahun =$request->tahun;
$movielist->genre[] = ($request->genre);
$movielist->rating =$request->rating;
$movielist->biaya_produksi =$request->b_produk;
$movielist->pendapatan = $request->pendapatan;
$movielist->sinopsis =$request->sinopsis;
$movielist->save();
return redirect('/adminmovielist');
}
In the HTML, you defined genre value as an array so you can directly get genre value through $request->genre and can save it in the database. Actually no need array_keys().
Please change code:
$movielist->genre[] = ($request->genre);
to
$movielist->genre = $request->genre;
And check the data type of column genre in the database is it right

Flatpickr creates additional input field

I'm implementing a project in laravel. There is a view with some input fields. One of them should be viewed with flatpickr. Although default options are used, flatpickr adds always an additional input field. I want to use my defined input field with flatpickr.
Generated code:
I tried already to override the default option with altInput: false but this doesn't work at all.
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-header">
<h3>Buchung {{ isset($booking) ? 'ändern' : 'anlegen' }}</h3>
</div>
<div class="card-body">
<form method="post" action="{{ isset($booking) ? route('bookings.update', $booking->id) : route('bookings.store') }}">
#csrf
#if (isset($booking))
#method('PUT')
#endif
<div class="form-group">
<label for="project">Projekt</label>
<select name="project" class="form-control">
#foreach ($projects as $project)
<option value="{{ $project->id }}">{{ $project->name }}</option>
#endforeach
</select>
</div>
<div class="form-row">
<div class="form-group col-md-3">
<label for="date">Datum</label>
<input type="text" id="date" name="date" class="form-control">
</div>
...
#endsection
#section('scripts')
<script>
flatpickr('#date');
</script>
#endsection
I had a similar problem, flatpickr was creating a new input field - hidden, it did not create a new input field visible in the browser - because altInput: was set to true. I am not sure if you can set it to false without compromising what you want to achieve, but maybe this leads you in the right direction to fix it?

Dropdown menu from any table in database - Laravel

I have a form, which has some text field.
Let me explain this field:
So_number from table sales_order
so_date from table sales_order
customer_name from table customers
and then I've got linked all of model and controller and views
My controller is linked of my 3 models.
and I try to call with {{}} this, as same as like variable as I made,
I call the details with this
<td>{{$so->so_number}}</td>
this is my web.php of details
Route::get('/so/{so_number}/details', 'So_Controller#details');
this is my So_Controller#details:
public function details($so_number)
{
$data_so = \App\Sales_Order::all();
$data_so_detail = \App\Sales_Order_Details::all();
$data_customer = \App\Customer_Model::all();
return view('salesorder.details', ['data_so_detail' => $data_so_detail, 'data_so' => $data_so, 'data_customer' => $data_customer]);
}
This is my views of my details.index.php
<div class="modal-body">
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<input type="text" name="so_number" value="{{$data_so->so_number}}" class="form-text" required>
<span class="bar"></span>
<label for="so_number">Sales Order Number</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<input type="text" name="so_date" value="{{$data_so->so_date}}" class="form-text" required>
<span class="bar"></span>
<label for="so_date">Sales Order Date.</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<label for="customer_name">Customer Name</label>
<select name="customer_name" id="customer_name">
#foreach ($data_customer as $customerName)
<option value="{{$customerName->customer_name}}"></option>
#endforeach
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<label for="customer_id"></label>
<select name="customer_id" id="customer_id">
#foreach ($data_customer as $customerId)
<option value="{{$customerId->customer_id}}"></option>
#endforeach
</select>
</div>
</div>
And the error is
ErrorException (E_ERROR)
Property [so_number] does not exist on this collection instance. (View: /Applications/XAMPP/xamppfiles/htdocs/belajar_laravel/resources/views/salesorder/details.blade.php)
Previous exceptions
Property [so_number] does not exist on this collection instance. (0)
My expected output is list of items in dropdown.
Please help me.
In your details.blade.php, you have written
{{$data_so->so_number}}
whereas in controller, you have written
$data_so = \App\Sales_Order::all();
all() returns a collection which is like a 2 dimensional array. Either you have to iterate through $data_so to access 'so_number' or in controller you have to write
$data_so = \App\Sales_Order::first(); //this is an example, first() or find() will return an instance of the model and you can directly access it like $data_so->so_number.
In another case, check your method details($so_number).
In case you want to retrieve from \App\Sales_Order a record for $so_number, you may use find().
You may read https://laravel.com/docs/5.8/eloquent-relationships to connect multiple models.

view new div based on option value of select tag using laravel

i have a dropdown list when i select any one option from list i should display an another form with name of the teacher and subject of the teacher in textbox.The form is not displaying.when i select one item it should display the selected name and description box to add subjects of teachers that they interested please help me
view page
<div class="portlet light bordered row">
<h1>Add teacher subject</h1>
<div class="row">
<form action = "{{ url('subjectby/adding/process') }}" method = "POST">
{{ csrf_field() }}
<div class= "col-md-12">
<div class="form-group">
<h3>Choose Teachers </h3>
<div class="input-group">
<div class="ui-widget border-dropdown" style="margin-left:50px;">
<select id="teacher" name="teachers" placeholder="Select Teacher">
<option value="">Select Teacher</option>
#foreach($user as $tec)
<option value="{{$tec->id}}">{{$tec->name}}</option>
#endforeach
</select>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="new">
<div class="form-group">
<div class="col-sm-6">
<label for="inputFacebook">Teacher Name</label>
<input type=hidden value="{{$tec->id}}" name="id">
<input type="text" value="{{$tec->name}}" class="form-control" name="name">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<label for="inputDescription">Subject Interested</label>
<textarea class="form-control" rows="8" name="intr" placeholder="Enter Subject Interest"> </textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="submit" value="add" name=b1>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
This isn't a Laravel specific problem. You are taking about DOM manipulation and so you best bet is to use jQuery or Vuejs or even vanilla Javascript if you so choose. Basically, you will write a script to watch for when your <select> changes, and then based on it's new value, show the part of the form you want.
Keep in mind that Laravel is a backend framework and so the user will never interact with it on a page. After a page loads, Laravel has done all it can do until another request is made. This is why Javascript was born - to let the user change things without reloading the page.

Resources