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();
Related
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);
}
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);
}
The admin of the application has access to list each projects of a user. Write now I am able to list the projects but they all are listed together no matter the unique id of the URL.
// Admin form case controller
public function adminforms (Project $project){
$users = User::get();
return view('smiledesign.adminforms', compact('users', 'project'));
}
///Records controller
public function records(user $users, project $project){
$project = project::get();
$users = user::get();
return view ('/smiledesign/records' , compact( 'users','project'));
}
<table class="table">
<thead>
<tr>
<th>Dr Name</th>
<th>User Id</th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td> {{$user->name}}</td>
<td> {{$user->id}}</td>
<td> {{$user->email}}</td>
</tr>
</tbody>
#endforeach
</table>
// Records view
<table class="table table-striped table-bordered" id="table_id">
<thead>
<tr>
<th>Case Number</th>
<th>Case Form</th>
<th>Patient Name</th>
<th>Date Created</th>
<th>Status</th>
</tr>
</thead>
#foreach ($project as $project)
<tbody>
<tr>
<td> {{$project->case_number}}</td>
#if ($project->services0)
<td> {{$project->services0}}</td>
#elseif ($project->services1)
<td> {{$project->services1}}</td>
#elseif ($project->services2)
{{-- <td> {{$project->services2 . ' ' . $project->mockup0}}</td> --}}
<td> {{$project->services2 . ' ' . $project->mockup0 . ' ' . $project->mockup1}}</td>
#endif
<td> {{$project->first_name . ' ' . $project->last_name}}</td>
<td> {{$project->created_at}}</td>
<td> {{$project->concerns}}</td>
</tr>
</tbody>
#endforeach
</table>
What I wanna see is each project of a specific user when I click The link from the adminforms user. Instead, every link I go I see all projects of all users
The relationship between the projects and users should be defined in the database and in the models themselves.
Database:
In my interpretation, a user can work on multiple projects and a project can have multiple users, this would be a many to many relationship. With this conclusion, I would go for an abstracted table setup like so:
table users has an id field.
table projects has an id field.
table project_users has an id field(always required for laravel), a user_id field(foreign key to users.id) and a project_id field(foreign key to projects.id). Do not forget to add a unique key which combines user_id and project id, there is no need for duplicate definitions.
Model relationships:
Project
function users()
{
$this->belongsToMany(User::class, 'project_users', 'project_id', 'user_id');
}
User
function projects()
{
$this->belongsToMany(Project::class, 'project_users', 'user_id', 'project_id');
}
If you read this piece of documentation, it will all make sense.
Query
$users = User::with('projects')->get();
or
$projects = Project:with('users')->get();
Now every user instance has a projects collection. If they do not, then you need to add a record to the database coupling a project to that user.
Showing results
foreach($users as $user)
{
foreach($user->projects as $project)
{
echo "$user->name works on project $project->name" . PHP_EOL;
}
}
or
foreach($projects as $project)
{
foreach($project->users as $user)
{
echo "$user->name works on project $project->name" . PHP_EOL;
}
}
Sidenote
My instructions are optimized for performance. You could also just call ->projects on the $user without using the with('projects') in the query, but that will mean that you will trigger a query on the spot. As this kind of code is prone to being used in foreach statements, you could be doing queries in foreach statements which is referred to a N+1 query problem.
There is two tables (one is Users and second is Branches). I want to show the Branch with its Branch Admin Name.And the branch Admin info is save in users table. There is an error When i am trying to show the data of Two table in one view. Tell me How i manage this issue.
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>
BranchName
</th>
<th>
BranchAdmin
</th>
<th>
Email
</th>
<th>
Contact
</th>
<th>
Location
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
#foreach($branch as $brnch)
<tr class="branchies-row">
<td>
{{$brnch->branch_name}}
</td>
#foreach($user as $users)
<td>
{{$users->name}}
</td>
#endforeach
<td>
{{$brnch->email}}
</td>
<td>
{{$brnch->contact}}
</td>
<td>
{{$brnch->address}}
</td>
<td>
<a data-id="{{$brnch->id}}" class="delete-branch">Delete</a> /
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Controller:
public function getBranchinfo(){
$user = User::where('type', '=', 'BranchAdmin');
$branch = Branch::all();
return view('Branch.branchinfo')->with('branch',$branch)->with('user', $user);
}
Model:
public function branch(){
return $this->hasOne('App\Branch', 'user_id', 'id');
}
We can use query builder - Join - for that :
$branches = DB::table('Branches')
->join('users', 'users.id', '=', 'Branches.user_id')
->where('users.type', '=', 'BranchAdmin')
->get();
and this should give you the data you need.
You have to make the relationship in branch model:
public function user(){
return $this->belongsTo('App\User');
}
Then in blade file you get branch admin name as:
#foreach($branch as $brnch)
<tr class="branchies-row">
<td>{{ $brnch->user->branchadmincloumn }}</td>
</tr>
#endforeach
branchadmincloumn is the cloumn name from your users table where your branch admin name is saving. So change branchadmincloumn to your desired column name.
Hope it helps..
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