Eloquent Relationship for Learning Management System using Laravel - laravel

I have three model for the LMS which are Course, Section and Lesson. So under course there are sections and under sections there are specific lessons. I already got the relationship for Course and Section but my problem is on the lessons.
My Model schema
Courses
ID,
title,
user_id,
cat_id,
Sections
Id,
course_id,
title,
lessons
id,
section_id,
course_id,
user_id,
body_content,
lesson_title
I have tried this code but it doesnt work..
Here is my section model:
class Section extends Model
{
public function course()
{
return $this->belongsTo(Course::class);
}
public function lessons()
{
return $this->hasMany(Lesson::class,'section_id');
}
}
Lesson.php
class Lesson extends Model
{
public function section()
{
return $this->belongsTo(Section::class);
}
}
Course.php
class Course extends Model
{
protected $fillable = ['title', 'image'];
public function user()
{
return $this->belongsTo(User::class);
}
public function sections()
{
return $this->hasMany(Section::class);
}
}
LmsController.php
public function show($id)
{
$course = Course::with('sections')->find($id);
$othercourses = Course::orderby('created_at','desc')->get();
$sections = Section::with('lessons')->find($id);
$previous = Course::where('id', '<', $course->id)->orderBy('id', 'desc')->first();
$next = Course::where('id', '>', $course->id)->first();
$categories = Lmscategory::orderBy('name', 'asc')->get();
return view('users.learning.show', [
'course'=> $course,
'othercourses'=>$othercourses,
'previous'=>$previous,
'next'=>$next,
'categories'=>$categories,
'sections'=>$sections
]);
}
Blade.php
#foreach($course->sections as $section)
<button type="butcon" class="list-group-item list-group-item-action active">
{{$section->title}}
</button>
#foreach($sections->lessons as $lesson)
<div class="list-group">
<button type="button" class="list-group-item list-group-item-action">
<i class="fa fa-check-square-o"> </i>{{$lesson->lesson_title}}
</button>
</div>
#endforeach
#endforeach
I need to come up with this output:
Course Title: ICT Application Software
Section 1: Getting to know function
Lesson 1: Function Wizard
Lesson 2: IF Function
Section 2: Advance formula and functions
Lesson 3: Formula
Lesson 4: Advance Functions

If you need to show lessons of sections you can add something like this in your controller $course = Course::with(['sections', 'sections.lessons'])->find($id);.
Blade
#foreach($course->sections as $section)
<button type="butcon" class="list-group-item list-group-item-action active">
{{$section->title}}
</button>
#foreach($section->lessons as $lesson)
<div class="list-group">
<button type="button" class="list-group-item list-group-item-action">
<i class="fa fa-check-square-o"> </i>{{$lesson->lesson_title}}</button>
</div>
#endforeach
#endforeach
And do dd($course) to see what you got. I think you need that to present like this:
Course Title: ICT Application Software
Section 1: Getting to know function
Lesson 1: Function Wizard
Lesson 2: IF Function
Section 2: Advance formula and functions
Lesson 3: Formula
Lesson 4: Advance Functions
Section.php
public function course(){
return $this->belongsTo(Course::class);
}
public function lessons(){
return $this->hasMany(Lesson::class);
}
Lesson.php
public function section(){
return $this->belongsTo(Section::class, 'section_id');
}

Related

how can i handle the relations beetween products attributes and values?

