I'm attempting to build up a query in Laravel.
I have two tables, with the following attirbuteds
User
id (auto)
name
email
userType
password
TenantPreferance
id (auto)
county
type
user_id (this is the user who created their
preference)
I'm trying to get a collection of data from users of a certain type where id of the user, matches the user_id in preferences.
But I get this error
Undefined property: Illuminate\Database\Eloquent\Builder::$id
$tenants = User::where('userType', 'tenant');
$Prefereances = TenantPreferance::where($tenants->id, $Prefereances->user_id);
$users = $Prefereances->get();
for : $tenants = User::where('userType', 'tenant');
must add:
first()
$tenants = User::where('userType', 'tenant')->first();
or : get()
$tenants = User::where('userType', 'tenant')->get();
You need to actually run the built sql. You also need to get a singular event of it out. So im using first() in this instance. Like this
$tenants = User::where('userType', 'tenant')->first();
$tentants = User::with('TenantPreferance')->where('userType', 'tenant')->get();
where TenantPreferance is the relationship name
Related
Trying to get property from a object in laravel. I have this:
public function index()
{
$firmen = Companies::all();
$allcountcompanies = Companies::count();
$agent = Agent::find($firmen->agent_firma_id);
return view('companies',compact(['firmen'],['allcountcompanies'],['agent']));
}
The Exeption gives me that:
Property [agent_firma_id] does not exist on this collection instance.
But when i put the id eg 1001 it shows corrent entry in db field.
What i0'm doing wrong?
Info:
CompaniesController.php (table: br_firma)
AgentController.php (table: br_firma_agents)
Table "br_firma_agents" contains a foreign_key from table "br_firma".
You are getting id from collection.
$firmens = Companies::all();
it return collection you may be use it useing loop and get one by one data as
for($firmens as $firmen){
$agent = Agent::find($firmen->agent_firma_id);
}
Or you create relation between Company and agent as,
In company model define relation as
public function agents(){
return $this->hasMany(Agent::class);
}
and in agent model define relation as
public function company(){
return $this->belongsTo(Companies::class);
}
and then call it as in controller,
$data = Companies::with('agents')->get();
You are trying to access an agent_firma_id from a collection of companies, you could first use a loop on that collection,or use an index on that collection,
you could try
$firmens = Companies::all();
foreach($firmens as $firmen){
$agent = Agent::find($firmen->agent_firma_id);
}
I've defined my Slot model to load the relations from User model like so :
public function userAssignedFull(): HasOne {
return $this->hasOne(User::class,'id','user_assigned');
}
('slots' table contains 'user_assigned' field by which I connect to User records on 'id')
The following code finds Slot model but without 'userAssignedFull'. I get only the user ID in 'user_assigned'.
$slot = Slot::with('userAssignedFull')->find($slot_id);
But calling this afterward returns me the wanted relation:
$fullUserModel = $slot->userAssignedFull;
Can anyone tell me what am I doing wrong ?
Builder::with() returns the Builder instance.
So you have to call $slot->userAssignedFull; to get the collection of data.
From the docs:
When accessing Eloquent relationships as properties, the relationship
data is "lazy loaded". This means the relationship data is not
actually loaded until you first access the property.
And this $slot->userAssignedFull; is your "first access the property".
Try this
$slot = Slot::where('id', $slot_id)->with('userAssignedFull')->first();
$slot->userAssignedFull;
How can i get article owner user_id and save to notifiable_id field
Suppos
$articlecomment = $article_owner_id
My code:
$articlecomment = new Article_comment();
$articlecomment->user_id = Auth::user()->id;//Comment by user id
$articlecomment->article_id = $request->articleid;
$articlecomment->comment = $request->comment;
$articlecomment->save();
auth()->user()->notify(new ArticleNotification($articlecomment));
//$articlecomment->user()->notify(new ArticleNotification($articlecomment));
Database Screenshot
i want article_owner_user_id on notifible_id field
enter image description here
Solved
$articlecomment->save();
$article = Article::where('id','=',$request->articleid)->first();
if($article->user_id != Auth::User()->id){
$article->user->notify(new ArticleNotification($article));
}
This is database i need article_owner_id on notifible_id field click here to see screenshot
If you have a relationship set up for your Article_comment and Article models, you can access the "Article Owner" through the relation.
For example, define a relation for your "Article" model inside your Article_comment class (assuming Article is the name of your model):
class Article_comment extends Model {
....
public function article() {
return $this->hasOne('App\Article', 'id', 'article_id')
}
....
}
Once you have that set, you can access your relation (and subsequent properties) like so (assuming article_owner_id is a property of your Article model):
$articlecomment->article->article_owner_id
Edit:
Whichever user you call notify on will notify that user. So to notify the article owner, you will need to get the user of the article and call notify from that instance (rather than the auth user). If you set up a relation to the user on your Article class, you can simply call notify from that, or get the user from the article_owner_id and call notify.
Example:
$user = User::where('id', '=', $articlecomment->article->article_owner_id)->first();
$user->notify(new ArticleNotification($articlecomment));
With a relation set on your Article class, you could instead call notify like this:
$articlecomment->article->user->notify(new ArticleNotification($articlecomment));
For more info on Eloquent relationships, see https://laravel.com/docs/5.6/eloquent-relationships#introduction
I need to display all the vehicle records belonging to properties of the currenty logged on user.
Before I pass the data to the view I need to filter out all the properties except those owned by the current user id.
I have a relationship in the Vehicle model that performs the foreign key constraint.
public function propiedad(){
return $this->belongsTo('App\Property', 'propiedad_id','id' );
}
My Controller is using this filter before passing the data to the view. I assume I have to acquire the property ids that belong to the user before I filter the vehicles that belong to those properties. This is where I am having trouble.
$user_properties = array(?????);
$Vehiculo = Vehiculo::where('propiedad_id', $user_properties);
If your properties table has a user_id column then on your user model define a vehicles hasManyThrough relationship like
public function vehicles()
{
return $this->hasManyThrough(Vehicle::class, Property::class);
}
Then in your controller you can do:
$users = User::with('vehicles')->get();
Or for a single user
$vehicles= User::find($id)->vehicles;
I am laravel newbie and I am trying to follow the documentation.So I have two models, 'User' model and a 'UserPhone' model. A user has many phones.
User model:
public function userPhone() {
return $this->hasMany('UserPhone');
}
UserPhone model:
public function user(){
return $this->belongsTo('User');
}
On my controller I am trying to "copy" the documentation:
$userPhone = User::find(1)->userPhone;
Well the result is an error:
Trying to get property of non-object
I know that I am missing something here , but I cannot find it.
I'm pretty sure that you don't have an user with id of 1.
$userPhone = User::find(1)->userPhone;
This should work, but, if it doesn't find the user the first part:
User::find(1)
I will return a NULL and NULL is not an object, then you get the error: Trying to get property of non-object.
My advice is, try to do this
var_dump( User::find(1) );
And you if you receive just a NULL, you found the problem.
Well the answer is that everything was ok!
I had accidentaly left
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
before the UserPhone Model Class declaration..It was such a newbie mistake.
If you want to fetch Users with their related phone numbers (userPhone) you can use Eager Loading.
//get all users (User) with their respective phonenumbers (userPhone)
$users = User::with('userPhone')->get()
//get User with id==1, with his related phonenumbers (userPhone of User(1))
$user_1 = User::with('userPhone')->where('id',1)->first()
and than you can do
if(!is_null($user))
$phones_of_user_1 = $user_1->userPhone();
else
$phones_of_user_1 = array();
That way, if a user of id==1 exists, you fetch his phone numbers. Else, you get an empty array and no exception/error (trying to get property on a non-object) thrown .
That relationship would automatically be loaded for you.
$user = User::find(1);
echo $user->userPhone->id;
This is assuming you have your database tables are setup correctly according to laravel's conventions and you actually have a User with an ID of 1.
1) You are missing a pair of () after userPhone
$userPhone = User::find(1)->userPhone();
2) You are not using the 'find' method properly. I think what you want to do is :
$userPhone = User::userPhone()->get();
or
$userPhone = User::find($phoneId); //where $phoneId is the id of the phone you are trying to find.
The 'find' method return only one object and will try to find it with it's id.