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.
Related
part of this data in store method decoded and i want to use this encoded data in blade template:
How is this possible?
public function store(VehiclesRequest $request)
{
$data = Vehicle::create([
'container_type' => $request->container_type,
'type' => $request->type,
'delivery_capacity' => $request->delivery_capacity,
'plate_number' => json_encode($request->plate_number),
'chasis_number' => $request->chasis_number,
'capacity_dimensions' => json_encode($request->capacity_dimensions),
'fuel_consumption_rate' => $request->fuel_consumption_rate,
'capacity_weight' => $request->capacity_weight,
'insurance_expire_date' => json_encode($request->insurance_expire_date),
'insurance_type' => $request->insurance_type,
'is_available' => $request->is_available,
]);
return redirect()->action('VehicleController#index');
}
VehicleController:
public function index()
{
$vehicles = Vehicle::all();
return view('vehicle.index', compact('vehicles'));
}
view:
<tbody>
#foreach(vehicles as $vehicle)
<tr>
<td>{{ $vehicles->plate_number }}</td>
<td>{{ $vehicles->delivery_capacity }}</td>
</tr>
#endforeach
</tbody>
i used from {{ json_decode($vehicles->plate_number) }} in blade template but an error occurred.
You can Decode json data in view.blade.php like this :
<tbody>
#foreach($vehicles as $vehicle)
<tr>
<td>
#foreach(json_decode($vehicles->plate_number) as $plate)
{{ $plate['variable_name'] }}
#endforeach
</td>
<td>{{ $vehicles->delivery_capacity }}</td>
</tr>
#endforeach
</tbody>
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.
I want to pass the parameter $questions to view, but it gives the following error:
ErrorException (E_ERROR)
Undefined variable: questions (View: C:\Users\Krishan\Documents\GitHub\GroupProject\lcurve\resources\views\quizz\questions\index.blade.php)
This is my controller index function part:
public function index()
{
$questions = Question::all();
return view('quizz/questions.index', compact('questions'));
}
This is a part Of my view:
<tbody>
#if (count($questions_options) > 0)
#foreach ($questions_options as $questions_option)
<tr data-entry-id="{{ $questions_option->id }}">
<td></td>
<td>{{ $questions_option->question->question_text or '' }}</td>
<td>{{ $questions_option->option }}</td>
<td>{{ $questions_option->correct == 1 ? 'Yes' : 'No' }}</td>
<td>
View-->
<!--Edit-->
{!! Form::open(array(
'style' => 'display: inline-block;',
'method' => 'DELETE',
'onsubmit' => "return confirm('".trans("quickadmin.are_you_sure")."');",
'route' => ['questions_options.destroy', $questions_option->id])) !!}
{!! Form::submit(trans('quickadmin.delete'), array('class' => 'btn btn-xs btn-danger')) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endif
</tbody>
enter image description here
where is questions_options coming from? You are passing questions. So your for loop should be
#if (count($questions) > 0)
#foreach ($questions as $question)
//rest of your code
#endforeach
#endif
and your return view part can be return view(quizz.questions.index, compact('questions'))
Firstly, the error message you have mention should be shown. The error message should be:
Undefined variable: questions_options (View:C:\Users\Krishan\Do........
Because you are passing questions to view but you are accessing question_options in view. So, it should say question_options in undefined in view.
Besides, do you know you can avoid this count check? You can use laravel's forelse tag here a below:
#forelse($questions as $question)
//Your table goes here
#empty
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endforelse
Firstly it works fine But I don't know how to come this error in this page but this problem can be solve when i changed the code in view {{$training->first()->sectionsCount}} but my specification has been incorrect.
My complete error is:-
ErrorException in Training.php line 51: Trying to get property of non-object (View:resources/views/Training/index.blade.php)
My code in model is:-
public function sectionsCountRelation()
{
return $this->hasOne('App\Schedule')->selectRaw('training_id, count(*) as count')->groupBy('training_id')->where('training_end_date','<',carbon::now());
}
public function getSectionsCountAttribute()
{
return $this->sectionsCountRelation->count;<!--This is line 51 -->
}
In controller is
public function index()
{
$training = Training::with('sectionsCountRelation')->get();
return view('Training.index',compact('training'));
}
In View:-
#foreach ($training as $training)
<tr>
<td>{{$i}}</td>
<td>{{ $training->category }}</td>
<td>{{ $training->topic }}</td>
<td>{{$training->sectionsCount}}</td>
<td>Update</td>
<td>Schedule</td>
<td>
{!! Form::open(['method' => 'DELETE', 'route'=>['training.destroy', $training->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
<?php $i++;?>
{!! Form::close() !!}
</td>
</tr>
#endforeach
Looks like you have some Trainings that have no Schedule assigned. That means, $this->sectionsCountRelation returns null and you can't access ->count on it. Try checking for null:
public function getSectionsCountAttribute()
{
return $this->sectionsCountRelation === null ? 0 : $this->sectionsCountRelation->count;
}
I am a laravel beginner and trying to pass id to controller method show. It does not showing anything after page reloads. I tried some google stuffs. It didnt bring any help. My related code is given below :
admin.blade.php
<div class="showOne">
<?php if(isset($users)){
var_dump($users);
}
?>
#foreach($inputs as $key => $user)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->address }}</td>
<td>{{ $user->phone }}</td>
<td>
{{ Form::open(['route' => ['admin.show', $user->id], 'method' => 'get']) }}
{{ Form::button('Details') }}
{{ Form::close() }}
</td>
<td>
{{ Form::open(['route' => ['admin.edit', $user->id], 'method' => 'get']) }}
{{ Form::button('Edit') }}
{{ Form::close() }}
</td>
<td>
{{ Form::open(['route' => ['admin.delete', $user->id], 'method' => 'get']) }}
{{ Form::button('Delete') }}
{{ Form::close() }}
</td>
</tr>
#endforeach
controller:
class AdminController extends \BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
// get all the inputs
$inputs = Userdatas::all();
// load the view and pass the inputs
return View::make('pages.admin')
->with('inputs', $inputs);
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
$user = Userdatas::find($id);
var_dump($user);
die();
return View::make('pages.admin')
->with('users', $user);
}
}
routes:
Route::get('admin', [
'uses' => 'AdminController#index'
]);
Route::get('admin/{id}', [
'uses' => 'AdminController#show',
'as' => 'admin.show'
]);
You could just use a link and let Laravel handle it to the propper action via your routes.
See the example below:
// Management
Route::get('management', 'ManagementController#showUser');
Route::get('management/add', 'ManagementController#showAdd');
Route::post('management/add', 'ManagementController#postAdd');
Route::get('management/edit/{id}', 'ManagementController#showEdit');
Route::post('management/edit/{id}', 'ManagementController#postEdit');
Route::get('management/delete/{id}', 'ManagementController#showDelete');
Route::post('management/delete/{id}', 'ManagementController#postDelete');
You can then just make links in your tables and style them via css as buttons.
#foreach($ManagementAll as $Management)
<tr>
<td>{{$Management->username}}</td>
<td>{{$Management->firstname}}</td>
<td>{{$Management->lastname}}</td>
<td>{{$Management->email}}</td>
<td>{{$Management->created_at}}</td>
<td>{{$Management->updated_at}}</td>
<td style="padding-top: 3px; padding-bottom: 0px;">
<a class="btn btn-default btn-circle" href="{{ URL::to('management/edit/' . $Management->id) }}"><i class="fa fa-pencil"></i></a>
<a class="btn btn-default btn-circle" href="{{ URL::to('management/delete/' . $Management->id) }}"><i class="fa fa-times"></i></a>
</td>
</tr>
#endforeach
Controller Show:
public function showEdit($ID)
{
//Return View With Management User Information
return View::make('management.edit')
->with('User', Management::find($ID));
}
Controller Post:
public function postEdit($ID)
{
//Handle Input
//Validation?
//Update Record
//Redirect Back
}
See this website for more information about this topic:
https://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers
Update
My view folder looks like this:
views
management
overview.blade.php
add.blade.php
edit.blade.php
delete.blade.php