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);
}
Related
I have three model one is pivot table (grade_studentattendances) and other model are (grades and studentattendances ). i just want to show data of student from one grade to eight grade only.
my view code is:
<table border="1">
<tr>
<th>Id</th>
<th>Full name</th>
<th>Roll no</th>
<th>Date</th>
<th>Status</th>
<th>Grade</th>
</tr>
#php
$i=1;
#endphp
#foreach($studentattendance as $item)
#php $grades=$item->Grade; #endphp
<tr>
<th>{{$item->id }}</th>
<th>{{$item->name}} </th>
<th>{{$item->roll_no}}</th>
<th>{{$item->date}}</th>
<th>{{$item-> status}}</th>
<th>
#foreach($grades as $grade)
{{$grade->class_name }}
#endforeach
</th>
</tr>
#endforeach
</table>
my controller is:
public function sattend()
{
$studentattendance=studentattendance::all();
return view('primaryhead.s_attend',compact('studentattendance'));
}
i just want to show the of students from grade one to grade eight in the form of table
I'm working with Laravel 8 to develop my project and in this project, I have a table named hostings which it's migration goes here:
public function up()
{
Schema::create('hostings', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
And now I want to return some results from the DB and show them on blade, so I added this:
<table>
<tr>
<th>Hosting Name:</th>
<td>{{ $hosting->name }}</td>
</tr>
</table>
But now I get this error:
Property [name] does not exist on this collection instance
So what is going wrong here? How can I fix this issue?
I would really appreciate if you share any idea or suggestion about this with me,
Thanks in advance.
Controller code:
public function index()
{
$hosting = Hosting::all();
return view('admin.index', compact('hosting'));
}
as you have collection you need to loop over it to get value
<table>
<tr>
#foreach ($hosting as $host)
<th>Hosting Name:</th>
<td>{{ $host->name }}</td>
#endforeach
</tr>
</table>
else you can show all the host name in comma separated by this
<table>
<tr>
<th>Hosting Name:</th>
<td>{{ $hosting->pluck('name')->join(',') }}</td>
</tr>
</table>
or you can get first data of collection like this
<table>
<tr>
<th>Hosting Name:</th>
<td>{{ $hosting->first()->name }}</td>
</tr>
</table>
ref
link https://laravel.com/docs/8.x/collections#method-pluck
https://laravel.com/docs/8.x/collections#method-join
try this:
<table>
<tr>
#foreach($hosting as $h)
<th>Hosting Name:</th>
<td>{{ $h->name }}</td>
#endforeach
</tr>
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....
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();
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