class Keyword extends Model {
public function results()
{
return $this->hasMany('App\Result');
}
}
class Result extends Model {
public function keywords()
{
return $this->belongsTo('App\Keyword');
}
}
these are my classes, but when i access
$keyword->results->where('engine','Google')->last()->created_at
I get error : Trying to get property of non-object
if I var_dump($keyword->results->where('engine','Google')->last()), it show object of Result class
So what I am doing wrong here? i need to filter results that is working fine, but can't access properties/attributes of result
I was accessing results for all keyewords in loop even if there was no related result, I did like
#if($ranking = $keyword->results->where('engine',$engine)->last())
{!! $ranking->created_at !!}
#endif
and the issue is solved, got help from : Laravel 5: “Trying to get property of non-object”
Related
So, Iam trying to make a conversation/message system.
So I have a problem right now. When I use the following line, I get [] (Empty Json):
$this->auth->user()->conversations
User model:
public function conversations()
{
return $this->belongsToMany(\App\Models\Conversation\Conversation::class);
}
But if I use this line, I get data that I want:
$this->auth->user()->messages()->first()->conversation->messages
Also when a line like the following one is used, I get an error
Property [conversation] does not exist on this collection instance.
$this->auth->user()->messages->conversation->messages
Here are Messages model line:
public function conversation()
{
return $this->belongsTo(\App\Models\Conversation\Conversation::class);
}
Here are User model line:
public function messages()
{
return $this->hasMany(\App\Models\Conversation\Messages::class);
}
Here are My database schema:
So my question is, do I do something wrong here or this is just some kind of bug? Thanks for any answers/help.
In my Controller function I am calling the following code:
$ad = Ad::activeRegular()->with('category', 'city')->limit($limit_regular_ads)->orderBy('id', 'desc')->get();
and it successfully returns an Ad Eloquent model object. This object has a relation defined in Ad.php as follows:
class Ad extends Model
{
public function category(){
return $this->belongsTo(Category::class, 'category_id');
}
}
Now the problem
But in my view when I try to fetch the Category of this Ad(vertisement) with the following code:
foreach($ad as $rad){
var_dump($rad->category->category_name)
}
it fires
Trying to get property of non-object
For information, var_dump($rad->category) successfully prints the contents of the related Category object.
Why can't I access the attributes of the object this way.
even $rad->category()->category_name throws the same error.
This happens because one of the ads doesn't have a category.
You can use optional() helper you if want to show name only if ad has a category:
foreach ($ad as $rad) {
var_dump(optional($rad->category)->category_name)
}
Or make sure all ads have a category.
I am using Laravel 5.2 and have a problem.
My code is;
$sts = STSMember::find($member_id)->join('rating', 's_t_s_members.member_id', '=', 'rating.member_id');
But I get the following error
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string'.
How to get member_id from two table?
find returns a model instance, you need either:
STSMember::where("id","=",$member_id)->join('rating', 's_t_s_members.member_id', '=', 'rating.member_id')->get();
Or ideally:
class STSMember extends Model {
//Other model code
public function rating() {
return $this->belongsTo(Rating::class);
}
}
Then you can do:
STMember::with("rating")->find($member_id);
Check https://laravel.com/docs/5.4/eloquent-relationships
I am new to Laravel 5 and was wondering how model object retrieval works.
For instance I have a separate table that is referenced by another table and I want to get the records from that.
Item Table
Category Table
I was trying to extend the User model
Class Item extends Model {
public function getCategory(){
$category = Category::find($this->category_id);
return $category;
}
}
So when I try to access the object retrieved in my view,
{{ $item->getCategory()->name }}
I get the error
Undefined property: Illuminate\Database\Eloquent\Builder::$name
What am I doing wrong? And what is the best practice in doing this? I used to do this in Symfony and it works so I was wondering how its done in Laravel.
Any help and input would be greatly appreciated.
Thank you all.
As stated in the docs here's how I did it
Class Item extends Model {
public function category()
{
return $this->hasOne('App\Category', 'id', 'category_id');
}
}
And accessed the object in the view this way
{{ $item->category->name }}
I have a dynamic property user in my model:
class Training extends Model
{
...
public function user()
{
return $this->belongsTo('App\User');
}
}
And I can easy get username in controller like this:
Training::find(1)->user->name
But I don't know how to perform the same in view. I tried this:
Controller:
return view('training/single', Training::find(1));
View:
{{ $user->name }};
but without success, I'm getting error Undefined variable: user. So it's look like I can't access dynamic property in view.
Any idea how can I use dynamic property in views?
I fear that's not really possible. There's no way to set the $this context in your view to the model. You could convert the model into an array with toArray() but that would include the related model and you would have to access it with $user['name'].
I personally would just declare the user variable explicitly:
$training = Training::find(1);
return view('training/single', ['training' => $training, 'user' => $training->user]);
Use eager loading
return view('training/single', Training::with('user')->find(1));