Can't access custom pivot element (Laravel framework) - laravel

I created a relation between User and Event. I added a pivot element 'part_sure' to the pivot table and updated the models appropriately:
Now I have a problem with accessing this pivot element. If I try to do this...
... it doesn't display anything! No error but also no value.
Looking with this command...
... what is accessable in the pivot element:
As you can see, the part_sure element is not there.
My user model:
My event model:
I really don't know why this isn't working. Tried to google it for almost an hour. I would be really happy if somebody could give me a hint!

You're calling the events method from the User model, but you aren't showing us the User model.
What does your User model look like? Just like you did with the Event model, you need to specify the extra pivot column in the User model.
User model
public function events() {
return $this->belongsToMany('radclub\Event')->withPivot('part_sure');
}
You may need to edit radclub\Event with the proper namespace.

Related

The right way to receive related data in Laravel?

Here are some models:
UserModel
SpecializationModel
UserSpecializationModel
I need to recieve authorized user's specialization. I can do that:
$specializations = UserSpecialization::where("user_id", Auth::user()->id)->get();
Also I can do this through the UserModel model using relation hasMany() specializations().
When to use first case and the second?
$specializations = Auth::user()->specializations();
Do I need a model UserSpecializationModel?
In general you don't need UserSpecilizatonModel, in most situations you wont access data directly from that table, you'll either do it through user or specialization model.
Check also https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns for accessing data from pivot table.

Which relation to use in Laravel?

Which relation to use in Laravel to bind two table through third?
When Doctors can be assigned to some Centers. The intermediate table will be as:
doctor_id | center_id
How to create model in Laravel for this case?
You don't need a model for the intermediate table, simply use attach
Example:
$center = Center::create();
$doctor = Doctor::find(1);
$doctor->centers()->attach($doctor->id);
This is a very simple example but should give you the idea, of how to approach it.
All of it of course requires you have set up your Center and Doctor model with the correct many to many relations
Doctor.php model:
public function centers()
{
return $this->belongsToMany(Doctor::class);
}
See the documentation, for more information.
You could obviously create a model called DoctorsCenter and create it manually by doing this, whenever you want to attach a relation.
DoctorsCenter::create(['center_id' => $center->id, 'doctor_id' => $doctor->id]);
I don't see any good reason for doing this, and would not recommend it.
You can use hasMany or belongsTo relationship of Laravel.
See the laravel documentation, for more information

Laravel - set up a hasMany like relationship without the weak table

I would like your advice in the implementation of the following.
There is an Activity model and a activities table in my project, an activity can have multiple 'tips', and I need to store those tips somewhere.
Each tip has only a text description, if I wasn't using Laravel, I would simply create a table like activity_tips where I would store the id of the activity and the description of the tip, each row been one tip belonging to one activity, maybe an id incremental, but nothing more (at least what I planned).
Now, Laravel comes with this beauty called relationships, but it must be between two models.
I know I can't use something like this because it needs the Tip model (and table).
public function tips(){
return $this->hasMany('App\Tip', 'id_activity', 'id')
->select('id', 'id_activity', 'tip');
}
I could create a Tip model and table, but it doesn't feel right because the Tip table would the 'same' as activity_tips.
Is there any Laravel feature than can help me with that? Also by keeping everything in the model? Maybe use raw sql statements to only have the activity_tips
I found out about the hasMany constrain from laravel and ended up doing a tips table and Tip model.
tips: id, id_activity, tip, created_at, updated_at
public function requirements(){
return $this->hasMany('App\Tips', 'id_activity', 'id');
}
Silly me, I hope someone find this usefull
In the Activity model add:
public function tips(){
return $this->hasMany(Tip::class);
}
This assuming you have a Tip model with 'id', 'activity_id', 'tip'

Cakephp 3.6. How to call a helpers query from a controller?

I have on my homepage a small search form with a list of categories.
I use a helper to query the database to get all categories. This is fine and works great.
Now I want to update the categories select with an updated list of categories based on a performed search.
That means, I would need to call query inside the helper with a given $variable to run a new query and to return new data.
But this is my concern now.
I can't call a helper from within the PagesController, right?
So what is the right way to
- have a helper call to use on the fly in all views selecting DB Data?
- address an ajax call to a controller and to use the query from the helper?
Any advice will be highly appreciated!
Thanks!
Short answer:
You code needs to be moved in a Table class and not in a Helper class, if you need it both in the Controller and the View.
Long Answer:
The code you have created in your helper needs to be moved in the relevant Table class and that will fetch the data.
Then in both your view and the controller you will use the new method you created in your Table class.
For example in your CategoriesTable class you can add a method called:
public function fetchCategoryData(){
//TODO: Custom code that will fetch the category data like...
//return $this->find();
}
public function updateCategoyData($args){
//TODO: Custom code to update your category data
}
Now, in either the View or the Controller you can use this
$Categories = TableRegistry::get('Categories');
$Categories->fetchCategoryData();
//or
$Categories->updateCategoryData();

Laravel passing variable from multiple models

I have 2 models
User
Customer
Each user hasMany customers (aka Accounts)
the user model has
public function customer() {
return $this->hasMany('App\Customer', 'id','customer_id');
}
but when i try to output this:
$user->customer->name
i get an error saying:
Undefined property: Illuminate\Database\Eloquent\Collection::$name
When i use this one, i get the entire customers record output in JSON.
$user->customer
So the relationship is working, but obviously i am missing something. I was sure this was the right way to do it. I swear this worked in Laravel4.2 but now that I'm in Laravel 5 it doesn't.
As the error and the below link says, It is returning a collection (hasMany) not a single object, you need to either loop over the collection or do direct access in the collection based on the index.
that is to say... does doing this work?..
$user->customer[0]->name
http://laravel.io/forum/04-10-2014-undefined-property-illuminatedatabaseeloquentcollectionitem
You should loop over customers, if you want display customers in laravel view you can do the following
#foreach
($user->customer as $cust)
{{$cust->name}}
#endforeach

Resources