Laravel scope dynamic - laravel

I need to get logged user notifications by scope but it returns App\User::notifications must return a relationship instance.
Code
layout
#auth
#if(auth::user()->notifications > 0)
<div class="alert alert-success alert-dismissible">
×
<ul>
#foreach (auth::user()->notifications as $notification)
<li>{{$notification->subject}}</li>
#endforeach
</ul>
<strong>Success!</strong> Indicates a successful or positive action.
</div>
#endif
#endauth
User model
public function notifications() {
// return $this->hasMany(ProjectBroadcastApplicant::class);
$notifications = ProjectBroadcastApplicant::where('user_id', $this->id)->get();
return $notifications;
}
ProjectBroadcastApplicant model
public function user() {
return $this->belongsTo(User::class);
}
Where did I make mistake?!

Update your user Model:
public function notifications() {
return $this->hasMany(ProjectBroadcastApplicant::class);
}
If you want to filter the notification for the user then you can add condition with the relationship definition.

Solved
I've changed my user model to this
public function notifications() {
return ProjectBroadcastApplicant::where('user_id', $this->id)->get();
}
and my blade from #if(auth::user()->notifications > 0) to #if(auth::user()->notifications() > 0)
now it's working.

Related

how to display the username of the comment in the post with relation table

my relational database image
{{ \App\User::where('id', $comment->user_id)->value('name') }}
if I use the code above
name user show in all post
my model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Komen extends Model
{
protected $table='komentar_post';
}
my controller
public function index()
{
$post=Post::all();
$comment=Komen::all();
$user=User::all();
// dd($id);
return view('home',compact('post','comment','user'));
}
my view
#foreach($comments as $cm)
{{ \App\User::where('id', $cm->user_id)->value('name') }}
#endforeach
what the corect query i must use
You need to create relationship in your Komen model. It's something like this:
class Komen extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
Then, you can Eager Load that relationship when fetching the Komen.
PostsController.php
public function show(Post $post)
{
$comments = $post->comments()->with('user')->paginate();
return view('posts.show', compact('comments'));
}
Then, you can access user data when displaying each comments:
posts/show.blade.php
#foreach($comments as $comment)
{{ $comment->user->name }}
#endforeach
{!! $comments->links() !!}
Update
Ok, if you need to displaying comments when you're displaying all posts, first you need to setup the relationship!
Post.php
class Post extends Model
{
public function comments()
{
return $this->hasMany(Komen::class, 'post_id');
}
}
Komen.php
class Komen extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
PostsController.php
public function index()
{
$posts = Post::with('comments.user')->paginate();
return view('home', compact('posts'));
}
home.blade.php
#foreach($posts as $post)
#foreach($post->comments as $comment)
{{ $comment->user->name }}
#endforeach
#endforeach
{!! $posts->links() !!}

Display reviews for a specific restaurant Laravel

