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.
Related
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');
I am quite new to laravel so this might be silly mistake but I just can't figure out why it gives me this error. Right so on my website users can create posts and other users can like those posts. However, my implementation of the like system throws the following error:
ErrorException (E_ERROR)
Method Illuminate\Database\Eloquent\Collection::likes does not exist. (View: C:\xampp\htdocs\eventcw\resources\views\eventspage.blade.php)
This is my post controller method in charge of the likes:
public function postLikePost($post_id){
$loggedin_user = Auth::user()->id;
$like_user = Like::where(['user_id' => $loggedin_user, 'post_id' => $post_id])->first();
if(empty($like_user->user_id)){
$user_id = Auth::user()->id;
$post_id = $post_id;
$like = new Like;
$like->user_id = $user_id;
$like->post_id = $post_id;
$like->save();
return redirect()->route('events');
}else{
return redirect()->route('events');
}
}
My database relations seem fine,
here is my Like model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Like extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function post(){
return $this->belongsTo('App\Post');
}
}
Here is my likes table migration:
Schema::create('likes', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id');
$table->integer('user_id');
$table->timestamps();
});
Here is my post view:
<section class="row posts">
#foreach($posts as $post)
<div class="col-md-2 col-md-offset-3">
<article class="post">
<p>{{ $post->body }}</p>
<div class="info">Posted by {{ $post->user->first_name }} {{ $post->user->last_name }} on {{ $post->created_at }}</div>
<p>This post has {{ $posts->likes()->count() }} likes </p>
Like|
</article>
</div>
#endforeach
</section>
The error indicates you are calling likes() directly on a Collection.
$posts is the collection, which you are iterating over in your blade template.
Change {{ $posts->likes()->count() }} to {{ $post->likes()->count() }}
I am aiming on retrieving the user image (from profile table column 'profile_img') and display on each post's footer.
I can retrieve the name of the author using $post->author->name and the profile picture using $post->author->profile->profile_image but it only works when i have a single record (post). when there is more than one record i get an error Trying to get property 'profile' of non-object (View: xampp/........../home.blade.php)
Can somebody show me where do i go wrong??
Models:
User
public function post()
{
return $this->hasMany('App\Post');
}
public function profile()
{
return $this->hasOne('App\Profile');
}
Profile
public function user()
{
return $this->belongsTo('App\User');
}
Post
public function author()
{
return $this->belongsTo('App\User', 'id');
}
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class MainController extends Controller
{
public function index() {
$posts = Post::paginate(9);
return view('pages.home')->with('posts', $posts);
}
home view
#if(count($posts) > 0)
#foreach($posts as$posts)
<div>
<div class="post-title"><h3>{{$post->title}}</h3></div>
<div class="post-description">
{!!mb_substr($post>body,10,rand(35,40)) !!} ....
</div>
<div class="featured-details">
<div class="p-clearfix">
<img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
<div class="author-title lite">{{ $post->author->name }}</div>
<div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
</div>
</div>
</div>
<hr>
#endforeach
#else
no post yet
#endif
There appears to be some issues in your home view. Try it again with this code and see.
Home View
#if(count($posts) > 0)
#foreach($posts as $post)
<div>
<div class="post-title"><h3>{{$post->title}}</h3></div>
<div class="post-description">
{!!mb_substr($post>body,10,rand(35,40)) !!} ....
</div>
<div class="featured-details">
<div class="p-clearfix">
<img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
<div class="author-title lite">{{ $post->author->name }}</div>
<div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
</div>
</div>
</div>
<hr>
#endforeach
#else
no post yet
#endif
You can even go further and avoid the n+1 problem for authors by running your eloquent model query like this om your controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class MainController extends Controller
{
public function index() {
$posts = Post::with("author")->paginate(9);
return view('pages.home')->with('posts', $posts);
}
I hope this helps.
In your controller index method try with :
public function index() {
$posts = Post::with("author.profile")->paginate(9);
return view('pages.home')->with('posts', $posts);
}
And update post model relationship:
public function author()
{
return $this->belongsTo('App\User', 'user_id'); // update to users table foreign key
}
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.
I have 2 table as Food:
Food_id(pk)
Foodname
Description
Image
Price
Ans other table is Restaurant:
Res_id(pk)
ResName
Location
I make Pivot table food_restaurant as:
id
Food_id(fk)
Res_id(fk)
I want to show Location of Restaurant along with food details in my view.For that I make function in model Food.php as
public function restaurant()
{
return $this->belongsToMany('App\Restaurant','food_restaurant','Food_id','Res_id');
}
And other model Restaurant.php as
public function food()
{
return $this->belongsToMany('App\Food','food_restaurant','Res_id','Food_id');
}
My Controller is:
class Detailscontroller extends Controller
{
public function index()
{
$Foods= Food::all();
return view('welcome', compact('Foods'));
}
public function show($Food_id)
{
$food =Food::findOrFail($Food_id);
return view('show', compact('food'));
}
}
And Routes are:
Route::get('/','DetailsController#index');
Route::get('/{Food_id}', 'DetailsController#show');
View welcome.blade.php is:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
#foreach($Foods as $Food)
<p><img src="{{ asset('uploads/'.$Food->Image) }}" /><p>
<Food>
<h2>
<a href="/{{$Food->Food_id}}">{{$Food->FoodName}}
<i class="fa fa-chevron-circle-right"></i></a
</h2>
</Food>
#endforeach
</div>
</div>
#endsection
And show.blade.php is:
#extends('layouts.app')
#section('content')
<h1 align="center">{{$food -> FoodName}}</h1>
<p><img src="{{ asset('uploads/'.$food->Image) }}" /><p>
<detail>
<h3><p>FoodDescription:</h3><h4>{{$food->Description}}</h4></p>
<h3><p>Price:</h3><h4>{{$food->Price}}</h4></p>
#foreach ($food->restaurant as $restaurant)
<h3><p>Location:</h3><h4>{{$restaurant->Location}}</p></h4>
#endforeach
#endsection
But it does not show Location to me.How can I show Location in my show.blade.php view?I am new to laravel.Where is the problem?
return the food array to the view with associated restaurant.
public function show($Food_id)
{
$food = Food::with('restaurant')->findOrFail($Food_id);
return view('show', compact('food'));
}