Laravel - Display values with same value / number on the page - laravel

I have a page here which get all the value that has the same employee_no. here is my page.
the page id defines which employee's name , lastname and number to define. so for example I hve 10 users and i need to set those 10 users with their time in and outs so i need to have a dependent data for this page. that is why I need text and its value to get all the data which that user has.
here in my page, see the textbox? that is the data that im fetching and that will be my basis when I fetch all the records that has the same number.
so when i display that employee number, then all the data must be displayed. but on my case, no data is being displayed here take a look.
whereas, the final output should be this.
here is my view
{!! Form::open(['action' => 'Admin\EmployeeFilemController#insertSchedule', 'method' => 'POST']) !!}
{{Form::text('txtEmployeeNo', $employee->employee_no,['class' => 'form-control'])}}
{!! Form::close() !!}
here is my contoller
public function createSchedule(Request $request, $id)
{
$employee = Employeefm::find($id);
$employNum = $request->input('txtEmployeeNo');
$employeeSched = Schedule::where(['employee_no'=>$employNum])->get();
// dd($request->all);
return view('admin.employeemaintenance.createSchedule',compact('employee','employeeSched'));
}
my route
Route::get('/admin/employeemaintenance/{id}/createSchedule', 'Admin\EmployeeFilemController#createSchedule');
P.S I have displayed the data because i change the code $employNum
$employeeSched = Schedule::where(['employee_no'=>$employNum])->get();
to this a fixed number 54232
$employeeSched = Schedule::where(['employee_no'=>`54232'])->get();
and here is how i fetched it
#foreach ($employeeSched as $setTime)
<tr>
<td> {{Form::label('date_today', $setTime->date_today)}}</td>
<td> {{Form::label('time_in', $setTime->time_in)}}</td>
<td> {{Form::label('time_out', $setTime->time_out)}}</td>
</tr>
#endforeach

Related

Laravel send email to address from database

In my index view I show all the users and there is a button that will change the user status to active and not active. The code looks like this:
#foreach($users as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{$user->surname}}</td>
<td>{{$user->email}}</td>
<td>
#if($user->is_active == 0)
{!! Form::model($user, ['method' => 'PUT', 'action'=>['AdminUserController#activateuser', $user->id]]) !!}
{!! Form::submit('Activate', ['class'=>'btn btn-primary']) !!}
{!! Form::close() !!}
#else
{!! Form::model($user, ['method' => 'PUT', 'action'=>['AdminUserController#activateuser', $user->id]]) !!}
{!! Form::submit('De-Activate', ['class'=>'btn btn-danger']) !!}
{!! Form::close() !!}
#endif
</td>
<td>{{$user->cell}}</td>
<td><button class="btn btn-primary">View Property</button></td>
<td><button class="btn btn-danger">Delete</button></td>
</tr>
#endforeach
So when I click on activate/deactivate button I trigger my activateuser function of the controller. After activation, an email is sent.
The controller looks like this:
public function activateuser(Request $request, $id){
$user = User::findOrFail($id);
if($user->is_active == 0){
$user->update([$user->is_active = 1]);
Mail::send(new activateUser());
}
else{
$user->update([$user->is_active = 0]);
}
return redirect()->back();
}
At the moment the email is going to myself and my Mailabçe looks like this:
public function build()
{
return $this->view('emails.activateuser')->to('wosleybago#gmail.com');
}
What I want instead is to send the email to the email address from the user email in the database table.
How can I do that?
So, someho I should get the $user->email
I usually want my emails have all the information in itself, so I pass User instance or whatever instance that holds data required to compose the mail.
So the Mailable has __construct(..) like this:
/**
* #var \App\User
*/
public $user; // since this is a public property its going to be available in view of mailable as $user
__construct(App\User $user) {
$this->user = $user;
// further more I set the to(), this is what you are after
$this->to($user->email, $user->name);
// and subject
$this->subject('You are activated!');
}
...
And now all you need to do in the controller is the following:
Mail::send(new activateUser($user));
As mentioned above, $user is available in the mail-view so you can use it there as well:
Hi, {{ $user->name }},
...
Note: change the activateUser to ActivateUser to follow PSR-2
Class names MUST be declared in StudlyCaps.
I also use queued mails so I set the $timeout and $tries properties right on the Mailable class.
Sending email is described in Doc: https://laravel.com/docs/5.6/mail#sending-mail
Put this code inside activateUser() function
Mail::to($user)->send(new YourMailableName());
Do not forget to import Mail and YourMailableName using "use" keyword.
Or you can use user email instead object
Mail::to($user->email)->send(new YourMailableName());
And remove ->to('wosleybago#gmail.com') from your Mailable/
You should pass User Email in when creating new activateUser instance, like so
Mail::send(new activateUser($user->email));
And then use this attribute later.
Sending Email to particular a person is quite simple. My suggestion would be as follows:
Use Queue to send mail later as it will take some time to respond from controller to view.
In existing code you can get the email of the current user and send it using a helper to() that comes with mail functionality of laravel.
You can code it like this.
if($user->is_active == 0){
$user->update([$user->is_active = 1]);
Mail::to($user->email)->send(new MailableClassInstance);
}

Populate form without model in laravel

My application is made in laravel for a competition admin.
I have 'create' and 'edit' forms on Teams and Players. One team has multiple players.
I would like to link from the Team page to the 'create player' page. The Create Player page does not use a model (doesn't bind). How can I still prefill the select box with the team from the team page? Can I bind without saving a record in the database?
What should my routes be like?
Pass the team ID in the URL?
/players/create?team={teamId}
PlayersController#create method:
$teams = Team::all();
return view('players.create', compact('teams'));
players.create view:
<select name="team">
#foreach ($teams as $team) {
<option value="{{ $team->id }}"{{ $request->has('team') && $request->query('team') === $team->id ? ' selected' : '' }}>{{ $team->name }}</option>
#endforeach
</select>
You could make a route for example
// teamId is optional
Route::get('player/create/{teamId?}', ['as' => 'player_create', function ($teamId = null) {
// You can of course better do this logic in a controller!
// just an example :)
// check if $teamId is null here for example
// Or whatever logic you want to grab a team by
$team = Team::find($teamId);
$teams = Team::all();
// Again.. or whatever way you want to pass your data!
return view('player.create', ['teamName' => $team->name, 'teams' => $teams, 'whatever' => 'elseyouneed']);
}]);
And in the form of your view:
{!! Form::select('team', $teams, $teamName) !!}
Edit
since html isn't part of the core anymore, you can't use that out of the box so I suppose Chris' approach is better. You could however install a package for it.

Laravel 5 retrieve checkbox array value

I am creating project with laravel 5, i did store checkbox array value into table using below's code
$permission_id = Input::get('permission_id');
if(is_array($permission_id))
{
$permission_id = implode(',', $permission_id);
}
$userpermissionlist->permission_id = $permission_id ;
it's stored value like 2,3,4 now i need to explode this value and selected checkbox value will be checked.. how can i do that..My view code is
{!! Form::checkbox('permission_id[]', $userpermission->id) !!}
Setting the third parameter to true in checkbox() method will mark the checkbox as checked. Assuming you have $checked_permission_ids which is an array of all the checked ids, you could do this:
#foreach( $user_permissions as $userpermission )
{!! Form::checkbox('permission_id[]', $userpermission->id, in_array( $userpermission->id, $checked_permission_ids ) ) !!}
#endforeach

Laravel Filtering data between dates

I'm beginner in Laravel and web development and I have a silly question, I'm building a system that lists the user financial transactions. In the homepage is listed all the transactions made by the user. I put two fields up there to make the date filter of the transactions, but I don't know how to proceed this filter in Laravel.
I'm using datepicker and it is working fine, I also know that the validation system allows me to use Laravel date: after and date: before, but I don't know how and in which method in the controller send these dates. Here's my dates form code:
{!! Form::open(['route' => 'transactions.index']) !!}
{!! Form::label('data_inicio', 'De: ') !!}
{!! Form::input('date', 'data_inicio', null, ['class' => 'datepicker', 'data-date-format' => 'dd/mm/yy']) !!}
{!! Form::label('data_fim', 'Até: ') !!}
{!! Form::input('date', 'data_fim', null, ['class' => 'datepicker', 'data-date-format' => 'dd/mm/yy']) !!}
{!! Form::submit('Enviar') !!}
{!! Form::close() !!}
The method in the controller which calls the view of the homepage is the index, here's the index code:
public function index()
{
$transactions = Auth::user()->transactions;
return view('transactions.index', ['transactions' => $transactions]);
}
Here is how I show the data in the view:
#foreach( $transactions as $transaction )
<tr>
<td>{!! date('d-m-Y', strtotime($transaction->created_at)) !!}</td>
<td>{!! $transaction->title !!}</td>
<td>{!! $transaction->amount !!}</td>
</tr>
#endforeach
I need to send these dates I got to this same method (index) when the Pesquisar button was clicked to be able to seek in the database only the transactions of the proposed date, the problem is I do not know do this.
I tried to send these dates as parameter request to the index method, modifying it like this:
public function index(Request $request)
I got a Column Not Found error when I click in the submit, but I'm not using this request in any query in the method.
I really think that it has a simple way to do it and I appreciate any help!!
Here's my model class:
class Transaction extends Model {
protected $table = 'transactions';
protected $guarded = [];
public function users()
{
return $this->belongsTo('App\User');
}
}
Try this to obtain the dates in your controller:
$data_inicio = Input::get('data_inicio');
$data_fim = Input::get('data_fim');
Then you can use the dates to query your Transaction relation (assuming this a one to many relationship and is configured propperly)
$transactions = Transaction::whereBetween('created_at',[$data_inicio, $data_fim])->where('user_id',Auth::id())->get();

Laravel explain me how working query

I have a problem with query on laravel.
Please show me how it work, because I can't understand doc.
For example, I have VideoController.php and I have some data from forms:
$gall = array(
'name' => Input::get('name'),
'user_id' => Auth::id()
);
now I want to add this data to DB, but I don't know how to call to create function in model (and how this function should look).
And please explain me, how I should select data from database and display it on view, where for example user_id = 15;
In your VideoController.php file you should have a method to store the data, which you should post your data to through a form or something.
In a view, this form should look something like this:
{{ Form::open(['route' => 'video.store']) }} <!-- check your routes to see if video.store exists, you'll get an error otherwise -->
... form elements ...
{{ Form::submit('Submit') }}
{{ Form::close() }}
In your store() function you should have code similar to this
$video = new Video(); // if your video model is Video.php
$video->name = Input::get('name');
$video->user_id = Auth::id();
$video->save();
And if you want to display data, i.e. your video index or something, in your VideoController.php file, in the index() function:
$videos = Video::all();
return View::make('video.index', array('videos' => $videos));
then in your view
#foreach($videos as $video)
{{ $video->name }}
#endforeach

Resources