how to display My table data in Laravel 5.2 - php

I have collaborators table in My Laravel app see this following
I need collaborator_id print in My index.blade.php file who equel with Auth::user()->id to logged with the system. I wrote following code in My Collaboration Model
public function scopeColabo($query){
return $query->where('collaborator_id',Auth::user()->id);}
and this is My ProjectCollaboratorController function
public function index(){
$collaborators = Collaboration::colabo()->getreturn view('collaborators.index')->withCollaboration($collaborators);}
and this is My index.blade.php
<div class="container">
#if($collaboration)
<div class="row">
#foreach ($collaboration as $proj)
<div class="col-md-3" style="border:1px solid #ccc;margin-left:5px;">
<h2>{!! $proj->project_id !!}</h2>
<p>Tasks: 0</p>
<p>Comments: 0</p>
<p>Attachments: 0</p>
</div>
#endforeach
</div>
#endif
#if($collaboration->isEmpty())
<h3>There are currently no Collaboration</h3>
#endif
</div>
But when I click collaboration link index.blade.php file generate
There are currently no Projects
but in My table there is data....how can print collaborator_id in collaboration Table relete with current logged user?

Try to use ->with() instead of ->withCollaboration:
public function index() {
$collaborators = Collaboration::colabo()->get();
return view('collaborators.index')->with(compact('collaborators'));
}
or just pass your data as second parameter:
public function index() {
$collaborators = Collaboration::colabo()->get();
return view('collaborators.index', compact('collaborators'));
}

The problem is with the collaborator_id in the table which is used to log to test the system:
In the tests, in the logging account, data should match with collaborator data, so collaborator_id should match with the logged user id.

Related

Cannot find the way to make #if statement works in blade

