Laravel Livewire : livewire multiple root elements detected - laravel

I'm currently developing a website using laravel livewire
The website has a multi-site form page, and there's a dynamic dropdown on the inside of that multi-site form page
The problem is when the form is filled, it won't store the data into the database
When i do inspect the website there's an error that read :livewire multiple root elements detected
How do i fix this??
Livewire model :
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Kredit;
use App\Models\Biaya;
use App\Models\Produk;
use App\Models\Promo;
use App\Models\Motorcycle;
use App\Models\MotorcycleBrand;
use App\Models\Domisili;
class KreditMulti extends Component
{
public $brand_id;
public function render()
{
$domisilis = Domisili::all();
// $motorcycles = Motorcycle::all();
// $motorcycle_brands = MotorcycleBrand::all();
//for the dynamic dropdown
if($this->brand_id){
$motorcycle_brands = MotorcycleBrand::where('motorcycle_id', $this->brand_id)->get();
} else {
$motorcycle_brands = [];
}
return view('livewire.kredit-multi',
['domisilis'=>$domisilis])
->withMotorcycles(Motorcycle::all())
->with('motorcycle_brands', $motorcycle_brands);
}
}
The livewire blade php :
<div class="form-group row">
<label for="motorcycle" class="col-md-4">Merek motor</label>
<div class="col-md-6">
<select wire:model="brand_id" class="form-control">
<option value="" selected>Choose Motor</option>
#foreach ($motorcycles as $m)
<option value="{{$m->id}}">{{$m->motorcycle_name}}</option>
#endforeach
</select>
</div>
</div>
<br>
#if (count($motorcycle_brands) > 0)
<div class="form-group row">
<label for="motorcycle_brand" class="col-md-4 col-form-label text-md-right">Jenis Motor</label>
<div class="col-md-6">
<select class="form-control" name="motorcycle_brand_id">
<option value="" selected>Choose the motor version</option>
#foreach ($motorcycle_brands as $motor)
<option value="{{$motor->id}}" wire:key="motorcycle_brand{{$motor->id}}">{{$motor->motorcycle_brand_name}}</option>
#endforeach
</select>
</div>
</div>
#endif
<br>

all livewire components must be start with a <div> and end with a </div>.
actually you must wrap the whole <div> and </div> s into a single <div></div>

Your blade code has several roots
1.- <div class="form-group row"....>
2.- <br>
3.- #if (count($motorcycle_brands) > 0)...#endif
4.- <br>
Enclose all that stuff in a single root
<div>
<div class="form-group row"....>
<br>
#if (count($motorcycle_brands) > 0)...#endif
<br>
</div>

The error is as exact as it can be. A Livewire component simply can only have a single root element. Instead, you're adding 2, including 2 breaks.
Wrap the whole component into a single <div></div>, and then it should be fixed.

public $motorcycle_brands,$domisilis,$brand_id,$motorcycles;
public function render()
{
$this->domisilis = Domisili::all();
$this->motorcycles = Motorcycle::all();
if($this->brand_id){
$this->motorcycle_brands = MotorcycleBrand::where('motorcycle_id', $this->brand_id)->get();
} else {
$this->motorcycle_brands = [];
}
return view('livewire.kredit-multi');
}
Blade
#if (count($motorcycle_brands) > 0)
<div class="form-group row">
<label for="motorcycle_brand" class="col-md-4 col-form-label text-md-right">Jenis Motor</label>
<div class="col-md-6">
<select class="form-control" wire:model="brand_id">
<option value="" selected>Choose the motor version</option>
#foreach ($motorcycle_brands as $motor)
<option value="{{$motor->id}}">{{$motor->motorcycle_brand_name}}</option>
#endforeach
</select>
</div>
</div>
#endif

Related

my livewire app is returning 404 not found when I selected a dropdown item

