I'm the beginner of laravel 6. I just want to ask. I want to display data but it doesn't show.
index.blade.php:
#if(isset($teachers))
#foreach($teachers->qualifs as $qualif)
<li>{{ $qualif->qual }}</li>
#endforeach
#endif
Controller:
public function index()
{
$teachers= DB::table('teachers')->first();
$qualifs = DB::table('qualifs')->find($teachers->id);
return view('teachers.index',compact('teachers','qualifs'));
}
qualif.php:
public function teachers()
{
return $this->belongsToMany('todolist\teacher', 'qualif_teachers');
}
teacher.php:
public function qualifs()
{
return $this->belongsToMany('todolist\qualif', 'qualif_teachers');
}
Note: Data is storing correctly, only displaying issue.
ERROR:Undefined property: stdClass::$qualifs
relationship belongs to a model instance that means to an eloquent object. when you are using query builder you will get stdObject instead of an eloquent object. and thus your relationship is not working. to make this work you have to use eloquent instead of query builder.
public function index()
{
$teachers= teacher::get();
return view('teachers.index',compact('teachers'));
}
and view will be like
#foreach($teachers as $teacher)
#foreach($teacher->qualifs as $qualif)
<li>{{ $qualif->qual }}</li>
#endforeach
#endforeach
Related
I'm using Laravel 5.6 and I have created two Models Question and User Model and these are linked to each other using one to many relation as:
Question Model
public function user() {
return $this->belongsTo('App\User');
}
User Model
public function questions() {
return $this->hasMany('App\Question');
}
And my controller code is:
public function index()
{
$user = User::all();
return view('home', compact('user'));
}
So, i'm trying to get question title and I have written this code in blade:
#foreach($user as $user)
{{ dd($user->questions->questions_title) }}
#endforeach
But getting error undefined index questions_title, but if only write this {{ dd($user->questions) }} it gave me all questions, so how to fix it.
I have also tried {{ dd($user->questions['questions_title']) }} but not fixed.
You'll want to loop over your questions relationship to see your question:
#foreach($user as $u)
#foreach($u->questions as $question)
{{ dd($question->questions_title) }}
#endforeach
#endforeach
Note: I changed $user to $u
In your controller, you are not eager loading your questions relationships. Change your code to the following:
public function index()
{
$user = User::with('questions')->get();
return view('home', compact('user'));
}
Using the with() method will load all specified relationship with the eloquent query. After this, you will need to loop through the questions collection using the ->each() collection method.
#foreach($users as $user)
#foreach($user->question as $question)
{{ $question->question_title }}
#endforeach
#endforeach
No need for two foreach loops we can simply use one like below:
#foreach ($user->questions as $question)
{{ dd($question->questions_title) }}
#endforeach
i'm trying to get all the Level_id and Term_id on my intermediate table using many to many relationship but i couldn't make it work and i want to pass it to my view using ordered list html.
i always end up with this..Trying to get property of non-object
Controller:
public function get_term_level()
{
$terms=Term::find(1);
foreach ($terms->level as $tm) {
$tm->pivot->Level_id;
}
return view('term_level.index',compact('terms'));
}
Term Model same as with my LevelModel
public function level(){
return $this->belongsToMany(Level::class, 'term_levels')->withPivot('Term_id', 'Level_id');
}
View
#foreach ($terms->pivot as $tm )
<ul>
<li>{{ $tm->Term_id }}</li>
{{ $tm->Level_id }}
</ul>#endforeach
Try this (inside your controller):
foreach ($terms->level()->get() as $tm) {
$tm->pivot->Level_id;
}
I have model named 'PropertyLead' as,
class PropertyLead extends Model
{
public function leadPropertyDetails()
{
return $this->belongsTo('App\Models\Property\Property', 'Property_id','id');
}
public function user()
{
return $this->belongsTo('App\Models\Access\User\User','user_id','id');
}
public function propertyRatings()
{
return $this
->hasMany('App\Models\Property\PropertyRating','Property_id','Property_id');
}
}
In my controller I am trying to get data as ,
$leads = PropertyLead::with('leadPropertyDetails','user','propertyRatings')
->get();
Here in $leads variable i am getting all the data that I want but In 'propertyRatings' I am getting user_id and other details. I also want to get the name user who rated that property using that user_id in propertyRatings object. I am really troubled in this query. Thanks in advance.
Use nested eager loading syntax to load nested relationships:
PropertyLead::with('propertyRatings', 'propertyRatings.user', 'leadPropertyDetails', 'user')->get();
Then you be able to display user name with:
#foreach ($leads as $lead)
#foreach ($lead->propertyRatings as $propertyRating)
{{ $propertyRatings->user->name }}
#endforeach
#endforeach
I have relations like this
User->hasMany->reviews
Review->belongsTo->user
Review->hasMany->ReviewAnswer
ReviewAnswer->belongsTo->Review
Now I try to display answers for specific review. My reviewAnswer table looks like:
Id, review ID, text
But the problem is when I do that:
#foreach($user->reviews as $data)
#foreach($data->reviewAnswers->where('review_id', $data->id) as $answer)
{{$answer->text}}
#endforeach
#endforeach
Then reviews displays okay, but I get the same answers for every review. How to repair that?
Make sure to check if all the relationships are correct. In this example, I assume that your models are stored in your app folder.
First, your User model should have reviews
public function reviews() {
return $this->hasMany("App\Review", "user_id", "id");
}
Next, set up your Review model to have answers
public function answers() {
return $this->hasMany("App\ReviewAnswer", "review_id", "id");
}
Then this is how you'll get all users with their respective reviews and answers
$users = User::with("reviews.answers")->get();
Finally, peform the loop in your view
#foreach($users as $user)
#foreach($user->reviews as $review)
#foreach($review->answers as $answer)
#endforeach
#endforeach
#endforeach
Try using the hasManyThrough() option which the eloquent models provide you.
Link: Laravel Eloquent Relations
This means in your case you would add:
class User extends Model {
public function reviewAnswers(
return $this->hasManyThrough(ReviewAnswer::class,Review::class);
)
}
and then simply access it in blade as:
#foreach($user->reviewAnswers() as $reviewAnswer) {
{{$reviewAnswer->text}}
}
I want to know how can I check if the current user has any order or not? if have show the list and if not show the message?
currently I'm using this but it doesn't work well:
#if (empty(Auth::user()->order))
<blockquote>
<h1>Sorry you have no order at the moment, would you like to check out our products?</h1>
<footer>Click here</footer>
</blockquote>
#else
.......
#endif
user model:
public function order(){
return $this->hasMany(Order::class);
}
order model:
public function user(){
return $this->belongsTo(User::class);
}
my controller that shows orders history view:
public function index()
{
$user = Auth::user();
$orders = Order::where("user_id", "=", $user->id)->orderby('id', 'desc')->paginate(10);
return view('users.orders', compact('user', 'orders'));
}
You can count the orders using:
Auth::user()->orders->count()
To get this working there are a few prerequisite:
Have a correct DB schema
Have relations setup (it seems you need one-to-many
If you have this and assuming your relation is setup as:
class User extends Model{
/**
* Get the orders for the user
*/
public function orders(){
return $this->hasMany('App\Order');
}
}
You could use the forelse blade syntax
#forelse (Auth::user()->orders as $order)
<li>{{ $order->title }}</li>
#empty
<blockquote>
<h1>Sorry you have no order at the moment, would you like to check out our products?</h1>
<footer>Click here</footer>
</blockquote>
#endforelse
This will create a for loop in which you could declare the html layout for your order. If there are no orders the empty block will instead be used.