Undefined variable: files (View: \resources\views\home.blade.php) - laravel

I am trying to get some data (all filenames) from files table in dropdown but it gives error: Undefined variable: files
Controller:
public function show($id)
{
$data = File::findOrFail($id);
$files = \DB::table('files')->get();
return view('userhome', compact('data', 'files'));
}
Blade template:
<div class="form-group">
<label class="text-left">Select Record</label>
<select name="parent_id">
<option value="">Select Record</option>
#foreach ($files as $filename)
<option value="{{ $filename->id }}">{{ $files->filename }}</option>
#endforeach
</select>
</div>
web.php: https://ibb.co/xMswcTG
What causes this error?

you return userhome.blade.php into your controller but try to get $files and $data variables into home.blade.php
change
return view('userhome', compact('data', 'files'));
to
return view('home', compact('data', 'files'));
all must be working ;)

You need to use "$filename->" object instead of "$files->" object in options tag.
Please update the blade with below code.
<div class="form-group">
<label class="text-left">Select Record</label>
<select name="parent_id">
<option value="">Select Record</option>
#foreach ($files as $filename)
<option value="{{ $filename->id }}">{{ $filename->filename }}</option>
#endforeach
</select>
</div>

Related

Getting name and id value from select Laravel livewire

I need a little help. I want to save also $item->name from the option to the Category database in the name column. Thanks for the help.
My blade code:
<label>Select a Category</label>
<select class="select2 form-control" for="cateId" wire:model.lazy="cateId">
<option value="">Select a Category</option>
#foreach ($categorylist as $item)
<option value="{{ $item->id }}">{{ $item->name }}</option>
#endforeach
</select>
my livewire controller:
public $name;
public function submit()
{
Category::create([
'name' => $this->$item->name,
]);
}
my Category model:
protected $table = "category";
protected $fillable = [
'name',
];
Just change option value like this:
<label>Select a Category</label>
<select class="select2 form-control" for="cateId" wire:model.lazy="catName">
<option value="">Select a Category</option>
#foreach ($categorylist as $item)
<option value="{{ $item->name }}">{{ $item->name }}</option>
#endforeach
</select>
and get it from $request->input('catName')

how to insert multi value data to database in laravel?

I want to store multiple data to database because i have 2 select option with same name. i've following some tutorial from google and youtube but i still can't figured out how to do it.
Table structure:
id | nama_mapel | guru_id | kode_mapel | ki_kd_id |
I have view like this, there's 2 select option with same name:
<input type="text" name="nama_mapel[]">
<input type="text" name="kode_mapel[]">
<select type="text" name="guru_id[]">
<option value disable>Pilih Guru</option>
#foreach ($guru as $item)
<option value="{{ $item->id }}">{{ $item->nama_guru }}</option>
#endforeach
</select>
<select name="ki_kd_id[]">
<option value disable>Pilih Kompetensi Dasar</option>
#foreach ($komp_dasar as $kd)
<option value="{{ $kd->id }}">{{ $kd->kompetensi }}</option>
#endforeach
</select>
<select type="text" name="ki_kd_id[]">
<option value disable>Pilih Kompetensi Inti</option>
#foreach ($komp_inti as $ki)
<option value="{{ $ki->id }}">{{ $ki->kompetensi }}</option>
#endforeach
</select>
and this is my controller:
public function store(Request $request)
{
$nama_mapel = $request->nama_mapel;
$guru_id = $request->guru_id;
$kode_mapel = $request->kode_mapel;
$ki_kd_id = $request->ki_kd_id;
foreach ($nama_mapel as $row => $key){
$data= [
'nama_mapel' => $nama_mapel[$row],
'guru_id' => $guru_id[$row],
'kode_mapel' => $kode_mapel[$row],
'ki_kd_id' => $ki_kd_id[$row],
];
DB::table('mapel')->insert($data);
}
return redirect()->back();
}
Any help would be very appreciated, and sorry for my bad english.
I have found out how to do this.
For view it like this, only use array [] in ki_kd_id name. The reason is because the others is same. I have duplication for all fields except ki_kd_id.
<input type="text" name="nama_mapel">
<input type="text" name="kode_mapel">
<select type="text" name="guru_id">
<option value disable>Pilih Guru</option>
#foreach ($guru as $item)
<option value="{{ $item->id }}">{{ $item->nama_guru }}</option>
#endforeach
</select>
<select name="ki_kd_id[]">
<option value disable>Pilih Kompetensi Dasar</option>
#foreach ($komp_dasar as $kd)
<option value="{{ $kd->id }}">{{ $kd->kompetensi }}</option>
#endforeach
</select>
<select type="text" name="ki_kd_id[]">
<option value disable>Pilih Kompetensi Inti</option>
#foreach ($komp_inti as $ki)
<option value="{{ $ki->id }}">{{ $ki->kompetensi }}</option>
#endforeach
</select>
and for the controller, it will be like this:
public function store(Request $request)
{
foreach ($request->ki_kd_id as $value){
if ($value) {
DB::table('mapel')->insert([
'nama_mapel' => $request->nama_mapel,
'guru_id' => $request->guru_id,
'kode_mapel' => $request->kode_mapel,
'ki_kd_id' => $value,
]);
}
}
return redirect()->back();
}
Use foreach to loop the request based on how many ki_kd_id that the request have. and then for the value just use $value as representative of ki_kd_id

