I finish to make relation in my model, but i dont know to passing data relation to view, hope you can help me.
Model Siswa
public function Absen()
{
return $this->hasMany(Absen::class);
}
Model Absen
public function Siswa()
{
$this->belongsTo(Siswa::class);
}
Absen Table
Siswa Table
AbsenController#index
public function index()
{
$absen = Absen::where('level', '=', 'Siswa')->get();
return view('absen.index')->with('data', $absen);
}
index.blade.php
#foreach($data as $index => $value)
<tr>
<td>{{ $index+1 }}</td>
<td>{{ $value->nama }}</td>
<td>{{ $value->keterangan }}</td>
<td>
{!! Form::open(['route' => ['siswa.destroy', $value->id],
'method' => 'DELETE']) !!}
{{ Form::submit('Hapus', ['class' => 'btn btn-danger']) }}
<a href="{{ route('siswa.edit', $value->id) }}" class="btn
btn-warning">Edit</a>
{!! Form::close() !!}
</td>
</tr>
#endforeach
The problem is the $value->nama not showing in my view, but i done to create relation in table siswa and table absen.Thank
nama is not an attribute of Absen class, yet your value is an Absen class. Though you have defined the relationship between Absen and Siswa, attributes of Siswa are still not directly inherited to Absens. You can first get the Siswa of the Absen and then get the attributes of the Siswa.
So what you have to do is:
Call $value->Siswa->nama instead of $value->nama, and you should have your nama displayed.
Related
I have Home.blade.php in which if I click vote button it should increment the count in candidate table.
home.blade.php
#foreach ($file as $show)
<div class="col-md-3" style="margin-bottom: 20px;">
<div class="card">
<img src="{{$show->image}}" style="width:100%">
<div class="card-body">
<h5 class="title">President</h5>
<p class="card-text">Name : {{$show->name}}</p>
<button>VOTE</button>
</div>
</div>
</div>
#endforeach
homecontroller
public function Count(){
DB::table('candidate')->increment('count');
return view('/home')->with('success', 'voted');
}
Route
Route::get('/home','Homecontroller#Count');
Please help me to increment the count, if not this method is there any other method to do the same.
Yes, there is a better way.
Add this to Your web.php
Route::put('/votesUp/{vote}', 'HomeController#upVote')->name('votes.upVote');
Route::put('/votesDown/{vote}', 'HomeController#downVote')->name('votes.downVote');
in your view list thats is index.blade.php add this two buttons
FORM BUILDER WAY
#foreach($candidates as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{!! $item->name !!}</td>
<td>{!! Form::model($item, ['method' => 'PUT', 'route' => ['votes.upVote', $item->id ] ,'enctype'=>'multipart/form-data' ]) !!}
{!! Form::submit( 'Up Vote', ['class' => '', 'name' => 'submitbutton', 'value' => 'upvote'])!!}
{!! Form::close() !!}</td>
<td>{!! Form::model($item, ['method' => 'PUT', 'route' => ['votes.downVote', $item->id ] ,'enctype'=>'multipart/form-data' ]) !!}
{!! Form::submit( 'Down Vote', ['class' => '', 'name' => 'submitbutton', 'value' => 'upvote'])!!}
{!! Form::close() !!}</td>
</tr>
#endforeach
HTML WAY
#foreach($candidates as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{!! $item->name !!}</td>
<td>
<form method="post" action="{{ route('votes.upVote', $item->id) }}">
#method('PUT')
#csrf
<input class="" name="submitbutton" value="Up Vote" type="submit">
</form>
<form method="post" action="{{ route('votes.downVote', $item->id) }}">
#method('PUT')
#csrf
<input class="" name="submitbutton" value="Down Vote" type="submit">
</form>
</td>
</tr>
#endforeach
I am Considering Your model as Candidate so add this to HomeController
ELOQUENT way
public function upVote(Request $request, $id)
{
Candidate::find($id)->increment('votes_count', 1);
return redirect()->back();
}
public function downVote(Request $request, $id)
{
Candidate::find($id)->decrement('votes_count', 1);
return redirect()->back();
}
DB Facade Way
public function upVote(Request $request, $id)
{
\DB::table('candidates')->where('id','=',1)->increment('votes_count', 1);
return redirect()->back();
}
public function downVote(Request $request, $id)
{
\DB::table('candidates')->where('id','=',1)->decrement('votes_count', 1);
return redirect()->back();
}
Explained:
When You click on the upvote button filed votes_count in candidates table will be
incremented by 1
When You click on the downvote button filed votes_count in candidates table will be decremented by 1
EDIT FOR undefined variable candidate
Find this line
#foreach($candidates as $item)
And Replace with
#foreach ($file as $item)
FIX FOR
Class App\Http\Controllers\HomeController does not exist
From
Route::put('/votesUp/{vote}', 'HomeController#upVote')->name('votes.upVote');
Route::put('/votesDown/{vote}', 'HomeController#downVote')->name('votes.downVote');
TO
Route::put('/votesUp/{vote}', 'Homecontroller#upVote')->name('votes.upVote');
Route::put('/votesDown/{vote}', 'Homecontroller#downVote')->name('votes.downVote');
Your Controller Class
Homecontroller
but i have wrongly written as
HomeController
Make sure your column count is integer type in migration
And:
You are writing it in wrong way
<button>VOTE</button>
change it to
<button>VOTE</button>
The vote button should be like this:
<button>VOTE</button>
With the functionalities like Upvote and Downvote as answered nicely by #manojkiran-a above, I would also add throttling. Now a days it is easy to create a bot which will send upvote requests even when you are using csrf token.
I would add :
'middleware' => 'throttle:5'
Which will allow only 5 requests per minute per IP address. Not entirely preventing it but making it little harder.
Route::put('/votesUp/{vote}', 'Homecontroller#upVote')->middleware('throttle:5')->name('votes.upVote');
Route::put('/votesDown/{vote}', 'Homecontroller#downVote')->middleware('throttle:5')->name('votes.downVote');
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.
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