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
Related
When livewire is rendering a view, I get an error message:
Missing required parameters for [Route: book.show]
But if I just print the value, like {{ $slug }} , the view is rendered correctly.
It looks like the model data is not available at render time. Where I am going wrong?
Snippet of my view
<a href="{{ route('book.show', $slug) }}">
Show book
</a>
My component
class BookForm extends Component
{
public $slug;
public $name;
public $summary;
protected $listeners = ['fillForm1' => 'editList'];
public function editList($id)
{
$book = Book::find($id);
$this->slug = $book->slug;
$this->name = $book->name;
$this->summary = $book->summary;
}
public function render()
{
return view('livewire.books.book-form');
}
}
My route
Route::get('/book/{slug}', 'BookController#show')->name('book.show');
My BookController / show method
public function show($slug)
{
...
}
I'm using "laravel/framework": "^7.0" and "livewire/livewire": "^2.5"
I think there must be a limitation for livewire not to send the model data when rendering the view. I used the following workaround, if you have a way to improve this please let me know.
public function editList($id)
{
$book = Book::find($id);
$this->slug = route('book.show', $book->slug);
$this->name = $book->name;
$this->summary = $book->summary;
}
I am creating an online course site. I have issue with retrieving the course content of a specific course.
This function shows all the contents of different courses as well. I want to show the content of a specific course instead.
public function index()
{
$contents = Content::all();
return view('content.index', compact('contents'));
}
This is my content model.
class Content extends Model
{
protected $fillable = [
'topic', 'content',
];
public function course()
{
return $this->belongsTo('App\Course');
}
}
Thsi is course model.
class Course extends Model
{
protected $fillable = [
'title', 'about', 'image',
];
public function contents(){
return $this->hasMany('App\Content');
}
}
Content Migration
public function up()
{
Schema::create('contents', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('course_id');
$table->string('content');
$table->string('topic');
$table->timestamps();
});
}
Content index blade
#foreach ($contents as $content)
<div class="col-lg-12 content-list">
<a href="/content/{{$content->id}}">
<div class="cl-item mb-2" style="border-radius: 8px; padding: 18px; background-color: #c2c6ca;">
<h4 class="m-0">{{$content->topic}}</h4>
</div>
</a>
</div>
#endforeach
You need to create a route in web.php like following:
Route::get('/courses/{course_id}/contents', 'ContentController#get')->name('web.course_contents');
In the above code base, we pass "course_id" param for which we want to fetch the contents for.
In ContentController.php, do the following:
class ContentController extends Controller
{
get($course_id)
{
$contents = Content::where('course_id', $course_id)->get();
return view('content.index', compact('contents'));
}
}
Content::where('course_id', $course_id)->get() will run select * from contents where course_id = ? query to your database. You can check this by doing the following:
class ContentController extends Controller
{
get($course_id)
{
$contents = Content::where('course_id', $course_id)->get();
logger(Content::where('course_id', $course_id)->toSql());
return view('content.index', compact('contents'));
}
}
You can learn more about Laravel Query Builders here.
Happy Coding!
For this, all you need to do is use with to fetch both contents and their courses at the same time as:
public function index()
{
$contents = Content::with('course')->get();
return view('content.index', compact('contents'));
}
For more information, you can visit this link: https://laravel.com/docs/6.x/eloquent-relationships#eager-loading
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.
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
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>