hasOne relationship and associate()? - laravel

I have this relationship :
public function company(){
return $this->hasOne('App\Models\User','personal_users_business_users', 'user_id', 'business_user_id');
}
When i try this:
$user->company()->associate($business_user_id);
Im getting this error:
Call to undefined method
Illuminate\Database\Query\Builder::associate()
I have pivot table personal_users_business_users and it has user_id, business_user_id, approved
And now i want to insert in that pivot table business_user_id. What im doing wrong?

Your relationship should be like this:
public function company()
{
return $this->hasOne(User::class);
}
User is the model you want to connect with,
and you should call the relationship like this:
$user->company->business_user_id;
$user must be the Model you have the relationship function in.
company is the function and the business_user_id the field you want to get

associate() is a method of the belongsTo relationship, but it looks like from the above you are trying to call it via the hasOne relationship.
UPDATE
You can create a function in your App\Models\User class
public function company() {
return $this->belongsTo('MODEL_OF_COMPANY', 'foreign_key', 'owner_key', 'relation');
}
then call $this->company()->associate($company)

Related

Laravel nested `hasOne` relationship with `where` clause

I have Shop model which has multiple Content entries:
public function contents(): HasMany
{
return $this->hasMany(Content::class);
}
And I had hasOne relationship with one specific type of content like this:
public function widgetContent(): HasOne
{
return $this->hasOne(Content::class)->where('type', Content::WIDGET);
}
It was working fine but I moved Content's type into a new model called ContentType, and removed the type field from Content model, but instead added another hasOne relationship via content_type_id field.
Now, I'm trying to achieve the same widgetContent relationship but I can't. Here's my code:
public function widgetContent(): HasOne
{
return $this
->hasOne(Content::class)
->whereHas('contentType', fn ($q) => $q->where('type', ContentType::WIDGET));
}
What should I do to achieve this? Also, if this is not the best practice (that's how I feel), what is the best practice?
You could create a closure and use that in stead. So you would still have the relationship like this:
public function content(): HasOne
{
return $this
->hasOne(Content::class);
}
But then you add a closure in your model:
public function getWidgetContent()
{
return $this->content()->whereHas('contentType', function(Builder $q){
$q->where('type', ContentType::WIDGET)
})->get();
}
And then you just call the closure function when you need the results.

How to define multiple belongsTo in laravel

My table has many foreign key for example prefecture_id, gender_id and status_id.
And I made model for those table.
So I want to define multiple belongsTo method like following for get all data with query builder..
But In fact belongsTo can't use like this.
public function foreign(){
return $this->belongsTo([
'App/Prefecture',
'App/Gender',
'App/Status',
]
}
And if the only way is defining multiple method for belongs to.
How do I get all belongstos data in querybuilder.
Please give me advice.
As far as I am aware, there's not a way to get multiple belongsTo from a single method. What you have to do is make one method for each relationship and when you want to load the relationships you can do the following.
Model
public function prefecture()
{
return $this->belongsTo(\App\Prefecture::class);
}
public function gender()
{
return $this->belongsTo(\App\Gender::class);
}
public function status()
{
return $this->belongsTo(\App\Status::class);
}
Query
// This will get your model with all of the belongs to relationships.
$results = Model::query()->with(['prefecture', 'gender', 'status'])->get();

user_id in other table does not get value in one to many relation in laravel

In restaurant table, foreign key do not get value of user table. I make relation one to many in user and restaurant tables. user can have many restaurants.
class Restaurant extends Model
{
protected $guarded=['user_id'];
protected $table ="rest_info";
public function menus() {
return $this->hasMany('App\Menu');
}
public function dishes(){
return $this->morphMany('App\Dish','dishable');
}
public function user(){
return $this->belongsTo('App\User','id','user_id');
}
}
If you wish to access these relationship with in your controller function you can use with keyword of laravel you have to something like this:-
If you wish to get the menus you can use something like this in your contoller Function
$getResturantdata = Resturant::with('menus')->get();
dd($getResturantdata);
If you wish to get the menus and users both you can use something like this in your contoller Function:-
$getResturantdata = Resturant::with('menus','users')->get();
dd($getResturantdata);

1:1 relationships in Laravel returns Undefined property

I keep getting this error in Laravel 5 when working on a 1:1 database relationship:
Undefined property: Illuminate\Database\Eloquent\Collection::$owner
In my controller i have method "stuff". When i return $stuff i get:
[{"id":4,"demoId":2,"slug":"loremipsum","languageId":1,"countryId":1,"created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00"}]
The relationship is on "demoId".
In my model i have this:
public function owner(){
return $this->belongsTo('App\Demotable2');
}
I'm using this code, that gives an error:
$routine = $stuff->owner->get()->toArray();
I expect to get the info in the demotable2. What do i do wrong?
There are certain rules that you need to apply when you try to make most of eloquent (convention over configuration), the problem you have is in naming your foreign key's, when you use :
public function owner(){
return $this->belongsTo('App\Demotable2');
}
eloquent expects to find a foreign key demo_id instead of demoId, when you change the name of the foreign key, you need to specify it in the relationship like so :
public function owner(){
return $this->belongsTo('App\Demotable2', 'demoId', 'id');
}
you read more here : http://laravel.com/docs/5.1/eloquent-relationships
I think this code will be in your model.
public function owner(){
return $this->belongsTo('App\Demotable2');
}
The relationship is defined in your model, not controller.
In App\Stuff.php
public function owner() {
return $this->belongsTo(App\Demotable2::class);
}
When this relationship is run, it will automatically look for a owner_id on your stuff table. However, you are using demoId. For that to work you have to define the foreign key in your relationship
public function owner() {
return $this->belongsTo(App\Demotable2::class, 'demoId');
}

laravel 5 pivot table

I'm trying to implement a friendlist for a user
so a user can have many friends
but for calling the friends I'm having trouble getting my relation right:
user
id
friends
user_id
friend_user_id
Class user:
public function Friends(){
return $this->hasMany('App\User', 'friends');
}
So Auth::User()->Friends should return a list of User models
You actually don't have a "one to many" relationship but rather "many to many" with friends being your joining table
Try this:
public function Friends(){
return $this->belongsToMany('App\User', 'friends', 'user_id', 'friend_user_id');
}
When creating a relation to the same model, you would do something like this:
Models/User.cs
public function Friends()
{
return $this->hasMany('User', 'user_friend_of', 'id'); // assuming user_friend_of is the FK on user-table
}
public function User()
{
return $this->belongsTo('User');
}
This would mean you have only 1 table, which has a foreign key to another row in the same table, for instance: (id - name - user_friend_of )
In your user class, define the function as follows:
public function Friends(){
return $this->hasMany('friends');
}
Calling $user->Friends()->get() should return you everything you need. Note that 'friends' should be the name of the table.

Resources