i'm working on a Laravel/Livewire project
there are some products and services in this platform that user can order them.
the price of these products and services are changeable by their attributes . like size,color,quality and....
and i made a Many To Many relation between products and attributes.
but a can't handle it in my view where user should select that attributes before ordering
and my for each loop return wrong data . and i get this error :
Trying to get property 'pivot' of non-object .
my migration :
public function up()
{
Schema::create('attr_product', function (Blueprint $table) {
$table->foreignId('attr_id')->constrained();
$table->foreignId('product_id')->constrained();
$table->string('value')->nullable();
$table->string('price')->nullable();
$table->timestamps();
});
}
product model :
public function attr(){
return $this->belongsToMany(Attr::class)->withPivot(['value','price'])->withTimestamps();
}
attr model:
public function product(){
return $this->belongsToMany(Product::class)->withPivot(['value','price'])->withTimestamps();
}
my controller :
class SingleProduct extends Component
{
public $product;
public function mount($id){
$this->product=Product::with('attr')->findOrFail($id);
}
public function render()
{
return view('livewire.front.product.single-product')->extends('layouts.Default')->section('content');
}
}
my loop in blade :
#foreach($product->attr as $attr)
<div class="col-lg-4 col-sm-6 mt-3">
<h6 class="mb-2 text-black">{{$attr->title}}</h6>
<select class="custom-select shadow-none">
#foreach($attr as $av)
<option value="{{$av->pivot->price}}">{{$av->pivot->value}}</option>
#endforeach
</select>
</div>
#endforeach
before #foreach add
#php
dd($product)
#endphp
You can troubleshoot your $product Model and relations
The right syntax for withPivot should be parameters separated by comma, try this:
product model
public function attr(){
return $this->belongsToMany(Attr::class)
->withPivot('value','price')
->withTimestamps();
}
attr model:
public function product(){
return $this->belongsToMany(Product::class)
->withPivot('value','price')
->withTimestamps();
}

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 Has Undefined Constant 'App\App\projects

I have created a model called company and projects, I need to show projects under a company. The company is showing fine, but when I added a project relation to the model, it displays an error. I am a newbie to Laravel.
Undefined constant 'App\App\projects' (View:
C:\xampp\htdocs\testdrive\resources\views\companies\show.blade.php)
C:\xampp\htdocs\testdrive\app\company.php
Model
use Illuminate\Database\Eloquent\Model;
class company extends Model
{
protected $fillable = [
'name',
'description',
'user_id'
];
public function user()
{
return $this->belongsTo(App\User);
}
public function projects()
{
return $this->hasMany(App\projects);
}
}
show.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<!-- Jumbotron -->
<div class="jumbotron">
<h1>{{$company->name}}</h1>
<p class="lead">{{$company->description}}</p>
</div>
<!-- Example row of columns -->
<div class="row">
#foreach($company->projects as $projects)
<div class="col-lg-4">
<h2>{{$projects->name}}</h2>
<p class="text-danger">{{$projects->description}} </p>
<p>
<a class="btn btn-primary" href="/projects/{{$projects->id}}" role="button">View Project</a>
</p>
</div>
#endforeach
</div>
</div>
#endsection
You've got a typo! namespaces should be strings:
public function user()
{
return $this->belongsTo('App\User');
}
public function projects()
{
return $this->hasMany('App\projects');
}
You are missing class keyword in your model
class company extends Model
{
//
protected $fillable=[
'name',
'description',
'user_id'
];
public function user()
{
return $this->belongsTo(App\User::class);
}
public function projects()
{
return $this->hasMany(App\projects::class);
}
}
Hope this helps
Either pass class of the model or it's namespace within questions
public function user()
{
return $this->belongsTo(App\User::class); //or $this->belongsTo('App\User');
}
public function projects()
{
return $this->hasMany(App\Project::class);
}
Aside from using quotes to reference the model as mentioned above.
It might also be necessary to define the namespace of your class.
Sidenote: it's also a Laravel naming convention to use PascalCase for classnames (ie. Company, WetCamel, FooBarClass)
namespace App\Company;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
protected $fillable = [
'name',
'description',
'user_id'
];
public function user()
{
return $this->belongsTo('App\User');
}
public function projects()
{
return $this->hasMany('App\Projects');
}
}
Use '' this to call the model.
Correct your syntax as:
public function projects()
{
return $this->hasMany('App\projects');
}

Resources