I'm making a "teacher's" app, and I want to make a log-in page which changes depending if there's registered users in the database or not.
I want to make a redirection button to a create user page if there aren't auth users in database, and to make a select user view if the database have one or more users.
The problem is that I don't know how to exactly do this, 'cause the view always shows me the first statement (what I've got in the if), also if in the database are registered users. Can anyone help me with this please?
This is the blade file:
#if (empty(Auth::user()->id))
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
Here you have the controller index method:
public function index()
{
$users = User::all();
return view('/', compact('users'));
}
And finally here you have the page:
The following code is the sample for it, kindly replace code accordingly
#if(!$user)
//show button
#else
//dont show button
#endif
I think your question is you want to check if there is user in database.
So no need to check if the user authenticated but to check if there is user on the database.
In your controller
public function index() {
return view('/', ['users' => User::all()]);
}
and in your blade file
#if(!$users)
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
This function will get the current authenticated user: Auth::user(). I guess what you are trying to achieve is #if(empty($users)) where $users is the variable you are passing on controller.
If you want to verify if the user that accessed to that view is authenticated you can simply use #auth and #guest.
Also i would suggest you to change your button to an <a> tag and your href would be <a href="{{ route('route.name') }}" where route.name would be defined in your routes file.
in your controller:
you can create a folder inside views called users and then the index.blade.php (views/users/index.blade.php)
public function index()
{
$users = Users::all();
return view('users.index')->with('users', $users);
}
in your view:
#if(count($users) < 1)
...
#else
...
#endif
count is validating if the $users array length is less then 1 (in other words if the array is empty).
Alternative you can you isEmpty()
#if($users->isEmpty())
...
#else
...
#endif

Show.blade.php is not displaying the content from the database, only the layout

`i am having a problem with my show.blade.php template, everything works fine but when I click on a post in the index page it directs me to the /post/1 page without showing the post content only the extended layout. please help
Web.php
Route:: resource('best-practices' , 'BestpracticesController');
*bestpracticescontroller.php
public function index()
{
$bestpractices = Bestpractices::all();
return view('bp.index',compact('bestpractices'));
}
public function show(Bestpractices $bestpractices)
{
return view('bp.show',compact('bestpractices'));
}
bp.show view template
#extends('layouts.front')
#section('content')
<div class="blog-details pt-95 pb-100">
<div class="container">
<div class="row">
<div class="col-12">
<div class="blog-details-info">
<div class="blog-meta">
<ul>
<li>{{$bestpractices->Date}}</li>
</ul>
</div>
<h3>{{$bestpractices->title}} </h3>
<img src="{{asset('storage/'.$bestpractices->cover_img)}}" alt="">
<div class="blog-feature">
{{$bestpractices->body}}
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
Thats because when you register routes via
Route::resource('best-parctices', BestparcticeController');
//Generated show route is equivalent to
Route::get(
'/best-practices/{best_practice}',
[BestpracticeController::class, 'show']
);
//route parameter is best_practice
Hence to achieve implicit route model binding the route parameter name must match the parameter name in the controller method
public function show(Bestpractices $bestpractices)
{
//here $bestpractices will be an int and not an object with
//model record as implicit route model binding doesn't work
return view('bp.show',compact('bestpractices'));
}
public function show(Bestpractices $best_practice)
{
//here implicit route model binding works so $best_practices is an object
//with model record
return view('bp.show',['bestpractices' => $best_practices]);
}
Or if you don't want to change the method parameter name in the controller methods then you need to override the route parameter name in the Route:resource() call when you define routes
Route::resource('best-practices', BestpracticesController::class)
->parameters([
'best-practices' => 'bestpractices'
]);
Laravel docs: https://laravel.com/docs/8.x/controllers#restful-naming-resource-route-parameters

Q: how do I show, update, edit user profile by username in Laravel?

I have a custom authentication guard Applicant and already have created ApplicantsProfile model.
Problem 1 :
so far i am stuck with the show method, it is working but whatever ID i passed to the route it renders the view .
Problem 2 :
I want to access each applicant profile by it's ID
Problem 3 :
How do edit and update the authenticated user profile?
Profile Model
class ApplicantsProfile extends Model
{
protected $fillable = [
'job_title', 'applicant_id',
];
public function applicant()
{
return $this->belongsTo(Applicant::class);
}
}
Profile Controller
class ApplicantsProfileController extends Controller
{
public function __construct()
{
$this->middleware('auth:applicant');
}
public function show($id)
{
$profile = ApplicantsProfile::find($id);
return view('applicant.profile', compact('profile'));
}
}
Profile View
#extends('layouts.auth')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5">
<div class="card">
<div class="card-header">{{Auth::guard('applicant')->user()->name}}</div>
<div class="card-body">
{{Auth::guard('applicant')->user()->profile->job_title}}
</div>
</div>
</div>
</div>
</div>
#endsection
Route
Route::get('/account/{profile}', 'ApplicantsProfileController#show');
If I go to route: account/1 or account/2 it renders the view of the authenticated user.
in your profile view, you use Auth so it's normal to show these results.
you have to modify your view blade to something like :
#extends('layouts.layout')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5">
<div class="card">
<div class="card-header">
{{ $applicantsProfile['name'] }}
</div>
<!-- complete your code like so -->
in your controller you have to do something like:
public function show(ApplicantsProfile $applicantsProfile)
{
return view('applicant.profile', compact('applicantsProfile'));
}
and in your web.php route:
Route::get('/account/{applicantsProfile}', 'ApplicantsProfileController#show');

How to link two tables with a pivot table?

Hey so a bit of an overview of the project i want to do. I want to show the user's teams.
When the user logged in and opened the viewteams page. He must show teams he joined/created. To do so i ve done the below processes...
my ViewTeamController
{
public function index()
{
$user=User::first();
$teams=Team::all();
$user->teams()->attach($teams);
return view('teams.viewteams',compact('teams'));
}
public function store()
{
}
}
my User model
public function teams(){
return $this->belongsToMany(Team::class,'team_user','teams_id','users_id');
}
my Team model
public function users(){
return $this->belongsToMany(User::class,'team_user','teams_id','users_id');
}
my migration of the pivot table
public function up()
{
Schema::create('team_user', function (Blueprint $table) {
$table->unsignedBigInteger('users_id');
$table->unsignedBigInteger('teams_id');
$table->index('users_id');
$table->index('teams_id');
$table->timestamps();
});
}
web.php routes
Route::get('/viewteams','ViewTeamController#index');
Route::post('/viewteams','ViewTeamController#store');
my viewteams.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header"><h2 style="text-align:center;">Your Teams</h2></div>
<div class="card-body">
#foreach ($teams as $team)
#foreach($team->users as $user)
{{$user->org_name}}
#endforeach
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
my other controller for Creating a team
<?php
namespace App\Http\Controllers;
use App\Team;
use Illuminate\Http\Request;
class CreateTeamController extends Controller
{
public function index(Request $request)
{
return view('teams.createteams');
}
public function store(Request $request)
{
$team=Team::create($request->all());
return redirect()->route('home');
}
}
all my routes
Route::get('/login', function () {
return view('auth/login');
});
Auth::routes();
Route::get('/viewteams','ViewTeamController#index');
Route::post('/viewteams','ViewTeamController#store');
Route::get('/createteams','CreateTeamController#index');
Route::post('/createteams','CreateTeamController#store') ;
Route::get('/home', 'HomeController#index')->name('home');
To achieve currents users teams, first you should get authenticated user id using Auth facade.
After that you can load joined teams for user, using 'with' method. It loads teams relationships.
ViewTeamController
public function index()
{
$user = User::with('teams')->find(Auth::id());
return view('teams.viewteams',compact('user'))
}
viewteams.blade.php
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header"><h2 style="text-align:center;">Your Teams</h2></div>
<div class="card-body">
#foreach ($user->teams as $team)
{{ $team->name }}
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
You will need to revisit and update the code which stores data to the users table and the teams user. The easiest way to store the data in a way which populates the pivot table is to use the sync() method as described at https://laravel.com/docs/master/eloquent-relationships#updating-many-to-many-relationships
I'm making some assumptions about names and relationships, buy you may have something like this:
$user = User::find(2);
$user->teams()->sync([4,5]);
In this case, you would put into the pivot table a record for user_id of 2 and team_id of 4, and a second record for a user_id of 2 and a team_id of 5.
If you want to share more code showing route definitions and controllers for how you are currently handling things, we can help with any more specific implementation questions.

Determine whether user profile belongs to current user in Laravel

In my application, I have the concept of a user profile. The information that gets displayed differs depending on whether the user is viewing their own profile or another user's profile. Here's a simplified view of UsersController#show:
public function show($id)
{
$user = User::findOrFail($id);
$currentUser = Auth::user();
return view ('users.show', compact('user', 'currentUser'));
}
In my view, I end up having to write code that looks like:
#if ($currentUser === $user->id)
<section class="container search-form visible-nav">
<div class="row">
<div class="col-xs-12">
#include ('partials._search')
</div>
</div>
</section>
#endif
This seems like a clumsy implementation, especially for a language like Laravel. Is there a more concise way to achieve the same result in my views?
Not really. You need to check if current user is the viewed user somewhere - either in the controller or in the view.
You could simplify your code a bit though:
public function show($id)
{
$user = User::findOrFail($id);
return view ('users.show', compact('user'));
}
#if (Auth::id() === $user->id)
<section class="container search-form visible-nav">
<div class="row">
<div class="col-xs-12">
#include ('partials._search')
</div>
</div>
</section>
#endif
There are some other options like returning different blade templates depending on whether the current user is the same as the viewed user, but if the only difference would be a few #ifs I would keep it in one template.

Categories