Undefined variable $employes my controller
public function create()
{
return view('crud.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',//ambil nama id
'konten' => 'required',//ambil nama id
'image' => 'required|image',//ambil nama id
]);
$input = $request->all();
if ($image = $request->file('image'))
{
$destinationPath = '/uploads/employee/'; //arahfolder
$imageName = date('Y-m-d') . "." . $image->getClientOriginalName(); //nama file
$image->move($destinationPath, $imageName);
$input['image'] = "$imageName";
}
Employe::create($input);
return redirect()->route('employee.index')->with('success', 'sukses dah');
}
<!-- /.card-header -->
<div class="card-body p-0">
<table class="table table-striped table-sm">
<thead>
<tr>
<th style="width: 10px">#</th>
<th scope="col">Nama</th>
<th>Content</th>
<th>Thumbnail</th>
<th style="width: 100px">Action</th>
</tr>
</thead>
<tbody>
@foreach ($employes as $data)
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>
<a href="" class="badge bg-info"><i class="fa-solid fa-eye"></i> </a>
<a href="" class="badge bg-warning"><i class="fa-solid fa-pencil"></i></a>
<a href="" class="badge bg-danger"><i class="fa-solid fa-trash"></i></a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<!-- card-body -->
</div>
<!-- card -->
</div>
</div>
</div>
<!-- row -->
My model
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Employe extends Model
{
use HasFactory;
protected $table = 'employes';
protected $primaryKey = 'id';
protected $fillable = ['name','konten','image'];
}
You need to pass the $employees to the view for it to be accessible. Share your index method.
Can you show me controller for route "emloyee.index". It should pass param $employes to view
@tuankiet
Route::get('/employee', [CobaController::class, 'index'])->name('employee.index');
Route::get('/employee/create', [CobaController::class, 'create'])->name('employee.create');
Route::post('/employee/store', [CobaController::class, 'store'])->name('employee.store');
@tuankiet my index
<!-- /.card-header -->
<div class="card-body p-0">
<table class="table table-striped table-sm">
<thead>
<tr>
<th style="width: 10px">#</th>
<th scope="col">Nama</th>
<th>Content</th>
<th>Thumbnail</th>
<th style="width: 100px">Action</th>
</tr>
</thead>
<tbody>
@foreach ($employes as $dat)
<tr>
<td>{{ $loop->iteration }} </td>
<td> {{ $dat->name }} </td>
<td> </td>
<td> </td>
<td>
<a href="" class="badge bg-info"><i class="fa-solid fa-eye"></i> </a>
<a href="" class="badge bg-warning"><i class="fa-solid fa-pencil"></i></a>
<a href="" class="badge bg-danger"><i class="fa-solid fa-trash"></i></a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<!-- card-body -->
</div>
<!-- card -->
</div>
</div>
</div>
<!-- row -->
@andreans Show me your controller in php code not view blade. What're you writing in this controller CobaController::class, 'index'
public function store(Request $request)
{
$request->validate([
'name' => 'required',//ambil nama id
'konten' => 'required',//ambil nama id
'image' => 'required|image',//ambil nama id
]);
$input = $request->all();
if ($image = $request->file('image'))
{
$destinationPath = '/uploads/employee/'; //arahfolder
$imageName = date('Y-m-d') . "." . $image->getClientOriginalName(); //nama file
$image->move($destinationPath, $imageName);
$input['image'] = "$imageName";
}
Employe::create($input);
$employes=Employe::all();
return redirect()->route('employee.index')->with(['employes' => $employes, 'success' => 'sukses dah']);
}
hope it will be work
@Ishatanjeeb you gave an example of exactly what to watch out for.
This is directly from the documentation:
However, keep in mind that the getClientOriginalName and getClientOriginalExtension methods are considered unsafe, as the file name and extension may be tampered with by a malicious user.
Which is why I gave the references to two videos where symfony demonstrates how to deal with this safely.
Reference
https://laravel.com/docs/10.x/filesystem#other-uploaded-file-information
@andreans
is this error happen after saving new data ? or when you access the your_app_name/employee in your address bar ?
you should have something like this in your CobaController
public function index(){
$employes=Employe::all();
return view('crud.index', compact('employes'));
// I assume this index.blade.php is in the folder resource/views/crud
}
public function create(){
// your code here . . . .
}
public function store(Request $request){
// your code here . . . .
}
// other function here . . . .
Please sign in or create an account to participate in this conversation.