I am designing a laravel app, which I used module and livewire. I have a dropdown item which when I selected an item its returned an error 404 NOT FOUND
Here is my livewire component.
I am thinking may the wire:model="selectedSession" and wire:model="selectedTerm" is having problem
please i need your help
<?php
namespace Modules\Student\Http\Livewire;
use Livewire\Component;
use Modules\Student\Entities\Result;
use Modules\Student\Entities\Term;
use Modules\Student\Entities\Section;
class ResultDependance extends Component
{
public $selectedSession = null;
public $selectedTerm = null;
public $results;
public function mount($id)
{
$this->results = Result::with('student', 'section', 'term', 'subject')->where('student_id', $id)->get();
}
public function render()
{
return view('student::livewire.pages.resultdependance',
['terms' => Term::all()],
['sessions' => Section::all()]
);
}
}
my blade.php codes
<div class="section">
<div class="card">
<div class="card-body">
<div class="card-title">
<h4><strong>Result</strong></h4>
<div class="form">
<form action="" method="post">
{{csrf_field()}}
<div class="form-group">
<select name="session" id="" class="form-control form-select" wire:model="selectedSession">
<option selected>Select Session</option>
#foreach($sessions as $session)
<option value="{{$session->id}}">{{$session->section_title}}</option>
#endforeach
</select>
</div>
<br>
<div class="form-group">
<select name="term" id="" class="form-control form-select" wire:model="selectedTerm">
<option selected>Select Term</option>
#foreach($terms as $term)
<option value="{{$term->id}}">{{$term->term}}</option>
#endforeach
</select>
</div>
<input type="button" value="test" wire::loading.attr='disabled'>
<h1 wire:loading>please wait</h1>
</form>
<br>
</div>
</div>
</div>
</div>
</div>
I am designing a laravel app, which I used module and livewire. I have a dropdown item which when I selected an item its returned an error 404 NOT FOUND
Here is my livewire component.
I am thinking may the wire:model="selectedSession" and wire:model="selectedTerm" is having problem
please i need your help
It's missing the logic to filter the results based on the selected session and term.
you can do it by using livewire hooks or by adding this
public function filterResults()
{
$this->results = Result::with('student', 'section', 'term', 'subject')->where('student_id', $id)->where('section_id', $this->selectedSession)->where('term_id', $this->selectedTerm)->get();
}
and in your selectedSession
<select name="session" id="" class="form-control form-select" wire:model.lazy="selectedSession" wire:change="filterResults">
Now when the user changes the selected session or term, the component will filter the results and update the view accordingly.

Livewire - Dependant Dropdown select

I have created a dependant dropdown with livewire like this:
class CreateAppointment extends Component
{
public $doctorCategories;
public $doctors;
public $selectedDoctorCategory = NULL;
public function mount(){
$this->doctorCategories = Doctor_Categories::all();
$this->doctors = collect();
}
public function updatedSelectedDoctorCategory($doctor_type_ID)
{
if(!is_null($doctor_type_ID)){
$this->doctors = Doctor_Categories::with('doctors')->find($doctor_type_ID);
}
}
}
And this is in my blade file:
<div class="form-group row">
<label for="doctor_categories" class="col-md-4 col-form-label text-md-right">Doctor Categories</label>
<div class="col-md-6">
<select wire:model="selectedDoctorCategory" class="form-control">
<option value="">Choose category</option>
#foreach($doctorCategories as $cat)
<option value="{{ $cat->id }}">{{ $cat->doctor_category }}</option>
#endforeach
</select>
</div>
</div>
#if (!is_null($selectedDoctorCategory))
<div class="form-group row">
<label for="doctor" class="col-md-4 col-form-label text-md-right">Doctor</label>
<div class="col-md-6">
<select class="form-control" name="doctor_id">
<option value="" selected>Choose Doctor</option>
#foreach($doctors->doctors as $doctor)
<option value="{{ $doctor->id }}">{{ $doctor->name }}</option>
#endforeach
</select>
</div>
</div>
#endif
</div>
It works well but when i select "Choose category" again it throws an error saying attempted to read property doctors on null.How can i avoid this?
The issue here is, that livewire does not apply the TrimStrings and ConvertEmptyStringsToNull middlewares on its requests. This is by design and is not a bug. It can be reintroduced (see the comments to the linked issue) but I advise caution doing so. With select it should work fine, but with input it can mess things up.
Instead of checking for is_null, check for empty and you should be fine.

I want to get data to the dropdown by using another dropdown in the same page

