view page:
#if(count($applications)>0)
<table class= "table table-striped">
<tr>
<th>Email</th>
<th>Date Applied</th>
<th>Leave Type</th>
<th>Leave Status</th>
<th></th>
</tr>
#foreach($applications as $application )
<tr>
<td>{{$application ->user->email}}</td>
<td>{{$application ->dateApplied}}</td>
<td>{{$application ->leaveType}}</td>
<td>{{$application ->leaveStatus}}</td>
<td>
{!!Form::open(['action'=>['ApplicationAdminController#destroy', $application ->appId], 'method'=>'DELETE'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
</td>
</tr>
#endforeach
</table>
controller:
public function destroy($appId)
{
$application = Application::find( $appId);
$application ->delete();
$this->deleteLeave($appId);
return redirect('/applicationAdminHistory')->with ('success', 'Application Removed');
}
public function deleteLeave($appId){
$user_id = Auth::id();
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType,$day);
}
What I want to do is when click delete button the application will deleted and will do increments at the leave_summaries table. The increment at the leave_summaries based on the value of day and leave type in the application that has been deleted. After i run this code i got undefined variable error as shown below:
In this Code Please Pass $user_id , $leaveType ,$day:
public function deleteLeave($appId){
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType,$day);
}
You didn't defined these three variables : $user_id, $leaveType, $day
You need to pass these values as parameters into the deleteLeave() method or these values had to be defined before the method called.
public function deleteLeave($user_id, $leaveType, $day){
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType, $day);
}
Related
So I'm printing user complaints in table where I'm also printing a Delete button with every row. When I click that delete button, I want to delete that specific complaint from the table. I'm not using Resource Controller for this but a Basic Controller. Now, this is my code:
ViewComplaint.blade.php (Complaints Table with Delete Button):
<table id="cTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Student Name</th>
<th>Complaint Title</th>
<th>Complaint Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($complaints as $complaint)
<tr>
<td>{{ $complaint->name }}</td>
<td>{{ $complaint->cname }}</td>
<td>{{ $complaint->cbody }}</td>
<td class="btn-group">
{!! Form::open(array('route'=>['complaint.destroy',$complaint->id],'method'=>'DELETE')) !!}
{!! Form::submit('Delete',['type'=>'submit','style'=>'border-radius: 0px;','class'=>'btn btn-danger btn-sm',$complaint->id]) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
Web.php (Routes):
Route::get('/complaint/create','ComplaintController#create')->name('complaint.create');
Route::post('/complaint','ComplaintController#store')->name('complaint.store');
Route::get('/complaint','ComplaintController#index')->name('complaint.index');
Route::delete('/complaint/{$complaint->id}','ComplaintController#destroy')->name('complaint.destroy');
ComplaintController.php (Basic Controller):
class ComplaintController extends Controller
{
public function index() {
$complaints = Complaint::all();
return view('viewcomplaint',compact('complaints'));
}
public function create(User $user) {
$user = User::all();
$user->name = Auth::user()->name;
return view('createcomplaint',compact('user'));
}
public function store(Request $request, Complaint $complaint, User $user) {
$user = User::find($user);
$complaint->name = Auth::user()->name;
$complaint->cname = $request->input('cname');
$complaint->cbody = $request->input('cbody');
//update whichever fields you need to be updated
$complaint->save();
return redirect()->route('home.index');
}
public function destroy(Complaint $complaint,$id)
{
$complaint = Complaint::findOrFail($complaint->id);
$complaint->delete();
return redirect()->route('complaint.index');
}
}
Now when I click the Delete button on the table, it just gives me "404 | Not Found" error. What am I doing wrong here? I would really appreciate some help.
remove the $id from the route
Route::delete('/complain/{id}','ComplaintController#destroy')->name('complaint.destroy');
public function destroy($id) {
}
The route parameter is just a name; you are saying this particular route segment is dynamic and I want the parameter named complaint:
Route::delete('complaint/{complaint}', 'ComplaintController#destroy')->name('complaint.destroy');
Then you can adjust your destroy method to take the parameter complaint typehinted as Complaint $complaint to get the implicit binding:
public function destroy(Complaint $complaint)
{
$complaint->delete();
return redirect()->route('complaint.index');
}
Seems to me you're defining your route wrong. Change your route to:
Route::delete('/complaint/{id}','ComplaintController#destroy')->name('complaint.destroy');
You don't need an array() in your form opening, so hange your form opening to this:
{!! Form::open(['method' => 'DELETE', 'route' => ['complaint.destroy',$complaint->id]]) !!}
And remove the $complaint->id from your submit button, you don't need it there.
All you have to do now inside your function is to find Complaint that has the id you passed in your form:
public function destroy($id)
{
$complaint = Complaint::findOrFail($id);
$complaint->delete();
return redirect()->route('complaint.index');
}
Let me know if you stumble on any errors.
I'm receiving an array from my controller to my view to map it with the #foreach blade loop, but after an insertion into the database, when I want to redirect my app to the index blade document, this error appears:
This is my controller:
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return view('libros.index');
}
And this is my view:
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Titulo</th>
</tr>
</thead>
<tbody>
#foreach ($libros as $libro)
<tr>
<td>{{ $libro->id }}</td>
<td>{{ $libro->titulo }}</td>
</tr>
#endforeach
</tbody>
</table>
I expect that my controller method redirect send me to the view correctly, but everytime I enter a register to the database and the controller redirects me to my blade view, this error appears and I have to reload the page to enter into the view.
In your store method you either have to redirect the user to the index page, or retrieve and pass all the libros array again.
The former way is preferred.
Assuming you have given a name to your route, you can do something like:
public function store(Request $request)
{
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return redirect()->route('libros.index'); // replace libros.index with the index route name
}
I think it will work if you rewrite like this
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
$libros = Libro::all()->get();
return view('libros.index', compact('libros'));
}
I fetched data from my localhost database. However, a "No data available in table" shows up in my datatables. I am having a hard time figuring out what is the problem because I don't get any errors from it. I use the function fetch in my system_model.php to fetch data from the database. Is there any way to find why values from the database are not showing?
Here is my code for my controller:
class SFM_controller extends CI_Controller {
public function __construct() {
parent::__construct();
// Load form helper library
$this->load->helper('form');
$this->load->helper('url');
// // Load form validation library
$this->load->library('form_validation');
// // Load session library
$this->load->library('session');
// Load database
$this->load->model('system_model');
}
public function index()
{
$data = array(
//'logo' => base_url()."/assets/images/logo/fams-small.png",
//'full_name' => $this->session->user_full_name,
'fo_supp' => $this->system_model->fetch('fo_supp'),
);
$this->load->view('includes/SFM/SFM_Header');
$this->load->view('includes/SFM/SFM_NavBar');
$this->load->view('SFM_view', $data);
$this->load->view('includes/SFM/SFM_Footer');
}
function logout()
{
$this->load->view('includes/Login/Login_Header'); //$data);
$this->load->view('Login_view');
$this->load->view('includes/Login/Login_Footer');
}
}
Here is my code for my Model:
class system_model extends CI_Model
{
function fetch($table, $where = null, $group_by = null, $order_by = null, $limit = null)
{
if($where != null) {
$this->db->where($where);
}
if($group_by != null) {
$this->db->group_by($group_by);
}
if($order_by != null) {
foreach ($order_by as $key => $value) {
$this->db->order_by($key, $value);
}
}
if($limit != null) {
$this->db->limit($limit);
}
$query = $this->db->get($table);
return $query->num_rows() > 0 ? $query->result() : false;
}
Here is my code for my View:
<table id="datatable-buttons" class="table table-striped table-bordered">
<thead>
<tr>
<th>Supplier Code</th>
<th>Address</th>
<th>Country</th>
<th>Description</th>
<th>Telephone Number</th>
<th>Fax Number</th>
<th>Consolidating Agent</th>
<th>Contact Person</th>
<th>Actions</th>
<th>Discount 1</th>
<th>Discount 2</th>
<th>Discount 3</th>
<th>Discount 4</th>
<th>Discount 5</th>
<th>Last Transaction</th>
<th>Old Supplier</th>
</tr>
</thead>
<tbody>
<?php if(!empty($fo_supp)): ?>
<?php foreach($fo_supp as $supp): ?>
<tr>
<td> <?=$supp->supp_code?> </td>
<td> <?=$supp->address." ".$supp->address2?></td>
<td><?=$supp->country?></td>
<td><?=$supp->description?></td>
<td><?=$supp->tel_no?></td>
<td><?=$supp->fax_no?></td>
<td><?=$supp->contact?></td>
<td><?=$supp->cons_agent?></td>
<td>$320,800</td>
<td><?=$supp->disc1?></td>
<td><?=$supp->disc2?></td>
<td><?=$supp->disc3?></td>
<td><?=$supp->disc4?></td>
<td><?=$supp->disc5?></td>
<td><?=$supp->last_trans?></td>
<td><?=$supp->supp_code2?></td>
</tr>
<?php endforeach;?>
<?php endif; ?>
</tbody>
</table>
Var dump
Why is my var dump like this? and not showing values
Pass your $data array in the view file instead of header file in controller index function.
$this->load->view('SFM_view', $data);
SOLVED!
I was passing the data to the wrong controller in which my login submit is located in another controller!
I keep on getting this error whenever I try to update something into my data. It didn't happen a lot the last time but after putting the new update function, the error keep popping out and for some reason it points back to the new function that I had entered even though I am updating at the old function.
Controller:
//update for user
public function edit($id){
$object = user::find($id);
return view('edit', compact('object'));
}
public function update(Request $request, $id){
$object = user::find($id);
$object->Name = $request->input('Name');
$object->update();
return redirect('/home');
}
//update for Schools table
public function edit1($id){
$object2 = school::find($id);
return view('edit1', compact('object2'));
}
public function update1(Request $request, $id){
$object2 = school::find($id);
$test = array();
$test['School'] = implode(' , ', $request->School);
$test['SDate'] = implode(' , ', $request->SDate);
$test['EDate'] = implode(' , ', $request->EDate);
$object2->update($test);
return redirect('/home');
}
//error start here after putting this whole thing in. (I tried putting it into another separate controller but the error still continues)
public function edit2($id){
$object3 = hobby::find($id);
return view('edit2', compact('object3'));
}
public function update2(Request $request, $id){
$object3 = hobby::find($id);
$test2 = array();
$test2['computer_game'] = implode(' , ', $request->computer_game);
$test2['reading_book'] = implode(' , ', $request->reading_book);
$object3->update($test2);
return redirect('/home');
}
Error is highlighting this part even though when I try to update user or school data
$test2['computer_game'] = implode(' , ', $request->computer_game);
And it says
:implode(): Invalid arguments passed
I have no issue updating the hobby data but it the error keep pointing it back to the hobby implode part.
Is it possible I could only use update once on an implode function? Thanks in advance
edit2.blade.php (edit page for hobby, as shown it is an array)
<form class="form-horizontal" method="post" action="{{ url('/user/show/'.$object3->id) }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<th class="text-center">
#
</th>
<th class="text-center">
Sports:
</th>
<th class="text-center">
Books read:
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td>
<input type="text" name='computer_game[]' class="form-control"/>
</td>
<td>
<input type="text" name='reading_book[]' class="form-control"/>
</td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
<input type="submit" class="btn btn-primary" value="Save">
Just simple try the following:
$test2['computer_game'] = implode(' , ', (array)$request->computer_game);
This will convert $request->computer_game in to an array if it is not already.
You are getting the error because $request->computer_game is not an array. This can be verified by dumping the variable and killing the script.
var_dump($request->computer_game); die;
first of all what does var_dump(); gives to that object oriented array. plz share then we'll try to answer.
Then to stop multiple loading you can make a private var ($loaded) and by default it would be falseprivate static $loaded = false; then befor any code starts in function use if statement to detect if loaded is true
if(self::$loaded){
return;
}
self::$loaded = true
this would help you! please post var_dump!
I can't figure out why my data is not being passed to View
I get the message Undefined variable: project
Can someone help identify where i am going wrong?
when i return $project->name straight from the controller it displays the response, however fails to pass to view.
Here is my controller function:
public function getApi(){
$client = new Client();
$response = $client->get('https://play.geokey.org.uk/api/projects/77');
$body = $response->getBody()->getContents();
$project = json_decode($body);
return view ('response', compact($project));
}
And here is my View:
<h1>Welcome</h1>
<table class="table table-bordered">
<tr>
<th>Project Name</th>
<th>Date of Creation</th>
<th>Contribution Total</th>
</tr>
<tr>
<td>{{$project->name}}</td>
</tr>
</table>
Instead of:
return view ('response', compact($project));
Type
return view ('response', compact('project'));
return view ('response', compact('project')); instead of compact($project)