ErrorException in 1e886a45102a3e4898f23b52cd7ca771 line 354: Undefined variable: data (View: C:\xampp\htdocs\soulfy_repo\framework\resources\views\soulfy\setting.blade.php)
Where should I put my code to define data?
setting.blade.php
<form action="{{ action('HomeController#getBackgroundTheme')}}" method="post">
<span class="setting-name">THEME</span>
<!-- <form method="POST" action="/posts"> -->
{{ csrf_field() }}
<span class="setting-value center">
<select name="menu">
<option value="Landscape">Landscape</option>
<option value="Lifestyle">Lifestyle</option>
<option value="Music">Music</option>
<option value="Office">Office</option>
<option value="Hobby">Hobby</option>
<option value="Politic">Politic</option>
<option value="Building">Building</option>
</select>
<!-- <div style="width: 150px; height: 30px;"><input type="image" src="http://localhost/framework_freshway/public_html/images/submit.png" value="SUBMIT" width="10"> -->
List User
#foreach($data as $d)
<tr>
<td>
<img width="200px" height="200px" src="{{url('/')}}/uploads/{{$d->background}}"/>
</td>
</tr>
#endforeach
<input type="submit" value="Submit">
</span>
<br><br><br>
</form>
HomeController.php
public function getBackgroundTheme()
{
$data = user::all();
return view('setting', ['data' => $data]);
}
You should either pass an empty array $data, or check if $data is set before using it. I personally prefer the second method.
#if(isset($data))
#foreach($data as $d)
<tr>
<td>
<img width="200px" height="200px" src="{{url('/')}}/uploads/{{$d->background}}"/>
</td>
</tr>
#endforeach
#endif
public function getBackgroundTheme()
{
$data = user::all();
return view('setting')->with('data',$data);
}
You are not passing variables to views. either you can with method or you can compose
To solve the problem:
check if $data defined before excute a loops
#if (isset($data))
#foreach($data as $d)
<tr>
<td>
<img width="200px" height="200px" src="{{url('/')}}/uploads/{{$d->background}}"/>
</td>
</tr>
#endforeach
#endif
Recommendation: create form view and post form in two controller actions.
Related
I'm creating a table, whose rows are wrapped inside a livewire component
<div class="card">
<div class="card-body">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Nombre del Riesgo</th>
<th>Información</th>
<th>Dueño del Riesgo</th>
<th>Costo Adicional</th>
<th>Prevención</th>
</tr>
</thead>
<tbody>
<div>
#foreach($risks as $risk)
<livewire:risk-row :risk="$risk"/>
#endforeach
</div>
</tbody>
</table>
</div>
</div>
But in the row component, whenever I change an input, or select something from a dropdown select, the component is not re rendering.
I was wondering why this could be happening. So I opened my console and saw:
Livewire: Multiple root elements detected. This is not supported. See docs for more information https://laravel-livewire.com/docs/2.x/troubleshooting#root-element-issues <div wire:id="6CNMP1hiBR3Qy7IbmfbQ"> </div>
value # index.js:85
Component # index.js:27
(anonymous) # index.js:88
value # index.js:87
(anonymous) # schedule:432
Here's the component blade file
<div>
<tr>
<td>{{ $risk['name'] }}</td>
<td>
<div><strong>Proceso: </strong>{{ $risk['process']['name'] }}</div>
<div><strong>Frecuencia: <span class="badge badge-secondary text-sm">{{ $risk['frequency_label'] }}</span></strong></div>
<div><strong>Impacto: </strong><span class="badge badge-info text-sm">{{ $risk['impact_label'] }}</span></div>
<div><strong>Riesgo: </strong><span class="badge badge-{{ $risk['risk_color'] }} text-sm">{{ $risk['risk_label'] }}</span></div>
</td>
<td>
<select name="owner" id="owner" class="form-control" wire:change="test">
#foreach(App\Models\Risk::OWNERS as $owner)
<option value="{{ $owner['id'] }}">{{ $owner['name'] }}</option>
#endforeach
</select>
{{ $owner_id }}
</td>
<td>
<select name="owner" id="owner" class="form-control">
#foreach(App\Models\Risk::COSTS as $cost)
<option value="{{ $cost['id'] }}">{{ $cost['name'] }}</option>
#endforeach
</select>
</td>
<td>
<select name="owner" id="owner" class="form-control">
#foreach(App\Models\Risk::PREVENTIONS as $prevention)
<option value="{{ $prevention['id'] }}">{{ $prevention['name'] }}</option>
#endforeach
</select>
</td>
</tr>
</div>
Is there any workaround for this?
When using livewire inside a foreach, you need to add a key.
<tbody>
#foreach ($risks as $risk)
<livewire:risk-row :risk="$risk" :wire:key="$loop->index">
#endforeach
</tbody>
Also, you could use the <tr> as the root element and avoid having <div> inside the <tbody>.
Finally, you're using a lot of id and name attributes that repeat over and over. You should not have duplicate ids. As for the name attributes, you can use the array notation.
<tr>
<td>{{ $risk['name'] }}</td>
<td>
<div><strong>Proceso: </strong>{{ $risk['process']['name'] }}</div>
<div><strong>Frecuencia: <span class="badge badge-secondary text-sm">{{ $risk['frequency_label'] }}</span></strong></div>
<div><strong>Impacto: </strong><span class="badge badge-info text-sm">{{ $risk['impact_label'] }}</span></div>
<div><strong>Riesgo: </strong><span class="badge badge-{{ $risk['risk_color'] }} text-sm">{{ $risk['risk_label'] }}</span></div>
</td>
<td>
<select name="owner[]" class="form-control" wire:change="test">
#foreach(App\Models\Risk::OWNERS as $owner)
<option value="{{ $owner['id'] }}">{{ $owner['name'] }}</option>
#endforeach
</select>
{{ $owner_id }}
</td>
<td>
<select name="cost[]" class="form-control">
#foreach(App\Models\Risk::COSTS as $cost)
<option value="{{ $cost['id'] }}">{{ $cost['name'] }}</option>
#endforeach
</select>
</td>
<td>
<select name="prevention[]" class="form-control">
#foreach(App\Models\Risk::PREVENTIONS as $prevention)
<option value="{{ $prevention['id'] }}">{{ $prevention['name'] }}</option>
#endforeach
</select>
</td>
</tr>
I make task manager there is a form where to assign task I want to do that if user select multiple option then it is seperated by comma
This is my form
<form method="post" action="{{route('assignments.store')}}">
#csrf
<table>
<tr>
<td>Task Title : </td>
<td>
<select name="task_id" id="task_id">
#foreach ($tasks as $task)
<option value="{{ $task->id }}">{{ $task->title }}</option>
#endforeach
</select>
</td>
</tr>
<tr>
<td>Staff Name : </td>
<td>
<select name="staff_id" multiple>
<option value=null>Select One</option>
#foreach ($staffs as $staff)
<option value="{{ $staff->id }}">{{ $staff->name }}</option>
#endforeach
</select>
</td>
</tr>
<tr>
<td>Done At :</td>
<td><input type="time" name="done_at" class="form-control"></td>
</tr>
<td><button class="btn btn-primary" name="submit" type="submit" value="submit">Submit</button></td>
</table>
</form>
And this is my store function from where I storing the data recieved from form
$request->validate([
'staff_id' => 'required',
'task_id' => 'required',
'done_at' => 'sometimes',
]);
$assignment = new Assignment();
$assignment->staff_id = $request->staff_id;
$assignment->task_id = $request->task_id;
$assignment->done_at = $request->done_at;
$assignment->save();
return redirect()->route('assignments.index', compact('assignment'))->withSuccess('Done');
try this,
change the name=staff_id[]
<select id="staff" name="staff_id[]" multiple="multiple">
---
</select>
you can check the by
dd(implode(',', $request->staff_id));
$assignment = new Assignment();
$assignment->staff_id = implode(',', $request->staff_id);
And ya one more thing I forgot to tell you in your database I think staff_id is f.k so it will allow one id at the item to store so what you have to do CHANGE staff_id with type varchar then it will allow to store value with comma separate.
And below is the image form implode we are converting an array to string
Please set select name = staff_id[] then only you will get multiple data
I'm trying to get the user role with permissions and compare them with all permissions that are in the database, if they match then a checkbox should be checked... I'm not sure if I'm doing it correctly
RoleController :
public function edit($id)
{
$role =Role::where('id', $id)->with('permissions')->first();
$permissions = Permission::all();
return view('admin.roles.edit', compact('role', 'permissions'));
}
edit.blade.php :
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">TYPE</th>
<th scope="col">CREATE</th>
<th scope="col">VIEW</th>
<th scope="col">UPDATE</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody>
#foreach($permissions as $permission)
<tr>
<td>
<div class="custom-control custom-checkbox ml-3">
<input type="checkbox" class="custom-control-input" id="{{ $permission->name }}" value="{{ ($permission->name) }}" {{ ($permission->name == "$role->permissions->name") ? "checked" : "" }}>
<label class="custom-control-label" for="{{ $permission->name }}">{{ $permission->display_name }}</label>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
I'm not getting any errors but I cant make the checkbox to be checked if they match... I'm using Laratrust 5.2
You can do it like this,
#foreach($permissions as $permission)
#php
$attribute = ($permission->id === $role->permissions->id)
? 'checked="checked"'
: '';
#endphp
<label for="permission-{{ $permission->id }}">
<input type="checkbox" {{ $attribute }} class="custom-control-input" id="permission-{{ $permission->id }}" value="{{ ($permission->id) }}">
<span>{{ ($permission->name) }}</span>
</label>
#endforeach
$permissions and $role->permissions should share the id field with same integer value where applicable.
I've added some extra html as it's considered as a good practice and changed your id & value attributes so it becomes more precise & standard.
i have a view input like that :
<form class="form-group" action="/user6" method="post" enctype="multipart/form-data">
<table class="table table-striped">
<tbody><tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th style="width: 60px">Tidak Baik</th>
<th style="width: 60px">Baik</th>
</tr>
<div class="form-group">
<tr>
<td>1.</td>
<td><input class="form-control" style="border:none" type="text" name="question1" value="Kondisi, kebersihan, pelumasan bearing" readonly></td>
{{-- <td><input class="form-control" type="text" placeholder=".input-lg"></td> --}}
<td>
<label><input type="radio" name="answer1" value="tidak baik" checked></label>
</td>
<td>
<label><input type="radio" name="answer1" value="baik"></label>
</td>
</tr>
<tr>
<td></td>
<td> <input class="form-control" style="border:none" type="file" name="image" value="" readonly> </td>
</tr>
</div>
<tr>
<td></td>
<td> <div class="form-group">
<label>Catatan</label>
<textarea class="form-control" name="catatan" rows="3" placeholder="Enter ..." required></textarea>
</div>
</tr>
<input type="hidden" name="alat_id" value="7">
<input type="hidden" name="status" value="3 Bulanan">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</tbody>
</table>
<div class="box-footer">
<button type="submit" class="btn btn-primary" name="name" value="post">Submit</button>
</div>
</form>
and to save this i have controller :
public function store6(Request $request)
{
if($request->hasfile('image'))
{
foreach($request->file('image') as $file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/images/', $name);
$data[] = $name;
}
}
$user = new pemeliharaan;
$id = Auth::user()->id;
$user->user_id = $id;
$user->alat_id = $request->alat_id;
$user->pertanyaan =json_encode($request->except
(['_token','name','alat_id','status','catatan']));
$user->catatan = $request->catatan;
$user->status = $request->status;
$user->save();
//dd($user);
return redirect('user/show6')->with('success', 'Data Telah Terinput');
}
before i add "enctype="multipart/form-data" , my view dont have error BUT cant display and saved image at directory . i want saved image to directory but cant saved .
i create a folder named 'images' at public . after i add enctype="multipart/form-data" . this view having error "htmlspecialchars() expects parameter 1 to be string, array given "
its my view after input :
<table class="table table-condensed">
<tbody><tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th>Hasil</th>
{{-- <th style="width: 40px">Label</th> --}}
</tr>
<tr>
<td>1.</td>
<td>{{ $pemeliharaan->pertanyaan['question1'] }}</td>
<td>
{{ $pemeliharaan->pertanyaan['answer1'] }}
</td>
</tr>
<tr>
<td>2.</td>
<td>{{ $pemeliharaan->pertanyaan['image'] }}</td>
<td>
{{ $pemeliharaan->pertanyaan['image'] }}
</td>
</tr>
<td><img src="{{ url('images/'.$pemeliharaan->pertanyaan['image'])}}"></td>
</tbody></table>
Something in your table is array, it's not a string so when you put it inside {{}}, it will show error.
You should check
$pemeliharaan->pertanyaan['question1']
$pemeliharaan->pertanyaan['answer1']
$pemeliharaan->pertanyaan['image']
by using dd() function.
Deal with one problem at a time. At present you are storing the image but not saving the path to the file, which you saved in $data. You are not using $data anywhere.
Then you have a problem rendering the new model in the view. This is a totally different issue and is caused by having non-string data passed to the htmlspecialchars function which is what {{ }} does.
This is my view, and I want to pass r{{$i}}_selected to controller, but it gives me an error. Undefined variable: enterprise . How can I solve this?
<form name="frm-example" id="frm-example"
action="{{ route('pengawas_alokasi.store', $enterprise ) }}"
method="post">
{{ csrf_field() }}
<div class="table-responsive text-center">
<table class="table table-borderless table-striped table-hover" id="myTable">
<thead class="thead-dark">
<tr>
<th></th>
<th class="text-center">#</th>
<th class="text-center">Nama Perusahaan</th>
</tr>
</thead>
<?php $i=1;?> #foreach($enterprises as $enterprise)
<tr>
{{--
<td></td> --}}
<td>
<input type="checkbox" name="r{{$i}}_selected" value="{{$enterprise->id}}">
</td>
<td>{{$i}}</td>
{{-- <td>
<input type="hidden" name="nomor{{$i}}" value="{{$i}}" size="3">{{$i}}</td> --}}
<td>{{$enterprise->nama_perusahaan}}</td>
{{-- <td>
<input type="hidden" name="nama_perusahaan{{$i}}" value="{{$enterprise->nama_perusahaan}}" size="3">{{$enterprise->nama_perusahaan}}</td> --}}
</tr>
<?php $i=$i+1;?> #endforeach
</table>
</div>
<div class="form-group">
<input type="submit" name="" class="btn btn-primary" value="Submit">
</div>
</form>
This is the controller that responsible to the view above
public function insert($id)
{
$enterprises = Enterprise::get();
return view('pengawas.pengawas-insert-alokasi', compact('enterprises'));
}
This is the controller that should store the value that I want to retrieve in my view form
public function store_enterprise(Request $request){
//I'm not doing it yet
dd($request);
}
because you are sending "enterprises" from your controller and you are trying to send "enterprise" in your form's action
you can use hidden variable in your form instead of use URL variable :
<input type="hidden" name="enterprise" value="{{$enterprise}}">
and in your controller :
public function insert(Request $request)
{
//get enterprise value here
$enterprise = $request->enterprise;
}