Laravel Parameter passing error from controller to view - php

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

Related

Data doesn't display

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.

Undefined property: Illuminate\Database\Eloquent\Collection::$id

I don't know exactly why I'm getting this error as indicated by title. I am trying to write the code fro deleting records. This is the code below.
<TABLE >
#foreach($users as $user)
<TR><TD>{{ $user->id }}</TD><TD>{{ $user->firstname }}</TD><TD><div id="divContainer"><div class="theDiv"><form action="/users/{{ $users->id }}" method="POST"> {{ csrf_field() }} {{ method_field('DELETE') }}<button class="css-deletebutton">DELETE</button></form></div></div></TD><TD>{!! Form::submit('DEACTIVATE', ['class'=>'css-statusbutton']) !!}</TD></TR>
#endforeach
<!--{!! $users->render() !!}-->
</TABLE>
This is the route:
Route::resource('users', 'UserController');
This is the controller method:
public function show($id)
{
$users = User::find($id);
return view('userpages.show')->with('users', $users);
}
This is the view to be displayed for deletion of any record.
#section('body')
{!! Form::open([ 'method' => 'delete', 'route' => ['users.destroy', $users->id]]) !!}
<TABLE>
<TR><TD>{{ $users->firstname }}</TD><TD>{{ $users->lastname }}</TD><TD>{{ $users->email }}</TD><TD>{{ $users->username }}</TD><TD>{!! Form::submit('DELETE', ['class'=>'css-deletebutton']) !!}</TD></TR>
</TABLE>
{!! Form::close() !!}
#stop
This is the error message I'm getting:
ErrorException in acf1a7ad2174bd2b743200b1a50b4c9f line 14:
Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: C:\Users\ken4ward\Documents\xampp\htdocs\schoolproject\resources\views\userpages\index.blade.php)
I can see you are referring to $users->id within the foreach loop in the first code snippet - that's what's causing the error you're getting. $users is the variable that holds the collection and $user is the variable that you should use to access user's data, including user's ID.

Trying to get property of non-object in Laravel 5.1

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;
}

Pass $id from blade to controller method show Laravel 4

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

Laravel Form Looping Not Rendering Completely

My View
<tbody>
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
<td>{{ $category->slug }}</td>
<td>{{ ($category->TermTaxonomy ? $category->TermTaxonomy->description : '') }}</td>
<td>
{{ Form::open(['method' => 'DELETE', 'route' => ['admin_posts_categories_destroy', $category->term_id]]) }}
{{ Form::submit('Delete'); }}
{{ Form::close() }}
</td>
</tr>
#endforeach
</tbody>
then result in inspect element
so i can't delete the first row , but i can delete the other , why this things happen ? and how to fix it. already try in the other browser and still same.
Never mind , in have 2 form in my view the first is static form to add term, the second is loop form for restful delete, and missing {{ Form::close() }} in the first form (static) , so just put {{ Form::close() }} in the first form (static) and both form static and looping work like a charm. thanks for all.

Categories