I'm trying to update the status of a row in my Funds table using a button.
Here is my controller:
public function changeStatus(Funds $funds)
{
if ($funds > status == false) {
$funds->status = true;
$funds->update(['status' => $funds->status]);
return redirect('funds,index')->with('success', 'Good Job, Fund mark is done!');
} else {
$funds->status = true;
$funds->update(['status' => $funds->status]);
return redirect('funds.index')->with('success', ' Fund is pending!');
}
}
then I created the route for FundsController#changeStatus:
Route::patch('transactions/{funds}/completed', 'FundsController#changeStatus');
The HTML code i used in the index.blade.php file
<div class="card-body">
<table class="table table-bordered">
<thead>
<tr>
<th style="width: 10px">Account Number</th>
<th>Other Account Number</th>
<th>Remarks</th>
<th>acc_type</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach($funds as $fund)
<tr>
<td> {{ $fund->accno }} </td>
<td>{{ $fund->accnumber }}</td>
<td> {{ $fund->Remarks }} </td>
<td>{{ $fund->acc_type }}</td>
<td>{{ $fund->status }}</td>
<td>
{!! Form::model($fund, ['method' => 'PATCH', 'route' => ['funds.changeStatus', $fund->id]]) !!}
<button type="submit" class="btn btn-info">{{ $fund->status == false ? 'Marked Pending' : 'Marked complete' }}</button>
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
```
but I ended up with this error:
> Undefined property: stdClass::$slug (View: C:\Users\user pc\Desktop\dkn\resources\views\funds\index.blade.php)
Where did I go wrong and how can I update funds status using this method?
Oke you should first try to create a correct form.
First i would like to recommend you that you use named routes:
Route::patch('transactions/{funds}/completed', 'FundsController#changeStatus')->name('funds.changeStatus');
This will make it easier to get te correct route in your form.
Then the form should look something like this:
Form::model($fund, ['method' => 'PATCH', 'route' => ['funds.changeStatus', $fund->id]]);
If you want to use the slug (instead of the id):
Form::model($fund, ['method' => 'PATCH', 'route' => ['funds.changeStatus', $fund->slug]]);
When you want to use the slug, make sure to add the following method to your Funds model:
public function getRouteKeyName()
{
return 'slug';
}
Then in the controller:
public function changeStatus(Funds $funds)
{
if ($funds->status == false) {
$funds->update(['status' => true]);
return redirect('funds.index')->with('success', 'Good Job, Fund mark is done!');
} else {
$funds->update(['status' => false]);
return redirect('funds.index')->with('success', ' Fund is pending!');
}
}
OR
public function changeStatus(Funds $funds)
{
$funds->update(['status' => !$funds->status]);
if ($funds->status == true) {
return redirect('funds.index')->with('success', 'Good Job, Fund mark is done!');
} else {
return redirect('funds.index')->with('success', ' Fund is pending!');
}
}
Be sure your view address file is correct.
Related
I have the following method defined in my controller. I want to pass the value of $title along with the search results so that it can be displayed at the top of the blade page, but I am unsure how to do it.
public function index_sog(Request $request)
{
$title = 'Standard Operating Guideline';
return view('knowledgebase.index', [
'kbase' => Knowledgebase::orderBy('category', 'asc')
->filter(request(['tags', 'search']))
->where('type','SOG')
->paginate(20),
'search' => $request->input('search')
]);
}
My output...
<h4>{{//TITLE SHOULD GO HERE//}}</h4>
<div class="panel-container show">
<div class="panel-content">
#foreach ($kbase->groupBy('category') as $category => $group)
<table class="table">
<tr>
<th colspan="3" class="text-center bg-fusion-50"><strong>{{ $category }} <strong></th>
</tr>
#foreach ($group as $kb)
<tr>
<td>{{ $kb->title }}</td>
<td></td>
<td></td>
</tr>
#endforeach
</table>
#endforeach
</div>
</div>
ADD on return variables. And you can use on blade like {{ $title }}
> return view('knowledgebase.index', [
> 'kbase' => Knowledgebase::orderBy('category', 'asc')
> ->filter(request(['tags', 'search']))
> ->where('type','SOG')
> ->paginate(20),
> 'search' => $request->input('search'),
> 'title' => $title
> ]);
For example you can do this way:
return view('knowledgebase.index', [
'kbase' => Knowledgebase::orderBy('category', 'asc')
->filter(request(['tags', 'search']))
->where('type','SOG')
->paginate(20),
'search' => $request->input('search')
])->with('title', $title);
By adding the ->with() method to the return.
You can also put it inside the array of variables that you already have in return.
And then in your view:
<h4>{{ $title }}</h4>
Need some help here, I'm using Laravel app, my problem is my data wont display on the table. I tried some ways to display my data but it doesn't work.
Here's my code.
<tbody>
#if (count($expenses) > 0)
#foreach ($expenses as $expense)
<tr data-entry-id="{{ $expense->id }}">
<td field-key='expense_category'>{{ $expense->expense_category->name or '' }}</td>
<td field-key='entry_date'>{{ $expense->entry_date }}</td>
<td field-key='amount'>{{ $expense->amount }}</td>
<td field-key='created_by'>{{ $expense->created_by->name or '' }}</td>
<td>
#can('view')
#lang('quickadmin.qa_view')
#endcan
#can('edit')
#lang('quickadmin.qa_edit')
#endcan
#can('delete')
{!! Form::open(array(
'style' => 'display: inline-block;',
'method' => 'DELETE',
'onsubmit' => "return confirm('".trans("quickadmin.qa_are_you_sure")."');",
'route' => ['expenses.destroy', $expense->id])) !!}
{!! Form::submit(trans('quickadmin.qa_delete'), array('class' => 'btn btn-xs btn-danger')) !!}
{!! Form::close() !!}
#endcan
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="9">#lang('quickadmin.qa_no_entries_in_table')</td>
</tr>
#endif
</tbody>
here is my expenseController.
but i have never touched this code, i always work on the table form.
class ExpensesController extends Controller
{
/**
* Display a listing of Expense.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
if (! Gate::allows('expense_access')) {
return abort(401);
}
if ($filterBy = Input::get('filter')) {
if ($filterBy == 'all') {
Session::put('Expense.filter', 'all');
} elseif ($filterBy == 'my') {
Session::put('Expense.filter', 'my');
}
}
$expenses = Expense::all();
return view('admin.expenses.index', compact('expenses'));
}
}
try to find are you getting data from DB or not ....
$expenses = Expense::all();
dd($expenses);
It will show your data collection.
As I Think you are not getting any data from here.
Hi I am working on a project and I need to do some addition while working on a table.
like I have to fill the three fields and change the status to 2 using checkbox Array. I tried it all but with no luck. Kindly look into it and Let me know the changes I can Do.
My Model CustomerLoad.php is
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class CustomerLoad extends Model
{
protected $fillable = [
'driver_id',
'vehicle_id',
'company_id',
'customer_id',
'package_type',
'from',
'to',
'in_mile',
'weight',
'height',
'width',
'length',
'rate',
'duration_text',
'ticket_number',
'status_id'
];
public function status()
{
return $this->belongsTo('App\Status');
}
}
My Controller for creating the page is Like this
public function create()
{
$csloads = CustomerLoad::paginate(4);
$driver = Driver::pluck('name', 'id')->all();
$vehicle = Vehicle::pluck('vin', 'id')->all();
$company = Company::pluck('name', 'id')->all();
return view('customerload.create', compact('csloads', 'driver', 'vehicle', 'company'));
}
for storing it
public function updatecsloads(Request $request)
{
if(isset($request->update_all) && !empty($request->checkBoxArray)){
$csloads = CustomerLoad::findOrFail($request->checkBoxArray);
foreach($csloads as $csload){
$input = $request->all();
dd($input);
// $csload->update($input);
}
// return redirect()->back();
// } else {
// return redirect()->back();
}
}
My View is like
<div class="card">
<div class="card-header card-header-rose card-header-text">
<div class="card-ttle">
<h4 class="card-text">Allocate Loads</h4>
</div>
</div>
<div class="card-body">
{!! Form::model(['method' => 'PATCH', 'action' => ['CustomerLoadsController#updatecsloads']]) !!}
<div class='form-group'>
{!! Form::select('checkBoxArray', ['' => 'Allocate'], null, ['class' => 'selectpicker form-control', 'data-style'=>'btn btn-link', 'id'=>''])!!}
</div>
<div class='form-group'>
{!! Form::submit('Allocate Loads', ['class'=>'btn btn-rose pull-right']) !!}
</div>
#if(count($csloads) > 0)
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th><input type="checkbox" id="options"></th>
<th>Driver Name</th>
<th>Company Name</th>
<th>Vehicle Vin Number</th>
<th>Origin</th>
<th>Destination</th>
<th>Package Type</th>
<th>Dimensions</th>
<th>Weight</th>
</tr>
</thead>
<tbody>
#foreach($csloads as $csload)
#if($csload->status_id == 1)
<tr>
<td>{{$csload->id}}</td>
<td><input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="{{$csload->status ? $csload->status->id == 2 : $csload->status->id == 1}}"></td>
<td>{!! Form::select('driver_id', ['' => 'Choose Driver Name'] + $driver, null, ['class' => 'selectpicker form-control', 'data-style'=>'btn btn-link', 'id'=>'exampleFormControlSelect1'])!!}</td>
<td>{!! Form::select('company_id', ['' => 'Choose Company Name'] + $company, null, ['class' => 'selectpicker form-control', 'data-style'=>'btn btn-link', 'id'=>'exampleFormControlSelect1'])!!}</td>
<td>{!! Form::select('vehicle_id', ['' => 'Choose Vehicle Vin Number'] + $vehicle, null, ['class' => 'selectpicker form-control', 'data-style'=>'btn btn-link', 'id'=>'exampleFormControlSelect1'])!!}</td>
<td>{{$csload->from}}</td>
<td>{{$csload->to}}</td>
<td>{{$csload->package_type}}</td>
<td>{{$csload->length}}x{{$csload->width}}x{{$csload->height}}</td>
<td>{{$csload->weight}}</td>
</tr>
#endif
#endforeach
</tbody>
</table>
</div>
#else
<h1 class="text-center">No Loads Found</h1>
#endif
{!! Form::close() !!}
</div>
</div>
</div>
I am struck in the project kindly answer the question as soon as you can I shall be really obliged.
Thanks in advance
From Munish Rana
Love your Work
change 'method' => 'PATCH' to 'POST'
and add {{ method_field('PATCH') }} to form field like this
<form method="POST" action="your url" >
{{csrf_field()}}
{{ method_field('PATCH') }}
</form>
I am pulling multiple records from MySQL and looping through model binding and filling all the input fields with some data.
Now, I may change 2 or 10 or all 26 fields and hit update button. I want to update all the records. Now, I don't know how $id works here? usually I update single record and I have $id which I can find and update only that field.
But that's not the case here. I am pulling 13 records(or 26 fields). 13 field_1 and 13 field_2. How to update all?
mycode
Database
Table
-id
-name
-field1 (updating this one)
-field2 (updating this one)
Routes
Route::get('/cat' , 'AdminController#cat');
Route::patch('/cat/{$id}/update','AdminController#cat_update');
Controller
public function cat(){
$cattfs = Catf::all();
return view('/cat',compact('cattfs'));
}
public function cat_update(Request $request, $id) // id = 1
{
$rules = array(
'field1' => 'required',
'field2' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect()->back()->withErrors($validator);
} else {
$cat = Cattf::find($id); //This wont work :/
$cat ->field1 = Input::get('field1');
$cat ->field2 = Input::get('field2');
$cat ->save();
return redirect('/cat');
}
}
Views
<div class="col-md-6" style="background-color:#fff">
<table class="table table-hover">
<thead>
<tr>
<th style="text-align: center">Product</th>
<th style="text-align: center">Pr</th>
<th style="text-align: center">Co</th>
</tr>
</thead>
<tbody>
#foreach ($cattos as $catto)
{!! Form::model($catto,[ 'method' =>'PATCH', 'url' => ['/cat/.$catto->id./update']]) !!}
<tr>
<td>{{$catto->name}}</td>
<td> {!! Form::text('field1' ,null , ['class'=>'form-control']) !!}</td>
<td> {!! Form::text('field2' ,null , ['class'=>'form-control']) !!}</td>
</tr>
#endforeach
<td colspan="3">
{!! Form::submit('UPDATE', ['class'=>'btn btn-primary btn-block']) !!}
{!! Form::close() !!}
</td>
</tr>
</tbody>
</table>
</div>
snapshot of the form
I don't think you can do this with Model binding (correct me if I'm wrong someone).
What you can instead do is generate an array of data to post to your controller.
For example:
{!! Form::open(['route' => 'catupdate']) !!}
#foreach ($cattos as $catto)
<tr>
<td>{{$catto->name}}</td>
<td>{!! Form::text('categories['.$catto->id.'][field1]', null, ['class'=>'form-control']) !!}</td>
<td>{!! Form::text('categories['.$catto->id.'][field2]', null, ['class'=>'form-control']) !!}</td>
</tr>
#endforeach
{!! Form::close() !!}
You should then receive an array in your controller, which you can loop around and update each record.
public function catupdate()
{
$categories = request()->input('categories');
foreach($categories as $id => $values) {
$cat = Cattf::find($id);
$cat->field1 = $values['field1'];
$cat->field2 = $values['field2'];
$cat->save();
}
}
You can then also validate the array by doing the following in your Request file
public function rules()
{
return [
'categories.*.field1' => 'required',
'categories.*.field2' => 'required'
];
}
this is untested and is an example demonstrating the concept
Just started with laravel using phpStorm. Im trying to create a form that take inputs, and a checkbox, then adds it to a database.
All of the rows in the database get listed in the view index.html. The user should be allowed to activate or deactivate (with the checkbox) the the message that should be listed when browsing the index.html file. Because if a news article is active, and you want to deactivate it, you should be able to just uncheck the checkbox, and it will be deactivated.
So I am wondering how my function should look like.
Im also unsure how to run the function ActivatedOrDeactivated when the checkbox gets checked or unchecked.
Its my first post, so please tell if something is hard to understand in my description.
My route:
Route::get('admin',
[
'uses' => 'NyhetsController#index',
'as' => 'admin.index'
]
);
Route::get('admin/create',
[
'uses' => 'NyhetsController#create',
'as' => 'admin.create'
]
);
Route::post('admin',
[
'uses' => 'NyhetsController#store',
'as' => 'admin.store'
]
);
Route::get('admin/{id}',
[
'uses' => 'NyhetsController#show',
'as' => 'admin.show'
]
);
Route::get('admin/{id}/edit',
[
'uses' => 'NyhetsController#edit',
'as' => 'admin.edit'
]
);
Route::put('admin/{id}',
[
'uses' => 'NyhetsController#update',
'as' => 'admin.update'
]
);
Route::get('admin/{id}/delete',
[
'uses' => 'NyhetsController#delete',
'as' => 'admin.delete'
]
);
Route::delete('admin/{id}',
[
'uses' => 'NyhetsController#destroy',
'as' => 'admin.destroy'
]
);
My Controller that contains the function (just something i wrote in anger):
public function ActivatedOrDeactivated($id){
$news = News::findOrFail($id);
$input = Input::get('active');
if($input == true)
{
$news->active = 'false';
$news->update($input);
}
else{
$news->active = 'true';
$news->update($input);
}
}
My index file that contains the form:
<h1>Alle postene i databasen</h1>
<p>{{ link_to_route('admin.create', 'Legg til ny nyhet') }}</p>
#if($news->count())
<table class="table table-striped table-boarded">
<thead>
<tr>ID</tr>
<tr>Title</tr>
<tr>Author</tr>
<tr>Message</tr>
<tr>Active</tr>
<tr>Picture path</tr>
<tr>Activate/Deactivate</tr>
</thead>
<tbody>
#foreach($news as $n)
<tr>
<td>{{ $n->id }}</td>
<td>{{ $n->title }}</td>
<td>{{ $n->author }}</td>
<td>{{ $n->message }}</td>
<td>{{ $n->active }}</td>
<td>{{ $n->picture_path }}</td>
<td>{{ Form::checkbox('active') }}</td>
<td>{{ link_to_route('admin.destroy', 'Delete', array($n->id)) }}</td>
</tr>
#endforeach
</tbody>
</table>
When you want to update the news item when a user clicks the checkbox without ajax this would a way to do it:
router.php
Route::post('admin/{id}/active',
[
'uses' => 'NyhetsController#toggleActive',
'as' => 'admin.toggle_active'
]
);
HTML index.blade.php
#if($news->count())
<table class="table table-striped table-boarded">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Message</th>
<th>Picture path</th>
<th>Active</th>
</tr>
</thead>
<tbody>
#foreach($news as $n)
<tr>
<td>{{ $n->id }}</td>
<td>{{ $n->title }}</td>
<td>{{ $n->author }}</td>
<td>{{ $n->picture_path }}</td>
<td>{{ $n->message }}</td>
<td>
{{ Form::open(array('url' => 'admin/' . $n->id . '/active')) }}
{{ Form::submit('', [ 'class' => ($n->active ? 'glyphicon glyphicon-ok' : 'glyphicon glyphicon-remove') ]);
{{ Form::close() }}
<td>{{ link_to_route('admin.destroy', 'Delete', array($n->id)) }}</td>
</tr>
#endforeach
</tbody>
</table>
#endif
NyhetsController
public function toggleActive($id) {
$news = News::findOrFail($id);
if($news->active)
{
$news->active = 'false';
$news->update($input);
}
else{
$news->active = 'true';
$news->update($input);
}
}
return Redirect::to('admin');
Code is untested!
In your Form::
{{ Form::checkbox('active', 'true') }}
In your Controller
$news = News::findOrFail($id);
if (Input::get('active') === 'true') {
$news->active = 'true'; // UPDATE:must be true of course
$news->update($input);
} else {
$news->active = 'false'; // UPDATE:must be false of course
$news->update($input);
}
By the way: you could save yourself some typing if you simply route restfully using
Route::resource('admin', 'NyhetsController');