Make survey at laravel 5.4 and MySQL - php

I need to make a survey for my website. I have this:
Route:
Route::resource('survey','surveyController');
Controller:
public function index()
{
$show=survey_title::find(1);
return view('survey',compact('show'));
}
Here we just read first survey.
Migrations:
Schema::create('survey_titles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});
Schema::create('survey_questions', function (Blueprint $table) {
$table->increments('id');
$table->integer('survey_title_id')->unsigned()->index();
//$table->integer('user_id')->unsigned()->index();
$table->foreign('survey_title_id')->references('id')->on('survey_titles')->onDelete('cascade')->onUpdate('cascade');
// $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('answer');
$table->integer('voted')->nullable();
$table->timestamps();
});
Models:
survey_question:
public function survey_titles(){
return $this->hasOne('App\survey_title');
}
public function users(){
return $this->hasOne('App\User');
}
survey_title:
public function survey_questions(){
return $this->hasMany('App\survey_question');
}
user:
public function survey_questions(){
return $this->hasOne('App\survey_question');
}
View:
<div class="col-md-8 col-md-offset-2">
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>Voted by 0 People </th>
<th>{{$show->title}}</th>
<th>Question ID: {{$show->id}}</th>
</tr>
<tr>
<th>Check</th>
<th>Answer</th>
<th>ID</th>
</tr>
</thead>
<tbody>
#foreach($show->survey_questions as $ans)
<tr>
<td><input type="radio" name="check[]"></td>
<td>{{$ans['answer']}}</td>
<td>{{$ans['id']}}:</td>
</tr>
#endforeach
</tbody>
</table>
<a class="btn btn-primary" href="{{action('surveyController#update',$ans->id)}}">Vote</a>
</div>
Now I put data manually in database and I retrieve.
When click on my 'Vote' I want it to send my Answer Id but instead it always sends last ID from table.
What's wrong?

This is happening because the:
<a class="btn btn-primary" href="{{action('surveyController#update',$ans->id)}}">Vote</a>
is out of the foreach loop, so after the loop is finished, the last id is being set to the link.
Put the href link inside the foreach loop, so it generates a link for each answer
<tr>
<th>Voted by 0 People </th>
<th>{{$show->title}}</th>
<th>Question ID: {{$show->id}}</th>
<th>Vote</th>
</tr>
#foreach($show->survey_questions as $ans)
<tr>
<td><input type="radio" name="check[]"></td>
<td>{{$ans['answer']}}</td>
<td>{{$ans['id']}}:</td>
<td><a class="btn btn-primary" href="{{action('surveyController#update',$ans->id)}}">Vote</a></td>
</tr>
#endforeach

Related

How to get the name of user through relations in Laravel

I want to implement simple teaming option to my app. I have theese tables:
users/teams /team_members/
-----/---------/------------/
id /id /id /
name /user_id /user_id /
... /... /team_id /
I have 3 models where I have defined relations:
User model:
public function teams() {
return $this->hasOne(Team::class);
}
public function teamMembers() {
return $this->hasManyThrough(Team::class, TeamMembers::class);
}
Team model:
public function user() {
return $this->belongsTo(User::class);
}
public function members() {
return $this->hasMany(TeamMember::class);
}
TeamMember model:
public function user() {
return $this->belongsTo(User::class);
}
User can only create one team, but join many teams. The problem I have is with displaying data in the frontend.
This is the query I make in the controller:
public function index(User $user) {
$teams = Team::where('user_id', auth()->user()->id )->with('members')->get();
return view('teams.index')->with('teams', $teams);
}
I have a table with the members of the current autheticated user team. I want to display the names if each member but the only thing i can do is $team->member->id and I get the ids of the users but I want to get their names too. I can do it with simple query but I dont want to make new query for every row.
This is the blade file:
#if ( $teams->count() > 0 )
#foreach ($teams as $team)
<table class="table table-striped table-sm table-responsive-lg">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Members</th>
<th scope="col">Created</th>
<th scope="col" colspan="2"></th>
</tr>
</thead>
<tbody>
<tr>
<td>{{$team->name}}</td>
<td>{{ $team->members->count('members')}} {{ Str::plural('member', $team->id) }}</td>
<td>{{ $team->created_at->diffForHumans() }}</td>
<td>
Edit
</td>
<td>
<form action="" method="POST">
#method('DELETE')
#csrf
<button class="btn btn-sm btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
</tbody>
</table>
<br>
<h5>Team members:</h5>
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Added</th>
</tr>
</thead>
<tbody>
#foreach ($team->members as $member)
<tr>
<td>{{ $member->user->name }}</td>
<td>{{ $member->created_at->diffForHumans() }}</td>
</tr>
#endforeach
</tbody>
</table>
#endforeach
#else
<div class="container">
<h2>You do not own a team</h2>
<p class="lead text-muted">You can create your own team or join the team of other user.</p>
<p>
Create team
Join team
</p>
</div>
#endif
When I changed $team->member->id with $member->user->name it worked. It shows the names of the users but I get N+1 queries alert and I don't know how to fix it (i tried different queries and relations but it didnt work):
You are accessing the user relationship from each member. So lets eager load that relationship as well.
public function index(User $user)
{
$teams = Team::where('user_id', auth()->user()->id )->with('members.user')->get();
return view('teams.index')->with('teams', $teams);
}

Laravel I want to display only employees in table

Hi I'm building a human resources management system, I want to display only users that are not admin in a data table but I'm getting error dont know what I might be doing wrong. Here is my code;
<table class="table table-hover">
<thead>
<th>Username</th>
<th>Department</th>
<th>Salary</th>
</thead>
<tbody>
#foreach($this->$users()->get() as $user)
#if($user->is_admin !== 1)
<tr>
<td>{{$user->username}} </td>
<td>{{$user->department}} </td>
<td>{{$user->salary}} </td>
</tr>
#endif
#endforeach
</tbody>
Table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->boolean('is_admin')->nullable();
$table->string('department')->nullable();
$table->integer('salary')->nullable();
$table->string('image',255)->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
you can try #continue to skip loop
<table class="table table-hover">
<thead>
<th>Username</th>
<th>Department</th>
<th>Salary</th>
</thead>
<tbody>
#foreach($this->$users()->get() as $user)
#php
if($user->is_admin == 1){
#continue
}
#endphp
<tr>
<td>{{$user->username}} </td>
<td>{{$user->department}} </td>
<td>{{$user->salary}} </td>
</tr>
#endforeach
</tbody>
Instead of looping through the users table and checking every user to see if they are an admin user, you can extend your query by only selecting users that aren't admin users in the first place.
where('is_admin', '!=' 1)
This way you'll already have all the users that you want to show and you won't have to add extra logic to the view.
I had forgotten to write code in the controller
public function addEmployee()
{
$users = User::all();
return view('admin/addEmployee')->with('users',$users);
}

How to add custom actions against any button in Laravel?

I m making a web app in which there are two tables: classes & sections. My migrations are shown below.
Classes
Schema::create('classes', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});
Sections
Schema::create('sections', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('class_id')->unsigned();
$table->foreign('class_id')->references('id')->on('classes');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
In my Blade file, I want to show all the buttons of classes. When I click on that button it should show all the sections where class_id in section table is of the selected button.
I'm stuck because of the syntax. I'm new to Laravel and I'm unaware of how to add a response against the button.
My controller for section
public function index()
{
$sections = Section::all();
$class = Class::all();
$users = User::all();
return view('sections.index')->with('sections', $sections)->with('class', $class)->with('users', $users);
}
Blade
#foreach($classs as $cat)
<button class="btn btn-info">{{$cat->title}}</button>
//this shows all buttons with class name
#endforeach
<div class="box-body">
<table class="table table-responsive">
<thead>
<tr>
<th>Section Name</th>
<th>Class</th>
<th>Teacher</th>
<th>Modify</th>
</tr>
</thead>
How can I get all sections of only selected class under these tables?
This is code as suggested in which I have made changes, I m not getting data when click on button. Kinndly tell me what should I do ? Do I have to change controller
#foreach($classs as $cat)
<button class="btn btn-info class_btn" id="{{$cat->class_id}}">{{$cat->title}}</button>
<div class="box-body" style="display:none" id="table_{{$cat->class_id}}">
<table class="table table-responsive">
<thead>
<tr>
<th>Section Name</th>
<th>Class</th>
<th>Teacher</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
#foreach($sections as $sec)
<tr>
<td>{{$sec->title}}</td>
<td>
{{ \Illuminate\Support\Facades\DB::table('classses')->where('id',$sec->class_id)->value('title')}}
</td>
<td>
{{ \Illuminate\Support\Facades\DB::table('users')->where('id',$sec->user_id)->value('name')}}
</td>
<td>
<button class="btn btn-info" data-mytitle="{{$sec->title}}" data-myclassid="{{$sec->class_id}}" data-myuserid="{{$sec->user_id}}" data-secid={{$sec->id}} data-toggle="modal" data-target="#edit">Edit</button>
/
<button class="btn btn-danger" data-secid={{$sec->id}} data-toggle="modal" data-target="#delete">Delete</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endforeach
<script>
//onclick show particular table
$('.class_btn').on('click',function(){
$('.box-body').hide();
$('#table_'+$(this).attr('class_id')).show();
});
</script>
You Use Jquery And hide() ,show() function show particular table to particular class Like As
#foreach($class as $cat)
<button class="btn btn-info class_btn" id="{{Your_class_id}}">{{$cat->title}}</button>
<div class="box-body" style="display:none" id="table_{{Your_class_id}}">
<table class="table table-responsive">
<thead>
<tr>
<th>Section Name</th>
<th>Class</th>
<th>Teacher</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
//print your data
</tbody>
</table>
</div>
#endforeach
<script>
//onclick show particular table
$('.class_btn').on('click',function(){
$('.box-body').hide();
$('#table_'+$(this).attr('id')).show();
});
<script>
Another Way You Use Ajax....

How can I rearrange this table to another away laravel eloquent relation

I used laravel hasmany relationship in my user model. I just want to convert my table like below (row to column):
To:
Here is my User model:
public function meal(){
return $this->hasMany(Meal::class);
}
My controller:
public function index()
{
$members = User::all();
return view('admin.meal')->with([
'members' => $members,
]);
}
and #blade
<table class="table table-striped #if(count($members)>7) table-responsive #endif ">
<thead>
<tr class="text-uppercase">
{{--<th class="font-w700">Date</th>--}}
#foreach($members as $member)
<th class="font-w700">{{str_limit($member->fname,5,'...')}}</th>
#endforeach
</tr>
</thead>
<tbody>
#foreach($members as $member)
<tr>
#foreach($member->meal as $meal)
<td><span class="font-w600">{{$meal->user_id}}</span></td>
#endforeach
</tr>
#endforeach
</tbody>
</table>

how to manage the id's using laravel

I am join two tables (one is User and the second table is branch) and print the BranchAdmin name with its branch in the single view, the branchAdmin information is save in the User table, but when i perform the edit function on the branch data, it will show an empty page and only show the edit form when users and branch id is match. So how i manage that when i edit the branch it only concern with the branch id not with the company id that is foreign key in the branch?
Branch Controller:
public function getBranchinfo(){
$branch = DB::table('branch')
->join('users', 'users.id', '=', 'branch.user_id')
->where('users.type', '=', 'BranchAdmin')
->get();
return view('Branch.branchinfo')->with('branch',$branch);
}
BranchDetails View:
<div class="branches col-xs-12 col-sm-12 col-md-9 col-lg-9">
<input type="text" class="pull-right form-control search" placeholder="Search">
<div class="spaces"></div>
<table class="table table-bordered">
<thead>
<tr>
<th>
id
</th>
<th>
Branch-Name
</th>
<th>
AdminName
</th>
<th>
Email
</th>
<th>
Contact
</th>
<th>
Location
</th>
<th>
OpenHours
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
#foreach($branch as $brnch)
<tr class="branchies-row">
<td>
{{$brnch->id}}
</td>
<td>
{{$brnch->branch_name}}
</td>
<td>
Note->(In this i will take the name of user who is Admin of branch.) like this ($brnch->user_name)
{{$brnch->user_id}}
</td>
<td>
{{$brnch->email}}
</td>
<td>
{{$brnch->contact}}
</td>
<td>
{{$brnch->address}}
</td>
<td>
{{$brnch->open_hours}}
</td>
<td>
<a data-id="{{$brnch->id}}" class="delete-branch">Delete</a> /
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Branch Migration:
public function up()
{
Schema::create('branch', function (Blueprint $table) {
$table->increments('id');
$table->integer('company_id')->unsigned();
// here you need to define on deleteCascade, when this records delete, this will also
// delete it's records from other tables that related to this one via a foriegn key.
$table->foreign('company_id')->references('id')->on('company')->onDelete('cascade');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->String('branch_name');
$table->String('email');
$table->String('address');
$table->String('contact');
$table->String('open_hours');
$table->timestamps();
});
}
User Model:
public function branch(){
return $this->hasOne('App\Branch', 'user_id', 'id');
}
i think that the problem is that the two table have a field called id so if you wanna remove the conflict you can just inverse the order of your join so the last id you get from the query is the branch id
$branch = DB::table('user')
->join('branch', 'branch.user_id', '=', 'users.id')
->where('users.type', '=', 'BranchAdmin')
->get();
you can add aliases like this
$branch = DB::table('user')
->select(DB::raw('brunch.*, brunch.id as branch_id, user.*'))
->join('branch', 'branch.user_id', '=', 'users.id')
->where('users.type', '=', 'BranchAdmin')
->get();

Categories