I am trying to display reviews and ratings on a restaurant profile.
<div>
<h1>Reviews</h1>
#foreach($reviews as $review)
<hr>
<div class="row">
<div class="col-md-12">
#for ($i=1; $i <= 5 ; $i++)
<span class="glyphicon glyphicon-star{{ ($i <= $review->rating) ? '' : '-empty'}}"></span>
#endfor
{{ $review->user ? $review->user->name : 'Anonymous'}}
<p>{{{$review->value}}}</p>
</div>
</div>
#endforeach
</div>
Error
Undefined variable: reviews (View:
C:\xampp\htdocs\restaurantFinder\resources\views\restaurants\viewer.blade.php)
Reviews Controller
class ReviewsController extends Controller
{
public function index()
{
//
}
public function create()
{
//
}
public function store(Request $request)
{
if (!Auth::check()) {
return redirect('/index');
}
$review = new Review;
$review->user_id = auth()->user()->id;
$review->restaurant_id = $request->get('restaurant_id');
$review->value = $request->input('value');
$review->rating = $request->input('rating');
$review->save();
}
public function show($restaurant)
{
// $restaurant=Restaurant::find($id);
return view('restaurants.review', compact('restaurant'));
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
}
you are looking in the wrong place. to track the problem start with your route file. Search for the URL in the web.php file you will find the route there. ones you get the route, you will also get which controller and action are making this page render.
this issue is shown when you use a variable in a blade template but do not set it in the controller for use. In the controller, you can set this variable and you are good to go.
hope this help. incase of any issue feel free to ask.
update your ReviewsController.php
public function show($restaurantID)
{
$reviews = Reviews::find($restaurantID); // Find all reviews model by restaurantID
return view('viewer',compact('reviews ')); // This will do $reviews = reviews
// now you can foreach over reviews in your blade
}
this Link is useful

How to show comments with commented user name and photo

I'm trying to show the name of user alongside with their comment, as Tour does not belong to a user, I'm facing [user] issue here. Failed to pass the user information with comments. In my code, I can show only comments that belong to tour but not the users who comment.
Tour
class Tour extends Model{
protected $table = 'tour';
public function disTour()
{
return $this->hasMany('App\District');
}
public function review()
{
return $this->hasMany(TourReview::class);
}
TourReview Model
class TourReview extends Model{
protected $table = 'tour_review';
public function tour()
{
return $this->belongsTo('App\Tour');
}
public function user()
{
return $this->belongsTo('App\Users');
}
Users Model
class Users extends Model{
protected $table = 'users';
public function userBlogs()
{
return $this->hasMany('App\Blog');
}
public function tourReview()
{
return $this->hasMany('App\TourReview');
}
Controller
public function singleDetails($id)
{
$tour = Tour::find($id);
$comments = Tour::find($id)->review;
$users = TourReview::with('user')->where('id', $comments->pluck('id'))->get();
foreach ($users as $user){
dd($user);
}
//$blogs = Blog::with('images')->where('user_id', $user_id)->paginate(10);
dd($comments);
return view('Tours.single_tour')
->with(compact('tour', 'comments'));
}
Blade View
#foreach($comments as $comment)
<div class="review_strip_single">
<img src="{{asset('wanna show commented user photo')}}" height="78" width="78" alt="Image" class="img-circle">
<small> - {{$comment->created_at->format('d M Y')}} -</small>
<h4>{{wanna show user name}}</h4>
<p> {{$comment->tourreview_desc}} </p>
</div>
#endforeach
you can do nested query in controller
public function singleDetails($id)
{
$tour = Tour::with(['review.user'])->find($id);
return view('Tours.single_tour')
->with(compact('tour'));
or if you want only comments
$comments = Review::with('user')->whereHas('tour', function ($q)use($id){
$q->where('id', $id);
});
return view('Tours.single_tour')
->with(compact('comments'));
}
Blade View
#foreach($tour->comments as $comment)
<div class="review_strip_single">
<img src="{{asset('wanna show commented user photo')}}" height="78" width="78" alt="Image" class="img-circle">
<small> - {{$comment->created_at->format('d M Y')}} -</small>
<h4>{{$comment->user->name}}</h4>
<p> {{$comment->tourreview_desc}} </p>
</div>
#endforeach
or
#foreach($comments as $comment)
<div class="review_strip_single">
<img src="{{asset('wanna show commented user photo')}}" height="78" width="78" alt="Image" class="img-circle">
<small> - {{$comment->created_at->format('d M Y')}} -</small>
<h4>{{$comment->user->name}}</h4>
<p> {{$comment->tourreview_desc}} </p>
</div>
#endforeach

laravel function show in controller

I'm trying to show lessons from the course when i clicked on.
model lesson
public function course(){
return $this->belongsTo(Course::class);
}
model course
public function lesson() {
return $this->hasMany(Lesson::class);
}
show controller
public function show($id)
{
$cours = Course::findOrFailnd($id);
$lessons = course::findOrFail($id)->lesson;
return view('pages.lessons', compact('lessons', 'cours'));
}
page lesson
<div class="form-group">
<strong>Lessons : </strong>
#foreach ($lessons as $lesson )
{{$lesson->long_text}}
#endforeach
</div>
web routes
Route::resource('pages/lessons', 'LessonsController#show')->name('pages.lessons');
and i have this error:
Type error: Too few arguments to function Illuminate\Routing\PendingResourceRegistration::name(), 1 passed in C:\wamp64\www\learn2code\routes\web.php on line 21 and exactly 2 expected
For resource controllers its names instead of name:
Naming Resource Routes
By default, all resource controller actions have a route name;
however, you can override these names by passing a names array with
your options:
Route::resource('photos', 'PhotoController')->names([
'create' => 'photos.build'
]
Model Course
public function lessons() {
return $this->hasMany(Lesson::class);
}
Route
Route::get('pages/lessons/{course}', 'LessonsController#show')->name('pages.courses.lessons');
OR
Route::get('pages/courses/{course}/lessons', 'LessonsController#show')->name('pages.courses.lessons');
Controller show method
public function show(Course $course) {
return view('pages.lessons', compact('course'));
}
page lesson
<div class="form-group">
<strong>Lessons : </strong>
#foreach ($course->lessons as $lesson)
{{$lesson->long_text}}
#endforeach
</div>

Laravel - hasMany() and belongsTo() not working correctly for me in #foreach loop

I got following error when I try to use this on my page
Trying to get property of non-object
I have two tables:
cvicenis
id
title
body
category_id
categories
id
name
My Cviceni model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cviceni extends Model
{
public function category()
{
return $this->belongsTo('App\Category', 'id');
}
}
My Category model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function cviceni()
{
return $this->hasMany('App\Cviceni', 'category_id');
}
}
My controller:
public function index()
{
$cviceni = Cviceni::all();
return view('excercises.index', compact('cviceni'));
}
My index.blade:
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-sm-6 col-md-6 col-lg-3">
<h1>Přehled cvičení</h1>
#if(count($cviceni) > 0)
<ul>
#foreach ($cviceni as $c)
<li> {{ $c->category->name }}</li>
#endforeach
</ul>
#else
<h2>There is nothing to display</h2>
#endif
</div><!-- end of column -->
</div><!-- end of row -->
#endsection
If I use in my controller Cviceni::find(1) then I can get the value from $cviceni->category->name
But not when I use a foreach loop.
Can somebody help me please?
In the Cviceni model, I think you set up the foreign key is wrong. Your foreign key in the table is category_id, while you set up the relationship with foreign key is id. Try to replace the category() relationship with follow
public function category()
{
return $this->belongsTo('App\Category', 'category_id');
}
You need to eager load the models if you need them:
public function index()
{
$cviceni = Cviceni::with("category")->get();
return view('excercises.index', compact('cviceni'));
}

Resources