How to use select2 multiselect with livewire

What I am trying to achieve is, there are two models A and B with a relationship of One to many, where A has many B. So if the records for A and B have already been created, A can be assigned to many B. So I started this current project with livewire, and now I am at a loss. Normal select with multiple works just fine and populates the array in the backend of livewire. But when I use select2 nothing is stored in the array, and when I dd it, it shows an empty array.
Even though livewire provides wonderful set of tools, I am starting to realize that there is a lot of gray areas where things are missing or lacks external support, like select2 here.
Here is what I have done so far:
<div>
<div class="tab-pane fade show active" id="card1" role="tabpanel" aria-labelledby="card1-tab">
<div class="pt-3"></div>
<div class="row" >
<div class="form-group col">
<label for="cards">Enter Cards</label>
<select class="form-control select2" id="cards" multiple="multiple" onchange="this.dispatchEvent(new InputEvent('input'))>
<option disabled="">Select Cards</option>
#foreach($cardsUnassigned as $card)
<option value="{{ $card->id }}">{{ $card->imei_number }}</option>
#endforeach
</select>
</div>
<div class="form-group col">
<label for="technicians" class="">Technicians</label>
<select class="form-control select2" id="technicians" wire:model='techies.'>
<option disabled="">Select Technicians</option>
#foreach($technicians as $tech)
<option value="{{ $tech->id }}">{{ $tech->first_name }}</option>
#endforeach
</select>
</div>
</div>
#foreach($cardSelect as $key => $value)
{{$value}}
#endforeach
<button wire:click="assignCardToTech()" class="btn btn-primary">Submit</button>
</div>
</div>
#push('scripts')
<script>
$('#cards').select2({
placeholder: 'Select Cards to assign'
});
//send data to livewire
$('#technicians').select2({
placeholder: 'Select Technicians'
});
</script>
#endpush
And the backend:
<?php
namespace App\Http\Livewire\Stock\Assigns;
use App\Models\Card;
use App\Models\Sim;
use App\Models\Technician;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Support\Facades\Auth;
class Assigns extends Component
{
public $sortBy_card = 'imei_number';
public $sortBy_sim = 'ip';
public $sortDirection = 'asc';
public $search = '';
public $perPage = 10;
public $cardSelect = [];
public $techies;
public function render()
{
$simsUnassigned = Sim::query()
->whereNull('technician_id')
->searchUnassigned($this->search)
->get();
$cardsUnassigned = Card::query()
->whereNull('technician_id')
->searchUnassignedCard($this->search)
->get();
// dd($cardsUnassigned);
$simsAssigned = Sim::query()
->searchAssigned($this->search)
->orderBy($this->sortBy_sim, $this->sortDirection)
->paginate($this->perPage);
$cardsAssigned = Card::query()
->searchAssignedCard($this->search)
->orderBy($this->sortBy_card, $this->sortDirection)
->paginate($this->perPage);
$technicians = Technician::query()->get();
return view('livewire.stock.assigns.assigns',compact('simsUnassigned','cardsUnassigned','simsAssigned','cardsAssigned','technicians'))
->extends('layouts.base')
->section('content');
}
public function assignCardToTech(){
dd($this->cardSelect);
if($this->cards){
foreach($this->cards as $card){
}
}
}
}
Hopefully this helps.
######## INSIDE LIVEWIRE COMPONENT
public array $locationUsers = [];
protected $listeners = ['locationUsersSelected'];
public function locationUsersSelected($locationUsersValues)
{
$this->locationUsers = $locationUsersValues;
}
######## INSIDE LIVEWIRE BLADE
<div class="col-md-12 mb-3" wire:ignore>
<label for="locationUsers">Select Users</label>
<select id="locationUsers" class="form-control select2" multiple="multiple">
<option value="">--select--</option>
#foreach($this->users as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
#endforeach
</select>
</div>
######## INSIDE LIVEWIRE SCRIPTS
document.addEventListener('livewire:load', function () {
$('#locationUsers').on('select2:close', (e) => {
#this.emit('locationUsersSelected', $('#locationUsers').select2('val'));
});
$('#locationUsers').val(#this.get('locationUsers')).trigger('change');
});
Your could try adding this to your html
<div class="form-group col-md-6" wire:ignore>
<label for="manager" class="mb-0 h5">Assign Managers:</label>
<select wire:model="reporting_managers" id="manager" class="select-2 multiple='multiple' data-placeholder="Assign Managers">
#foreach ($managers as $manager)
<option value="{{ $manager->id }}" data-name="{{ $manager->name }}">{{ $manager->name }}</option>
#endforeach
</select>
</div>
and then in your javaScript section
$('#manager').on('select2:select', function (e) {
#this.set('reporting_managers', $('#manager').select2('val'));
});
$('#manager').on('select2:unselect', function (e) {
#this.set('reporting_managers', $('#manager').select2('val'));
});
This will directly store your selection in variable in livewire and won't get deselected on render.

How do I search in Laravel using selects?

On my test site I have a search by cities and categories, which are selected using a select
<div class="form-group">
<select class="selectpicker" name="sCity">
#foreach($cities as $city)
<option value="{{$city->slug}}">{{$city->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select class="selectpicker" name="sCat">
#foreach($categories as $cat)
<option value="{{$cat->slug}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
And such a method for searching
public function search(Request $request)
{
$result = DB::table('ads')
->where('city_slug', $request->sCity)
->orWhere('category_slug', $request->sCat)
->get();
dd($result);
}
If you search for one select, then the search is correct, but if you set values ​​to two selections at once, then only the first is triggered, and the second is ignored. How can you fix this?
Try this:
$result1 = DB::table('ads')
->where('city_slug', $request->sCity)
->get();
$result2 = DB::table('ads')
->where('category_slug', $request->sCat)
->get();
$result = $result1->merge($result2):
you could do something like this
in your view
<div class="form-group">
<select class="selectpicker" name="sCity">
<option value=0>please select one</option>
#foreach($cities as $city)
<option value="{{$city->slug}}">{{$city->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select class="selectpicker" name="sCat">
<option value=0>please select one</option>
#foreach($categories as $cat)
<option value="{{$cat->slug}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
and in your controller
$city_slug='';
$category_slug='';
if($request->sCity!=0)
{
$city_slug=' city_slug='.$request->sCity;
}
if($request->sCat!=0)
{
$category_slug=' or category_slug='.$request->sCat;
}
$result = DB::table('ads')
->whereRaw($city_slug.$category_slug)
->get();
dd($result);

How can I set the default value Laravel <select> element?

I want to retrieve database values in category name and i want to show default value in selection. This is my controller for my edit view.
I have a category_product table.
<div class="col-md-4">
<div class="form-group">
<label for="category">category</label>
<select class="form-control" name="category" id="category">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $product->categories()->category_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
ProductController.php
public function edit(Product $product)
{
$categories = Category::all();
return view('Admin.products.edit', compact('product', 'categories'));
}
Product.php
public function categories()
{
return $this->belongsToMany(Category::class);
}
I get this error.
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$category_id
You need to add default value in the select tag above foreach() loop.
<div class="col-md-4">
<div class="form-group">
<label for="category">category</label>
<select class="form-control" name="category" id="category">
<option value="xyz">xyz</option> /* set default option value */
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$category->id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
If you want set category name selected then you need to match category in product collection. ( if you have catetory_id in product table )
<select class="form-control" name="category" id="category">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$product->catetory_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>

Resources