I have a dropdown on my page to load districts. And I have another dropdown to load Divisions according to that selected district. The below code I have got all the divisions. But I want only the divisions according to the selected districts. How can I do?
#php
$districtAll=\App\District::all();
#endphp
<div class="row">
<label class="col-md-4 control-label" for="district">9. District</label>
<div class="col-md-8">
<select name="district">
<option type="text" class="detail-wp" value="">Select District</option>
#foreach($districtAll as $all)
<option value="{{$all->DISTRICT_ID}}">{{$all->DISTRICT}} </option>
#endforeach
</select>
</div>
</div>
<br>
#php
$getDsAll=\App\DsDivision::all();
#endphp
<div class="row">
<label class="col-md-4 control-label" for="ds_division">10. DS Division</label>
<div class="col-md-8">
<select name="ds_division" class="form-control">
<option type="text" class="detail-wp" value=""></option>
#foreach($getDsAll as $all)
<option value="{{$all->DIVISION_ID}}">{{$all->DIVISION}} </option>
#endforeach
</select>
</div>
</div>
You can do it using ajax request:
$('select[name=district]').on('change', function(){
$districtId = $(this).val();
$.get('/division/district/'+$districtId).done(function(res){
$.each(res, function (key, value) {
$('select[name=ds_division]').append(`<option value='` + value.id + `'>` + value.name +`</option>`);
});
});
to make this working you will need a route like
Route::get("/division/district/{district}", "DivisionController#getByDistrictId");
DivisionController#getByDistrictIdwill be
public function getByDistrictId($district)
{
return Division::where('district_id',$district)->get();
}
this code just to give you an idea of how you can do 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.

Populate Form Based on dropdown value in Codeigniter

I have about 30 different forms that will be displayed in a wizard, a form will be displayed based on the value from the dropdown selected, if all forms are created in a view file it doesn't seem effective, I try to save each form in the database and it will Called based on the dropdown value but not as expected, please help with this problem.
this is the code example that I saved into the database:
<label>Yang Bertandatangan Dibawah ini:</label>
<div class="form-group row">
<label class="col-form-label col-lg-2">Nama</label>
<div class="col-lg-10">
<select name="pemberi" class="form-control select-search" required data-fouc>
<option value="">--pilih--</option>
<?php
if(is_array($pegawai)){
foreach($pegawai as $row){
?>
<option value="<?php echo $row->nip;?>"><?php echo $row->nama;?></option>
<?php
}
}
?>
</select>
</div>
</div>
<label>MEMERINTAHKAN:</label>
<div class="form-group row">
<label class="col-form-label col-lg-2">Kepada:</label>
</div>
<div class="form-group row">
<label class="col-form-label col-lg-2">Nama:</label>
<div class="col-lg-10">
<select name="penerima" class="form-control select-search" required data-fouc>
<option value="">--pilih--</option>
<?php
if(is_array($pegawai)){
foreach($pegawai as $row){
?>
<option value="<?=$row->nip;?>"><?=$row->nama;?></option>
<?php
}
}
?>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-lg-2">Jabatan:</label>
<div class="col-lg-10">
<select name="jabatan_penerima" class="form-control select-search" required data-fouc>
<option value="">--pilih--</option>
<?php
if(is_array($pembuat)){
foreach($pembuat as $row){
?>
<option value="<?=$row->id;?>"><?=$row->nama_jabatan;?></option>
<?php
}
}
?>
</select>
</div>
</div>
<label>Untuk:</label>
<div class="row">
<div class="col-md-9">
<div class="form-group">
<textarea rows="10" cols="4" class="form-control" placeholder="Isi Perintah" name="isisurat" id="isisurat" required></textarea>
</div>
</div>
</div>
The code runs well if it is directly written in source code, but different if called from the database
as expected if I write the code in page directly:
If I save the code in database and call based on value of dropdown,it will show error:
Why don't you use normal load views based on a condition like in your index you would do this:
VIEW ONE
VIEW TWO
// and so on ...
Then in your controller:
public function your_method($your_view)
{
$data['view'] = $this->load->view($your_view, null, TRUE);
// Setting third param as true will return the view as a string
$this->load->view('main_view', $data);
}
Now in your main view you have a variable called view of your requested view just echo it in your main content area.

Resources