Property [***] does not exist on this collection instance - laravel-5

I tried doing this for a website but my 'hotel' table has both an 'id' and a 'hotel_id' column. I heard eloquent recognizes the associated table name with _id as the primary key when defining a relationship. How would I define my relationship with the comments to the hotel? I have tried this in my Hotel model
public function comments() { return $this->hasMany(Comments::class, 'hotel_id') }
this is my controller for the page
public function hotelPage($hotel_id)
{
$hotels = Hotel::select(
'hotel.hotel_id'
,'hotel.name'
,'hotel.photo_url'
,'hotel.address'
,'hotel.desc_en'
//,'hotel.hotel_url'
,DB::raw("(CASE WHEN
COALESCE(hotel.hotel_url,'')!='' THEN
LOWER(CONCAT(hotel.hotel_url,'?aid=*******'))
ELSE
CONCAT(hotel.hotel_url,'http://www.example.com/?aid=*******')
END)
as hotel_url"))
->where('hotel.hotel_id',$hotel_id)
->get();
return view('hotel.hotelPage', ['hotels' => $hotels]);
}
and when I load the view
#foreach($hotels->comments as $comment) {{$comment->body}} #endforeach
I get the error "Property [comments] does not exist on this collection instance./ / /hotelPage.blade.php"
Please help, I am using laravel 5.4

Related

Route model binding select specific columns with relation

Route:
Route::get('/posts/{post}', [PostController::class, 'show']);
Controller:
public function show(Post $post){
$postData = $post->load(['author' => function($query){
$query->select('post_id', 'name');
}])
->get(['title', 'desc', 'created_date'])
->toArray();
}
This returns all the posts in the database, While I only want to get the selected post passed to the show function.
So if I visit /posts/3, It should show data related to the post with id = 3, not all posts.
The result should be an array with only the selected post.
When you are inside the Controller you already have the Post you want. If you call get you are getting all posts with a new query.
To select specific columns from a relationship, you can do it like this:
public function show(Post $post)
{
$post->load('author:id,name,age'); // load columns id, name, age from author
}
Notice that including the id is required.
To specify columns for the Post, there's no way to do it per route, the only way is to override how Laravel resolves the implicit binding. For that, you can add this in your Post model class:
public function resolveRouteBinding($value, $field = null)
{
return $this->whereKey($value)->select(['id', 'body', 'author_id'])->firstOrFail();
}
Notice that including the author_id is required to load the Author afterwards inside the Controller method. Also keep in mind this will affect all routes where you implicitly load a Post.
Please don't use get() function on the model.
public function show(Post $post)
{
$post->load(['author' => function ($query) {
$query->select(['post_id', 'name']);
]);
dd($post->toArray());
}
Btw, the author shouldn't have a post_id, because author can have multiple posts. you should update the database structure or you are doing wrong in the controller. (my bad, Thanks #techno)

Eloquent relationship: return just one value not entire row

In my laravel project I built a comment section and want to display the names of the people commenting beside their comment.
Initially I have just the userID, so I built a relationship (hasOne) linking the comment table (comment & authorID) to the users table (id (authorID) & username)
Comment.php (model) is:
[..]
public function author()
{
return $this->hasOne(User::class, 'id', 'Author');
}
The User.php model is:
<?php
[..]
public function author()
{
return $this->hasMany(Comment::class, 'Author', 'id');
}
In the controller I get the data with:
$comments= Comments::where('LinkID', (string) $id)->with('author')->orderBy('updated_at', 'ASC')->get()->all();
This works but it gives me the entire row of the user per comment. For security reasons I just want to return the 'name' field of the row (username) without the rest (email, timestamps etc.).
How can I achieve this?
please try:
$comments= Comments::where('LinkID', (string) $id)->with(['author' => function ($q) {
$q = $q->select('name', 'id');
return $q;
}
])->orderBy('updated_at', 'ASC')->get()->all();
or another way:
$comments= Comments::where('LinkID', (string) $id)->with('author:id,name')->orderBy('updated_at', 'ASC')->get()->all();
see eager loading (section Eager Loading Specific Columns)
https://laravel.com/docs/7.x/eloquent-relationships#eager-loading
note that including 'id' is necessary because it 's responsible for the relation

How to make this Eloquent relationship in Laravel

I have a table called users with a column username. Another table called students with a column code. I have made a hasMany relationship in User model like below and it's working fine.
public function students()
{
return $this->hasMany(Student::class,'code','username');
}
I have another table called institutes where a column is similar to students table named inst. I need to show data from institutes table while showing data of an user. How to make this relationship?
users table
username|mobile|address|password
students table
username|name|inst|roll|reg
institutes table
name|inst|address|phone
This is my home controller
public function index()
{
$admins = User::where('username','=',Auth::user()->username)->get();
return view('home', compact('admins'));
}
And this is my view
#foreach($admins as $key => $admin)
#foreach($admin->students as $student)
{{ $student->reg }}
#endforeach
#endforeach
Add migration to your Students table :
$table->unsignedBigInteger('user_id')->nullable();
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('set null');
Update your model User on students method :
return $this->hasMany(Student::class);
Add "with method"
$admins = User::where('username','=',Auth::user()->username)->with('students')->get();

Laravel 5.5 Relationships - ErrorException Trying to get property of non-object

Can't display an info from DB to view, have an error
ErrorException
Trying to get property of non-object
Let me explain the logic:
User can create many companies
Companies can have many items
I don't have problems with inserting info to DB, everything is fine.
I can display every info from Users table and Company table in my Show View, but I can't display info from Company_Items tables in my view.
I guess something is wrong with my Relationships or keys, but what?
Trying to display Item name that belong to the specific company
{{$company->company_items->name}}
Company Table:
id(int)
company_name(string)
info(text)
user_id(int)
Company_items Table:
id(int)
company_id(int)
name(string)
Company Model:
class Company extends Model
{
protected $table = 'company';
//relation. MANY companies can be created by ONE user
public function user(){
return $this->belongsTo('App\User');
}
//relation. ONE company can have MANY items
public function item(){
return $this->hasMany('App\Item');
}
}
Item Model:
class Item extends Model
{
protected $table = 'company_items';
//relation. MANY items can be applied TO ONE company
public function company(){
return $this->belongsTo('App\Company');
}
}
Controller:
public function show($id)
{
$company = Company::find($id);
$user = User::find($id);
$company_items = Item::find($id);
return view('company_show')->with(['company' => $company,'company_items' => $company_items]);
}
View:
This is <b>{{$company->company_id}}</b> page info created by <b>{{$company->user->name}}</b><br>
Company category is <b>{{$company->company_category}}</b>.<br>
Item name is <b>{{$company->company_items->name}}</b>. // Can't display this expression
$company->company_items is a list of all the items of the company, you can't call a param on it. And you declared your relation with the name item and not company_items
Try to do :
$company->item[0]->name
or
#foreach($company->item as $item)
{{ $item->name }}
#endforeach
First error you made is:
$company_items = Item::find($id);
This will return the company Item that has the ID=$id which means it will return the Item that has the same ID as the company! I'm sure that's not what you want.
Instead you can do:
$company_items = $company->item;
which will return all the items of that company
and in your view you can access the items with a foreach loop:
#foreach($company_items as $item){
{{$item->name}}
}

Laravel orm Order by custom attribute

In my user model I have added custom attribute get number of post user have created.
I can successfully get that but when I have try to order by it give error.
1054 Unknown column 'posts' in 'order clause'
Can you let us know what is the Eloquent query you're running? Also make sure that the name of the table you're using is correct.
EDIT:
Does the posts column exist in the users table? Or are you trying to get all the posts from the Post Model and order the posts by their name in descending order?
If that is what you want to do then you would need to go to your User Model and set up your relationship with posts.
In User Model create this method:
public function posts() {
return $this->hasMany('Post'); // Post would be the name of the Posts Model
}
In Post model do this:
public function user() {
return $this->belongsTo('User'); // Where User is the name of the Users Model
}
Then according to the Laravel documentation you could do someonething like this with the query:
$users = User::with(array('posts' => function($query)
{
$query->orderBy('name', 'desc'); // assuming that 'name' would be the column in the posts table you want to sort by
}))->take(5)->get();
I hope I am correct and this helps and that solves your issue.

Resources