Fellow coders,
It might be a stupid question but I really am stuck on this part in my application.
I am making an hourregistration system for the company i'm an intern at. What I have done is creating a delete button in my application that deletes a record with the click of a simple button. Yet what I did was to delete all the visible records in the table I created that shows all the registrations.
When I deleted the final record I got an error of "undefined index hourregistration".
public function index()
{
$hoursregistrations = Hoursregistration::latest()->get();
$user_id = Sentinel::getUser();
$project_id = Project::pluck('description', 'id');
$company_name = Company::where('status','client')->pluck('company_name', 'id');
$activity_name = Subproject::pluck('id');
//dd($hoursregistrations);
return view('hoursregistrations.index', compact('hoursregistrations', 'user_id', 'project_id',
'activity_name', 'company_name'));
}
I think the problem lies at
$hoursregistrations = Hoursregistration::latest()->get();
Because I'm trying to get the latest value of the registration but there is none right?
Now i'm wondering how I could still show my view without breaking my inserting and/or viewing portion of the app.
#foreach ($hoursregistrations as $hoursregistration)
<tr>
<td hidden>{{ $user_id->first_name }}</td>
<td >{!! App\Project::getCompanyName($hoursregistration->project_id) !!} - {!! \App\Subproject::getTaskTitle($hoursregistration->subproject_id)!!}</td>
<td>{!! $hoursregistration->note !!}</td>
<td>{!! \App\Helpers::dateFormat($hoursregistration->date) !!}</td>
<td>{!! $hoursregistration->hours !!}</td>
<td>
<button id="btn-edit" name="btn-edit" class="btn btn-warning btn-xs btn-detail open-modal" value="{{$hoursregistration->id}}">Edit</button>
<form action="hoursregistrations/{{ $hoursregistration->id }}/delete" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button id="btn-delete" type="submit" name="btn-delete" class="btn btn-danger btn-xs btn-delete delete-hoursregistration" value="{{ $hoursregistration->id }}">Delete</button>
</form>
</td>
</tr>
#endforeach
This is the foreach loop that shows the data in the index.blade.php view
I would love some help so I can continue finishing this application
public function index()
{
$hoursregistrations = Hoursregistration::latest()->get();
$user_id = Sentinel::getUser();
$project_id = Project::pluck('description', 'id');
$company_name = Company::where('status','client')->pluck('company_name', 'id');
$activity_name = Subproject::pluck('id');
//dd($hoursregistrations);
return view('hoursregistrations.index', compact('hoursregistrations', 'user_id', 'project_id',
'activity_name', 'company_name'));
}
Everything is about s or not s.
The error you are talking about is because you have an occurrence of $hoursregistration (without s) before or after the #foreach of your view. The solution is to check and remove occurrence of $hoursregistration outside your loop:
// You can't use $hoursregistration before
#foreach ($hoursregistrations as $hoursregistration)
{{-- ... do what you want with $hoursregistration --}}
#endforeach
// You can't use $hoursregistration after
I am sure there is a confusion (typo) between $hoursregistration, $hoursregistrations and $hourregistration (the confusion is even in your post VS comment)
you can use if here if $hoursregistrations exists then it populate data else nothing happen
#if($hoursregistrations)
#foreach ($hoursregistrations as $hoursregistration)
<tr>
<td hidden>{{ $user_id->first_name }}</td>
<td >{!! App\Project::getCompanyName($hoursregistration->project_id) !!} - {!! \App\Subproject::getTaskTitle($hoursregistration->subproject_id)!!}</td>
<td>{!! $hoursregistration->note !!}</td>
<td>{!! \App\Helpers::dateFormat($hoursregistration->date) !!}</td>
<td>{!! $hoursregistration->hours !!}</td>
<td>
<button id="btn-edit" name="btn-edit" class="btn btn-warning btn-xs btn-detail open-modal" value="{{$hoursregistration->id}}">Edit</button>
<form action="hoursregistrations/{{ $hoursregistration->id }}/delete" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button id="btn-delete" type="submit" name="btn-delete" class="btn btn-danger btn-xs btn-delete delete-hoursregistration" value="{{ $hoursregistration->id }}">Delete</button>
</form>
</td>
</tr>
#endforeach
#endif
i think this might help
Related
Contact Model
public function number()
{
return $this->hasMany('App\Models\Number');
}
Number Model
public function contact()
{
return $this->belongsTo('App\Models\Contact');
}
Controller
public function viewContacts(){
$contacts = Contact::with('number')->get();
return view('contacts.view_contacts')->with(compact('contacts'));
}
}
my view
#foreach($contacts as $contact)
<tbody id="myTable">
<tr>
<td>{{ $contact->name}}</td>
<td>{{ $contact->address}}</td>
<td>
#foreach ($contacts as $number)
{{ $number->number }}
#endforeach
</td>
<td>
<a class="btn btn-xs btn-info" href="{{ ('edit-contact/'.$contact->id) }}">Modifica</a>
<span class="glyphicon glyphicon-remove"></span> Elimina</td>
</td>
</tr>
#endforeach
the results is:
But I need to see only the numbers associated with that contact
In Numbers table I have contact_id for reference.
thanks so much. help me.
tables:
Try this :
#foreach ($contact->number as $number)
{{ $number->number }}
#endforeach
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');
I have form and i am using form model binding. My form is
{!! Form::model($vehicle,['url' => '/pages/store']) !!}
<table style="width:650px; margin-left: 4px;" >
<tbody>
<tr>
<td>ID</td>
<td>Model</td>
<td>Brand</td>
<td>License Plate</td>
</tr>
<tr>
<td>{!! Form::text('id' ,null , ['readonly'], ['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
<td>{!! Form::text('model' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
<td>
{!! Form::select('brand_id', $brands, null, ['id'=>'brandBox', 'style' => 'width:150px;']) !!}
</td>
<td>{!! Form::text('licenseplate' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
</tr>
<tr>
<td colspan="2">Client</td>
</tr>
<tr>
<td colspan="2">{!! Form::select('representive_client_id', $clients, null, ['id'=>'clientSelectBox', 'class' => 'selectbox']) !!}</td>
</tr>
<tr>
<td colspan="2">Telephone Number</td>
</tr>
<tr>
<td colspan="2">{!! Form::text('tel_number' ,null ,['class' =>'textboxlong form-control', 'style'=>'height:23px;']) !!}</td>
</tr>
<tr>
<td colspan="2">Adress</td>
<td colspan="2">{!! Form::textarea('address' ,null ,['class' =>'textboxlong form-control','style'=>'height:60px;']) !!}</td>
</tr>
</tbody>
</table>
<div id="buttoncontainer">
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $first -> id ) }}"><<</a>
#if($previous)
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $previous ) }}">PREVIOUS</a>
#endif
#if($next)
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $next ) }}">NEXT</a>
#endif
<a class="btn btn-default" href="{{ URL::to( 'pages/vehicleprocess/' . $last -> id ) }}">>></a>
<a class="btn btn-default" id="add">EKLE</a>
{!! Form::submit('EDIT', array('class'=>'btn btn-primary')) !!}
{!! Form::submit('NEW RECORD', array('class'=>'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
I am passing the $vehicle as
$vehicle = Vehicle::where('vehicles.id',$id)->join('clients', 'vehicles.representive_client_id', '=', 'clients.id')->first();
Store Function
$client = new Client;
$client -> full_name = $client_id;
$client -> tel_number = $tel_number;
$client -> mobile_number = $mobile_number;
$client -> save();
$last_client_id = $client -> id;
$input = Request::except('client_id');
$vehicle = Vehicle::create($input);
$u_vehicle = Vehicle::orderBy('created_at', 'desc')->first();
$u_vehicle -> update(array('client_id' => $last_client_id));
I am able to see all values of these fields in my view but when it comes to store a new record to my database i am getting this error
Column not found Unknown column 'tel_number'
Guess i need to pass 2 models (Vehicle and Client) to the form but not sure how to make it. Any help would be appreciated.
In short you need to pass your view a instance of Vehicle with Client - I know you're trying to do that with the join but I'm guessing that isn't the right way to do it. I set up a clean install with Laravel and it appears to work when you set up the relationship on the model.
I'm unsure what your relationships are so let's assume a Client has one Vehicle so the Vehicle belongs to the Client. So we can put your relationship in your Vehicle.php
public function client() {
return $this->belongsTo('App\Client'); // or whatever the namespace to your class is
}
See more here on defining relationships: http://laravel.com/docs/5.1/eloquent-relationships#defining-relationships
Then when you load the model in your view you can do this:
$vehicle = \App\Vehicle::with('client')->where('id', $id)->first();
The important part being: with('client')
Then any text fields with Client attributes should be populated with the existing data.
I hope that helps, if you need more help please let me know. I've got sample code of it working in a blank laravel project so I can throw that up on github if you need to see it.
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
I have the following piece of code
public function index()
{
//
$leerlingen = DB::table('leerling')->get();
$leerling = Admin_Leerling::find(1);
dd($leerling->klas->title);
return View::make('leerling.index',compact('leerlingen'));
}
Like you can see, I am trying to get the title of my Klas in my controller. This is working I'm getting the correct value. But when I delete the dd($leerling->klas->title); and try to do the same thing but then in my index.blade.php I get an error.
#if(count($leerlingen))
#foreach($leerlingen as $leerling)
<tr>
<td>{{{ $leerling->id }}}</td>
<td>{{{ $leerling->voornaamLeerling . ' ' . $leerling->achternaamLeerling }}}</td>
<td>{{{ $leerling->klas->title }}}</td>
<td>
<button class="btn btn-blue">Wijzig</button>
<button class="btn btn-red">Verwijderen</button>
#if($leerling->active == 1)
<button class="btn btn-green">Actief</button>
#else
<button class="btn btn-red"> Niet Actief</button>
#endif
</td>
</tr>
#endforeach
#endif
Can someone help me with this ?
Thanks !
You should use in controller probably:
$leerlingen = Admin_Leerling::get();
or other model name to get your data, if you get data using query builder ( DB::table('leerling')->get();), it's not Eloquent object, so relations won't work
To get $lijst you can do
$lijst =Admin_Klas::find($id)->leerlingen;
Without () to get the relationship , and loop over $lijst in your view
#foreach( $lijst as $li ) {{$li->name}} #endforeach