i have table "task" and relationship many to many with itself
public function depends_on() //parents
{
return $this->hasMany(DependentTask::class, 'task_id');
}
public function dependents() //kids
{
return $this->hasMany(DependentTask::class, 'dependent_id');
}
the relationship may be like this
i want to select the tasks like this
first print 5 or 2
then 3 and 6
then 4 and 1
how can i do it by laravel?
This is how you can use recursive relations:
public function dependsOn()
{
return $this->hasMany('Account', 'task_id','dependent_id');
}
public function dependents()
{
return $this->dependsOn()->with('dependents');
}
Then:
$dependents = Dependents::with('dependents')->first();
$dependents->dependents;
$dependents->dependents->first()->dependents; // .. and so on
Related
I have two relationships on the users' table:
public function tokens_records()
{
return $this->hasManyThrough(Record::class, Token::class);
}
and
public function websites_records()
{
return $this->hasManyThrough(Record::class, Website::class);
}
How can I merge both queries into one?
If you want to have the records for both relationships, this is gonna work:
class Entity extends Model
{
public function tokens_records()
{
return $this->hasManyThrough(Record::class, Token::class);
}
public function websites_records()
{
return $this->hasManyThrough(Record::class, Website::class);
}
}
$records = $entity->tokens_records
->merge($entity->websites_records);
I'm currently working on a laravel project, but I'm kind of stuck finding the right eloquent relations.
My tables and the connections (should) look like this:
Project Relations
My model relations look like this:
User
public function team()
{
return $this->hasMany(Team::class, 'user_id');
}
public function evaluation()
{
return $this->hasMany(Evaluation::class, 'user_id');
}
Team
public function user()
{
return $this->belongsTo(User::class);
}
public function survey()
{
return $this->hasMany(Survey::class, 'team_id');
}
Evaluation
public function user()
{
return $this->belongsTo(User::class);
}
public function survey()
{
return $this->hasMany(Survey::class, 'evaluation_id');
}
Survey
public function team()
{
return $this->belongsTo(Team::class);
}
public function evaluation()
{
return $this->belongsTo(Evaluation::class);
}
public function surveyresponse()
{
return $this->hasMany(SurveyResponse::class, 'survey_id');
}
SurveyResponse
public function survey()
{
return $this->belongsTo(Survey::class);
}
public function testquestion()
{
return $this->belongsTo('App\TestQuestion');
}
Is this the way to go? Do I need a "Has Many Through" relation here? Or a "Polymorphic Relationship"?
Seems correct to me, i just didnt see the TesteQuestion model (your last relation).
Answering your question:
The HasManyThrough relation is just a shortcut for accessing distant relations via an intermediate relation, in your case: Users has many evaluations that has many surveys. With this relationship you could get all surveys from a user.
Your relation would look like this:
/**
* Get all of the surveys for the user.
*/
public function surveys()
{
return $this->hasManyThrough('App\Survey', 'App\Evaluation');
}
You can access this relation like this:
$user->surveys();
But you can achieve the same (without using the HasManyThrough) by doing:
$user->evaluations()->surveys();
Beware that this will return the evaluations too, not just the surveys and it requires more processing.
So i recommend you doing the HasManyThrough relationship if you pretend to access the surveys a lot.
i am trying to connect 4 tables together:
StepAnimation Model:
public function steps()
{
return $this->hasManyThrough('App\Models\Step','App\Models\WorkSequence');
}
public function step()
{
return $this->belongsTo('App\Models\Step');
}
Step Model:
public function workSequence()
{
return $this->belongsTo('App\Models\WorkSequence');
}
public function stepAnimation()
{
return $this->hasMany('App\Models\StepAnimation');
}
WorkSequence Model:
public function categoryWorkSequence()
{
return $this->hasMany('App\Models\CategoryWorkSequence');
}
public function step()
{
return $this->hasMany('App\Models\Step');
}
CategoryWorkSequence Model:
public function workSequence()
{
return $this->belongsTo('App\Models\WorkSequence');
}
I am currently selecting all the work sequences that have a step animation:
$this->stepAnimation = New StepAnimation();
$workSequence = $this->stepAnimation::with('step.workSequence')->get();
But now id like to attach all the categories to the worksequence, how do i need to extend the select for that to happen?
Then you can continue selecting the category relationship in your workSequence as long as it is available on your model. Like this.
$this->stepAnimation = New StepAnimation();
$workSequence = $this->stepAnimation::with('step.workSequence.categoryWorkSequence')->get();
Got a domain table which has a One To Many relationship with domain_hosts_table, server_hosts_table and systems_table. So far so good.
Calling the table data:
$domains = Domain::with('domain_host', 'server_host', 'system')->get();
Domain model :
public function domain_host()
{
return $this->hasOne('App\DomainHost', 'id');
}
public function server_host()
{
return $this->hasOne('App\ServerHost', 'id');
}
public function system()
{
return $this->hasOne('App\System', 'id');
}
DomainHost, ServerHost, System model :
public function domains()
{
return $this->hasMany('App\Domain');
}
Domains table :
So far so good.
Let's take a look at what this particular table returns while being foreached.
The first 2 rows should be the same (basing on their IDs), and all rows after the first 2 are just empty.
(dd of the fetched data, notice the relations being empty at 4th object, 1st object actually has data).
Had to define another parameter when defining my relationships:
public function domain_host()
{
return $this->hasOne('App\DomainHost', 'id', 'domain_host_id');
}
public function server_host()
{
return $this->hasOne('App\ServerHost', 'id', 'server_host_id');
}
public function system()
{
return $this->hasOne('App\System', 'id', 'system_id');
}
It was looking for the ID of the current row in the other table.
I am a bit stuck on this...
I have 3 tables: photographers, languages and languages_spoken (intermediate table).
I am trying to retrieve all the languages spoken by a photographer. I defined my models like this:
class Photographer extends Eloquent {
/**
* Defining the many to many relationship with language spoken
*
*/
public function languages() {
return $this->belongsToMany('Language', 'languages_spoken', 'language_id', 'photographer_id');
}
class Language extends Eloquent {
/**
* Defining the many to many relationship with language spoken
*
*/
public function photographers() {
return $this->belongsToMany('Photographer', 'languages_spoken', 'language_id', 'photographer_id')
->withPivot('speakslanguages');
}
This is how I was trying to retrieve all the results for the logged in photographer:
$photographer = Photographer::where('user_id', '=', $user->id);
if ($photographer->count()) {
$photographer = $photographer->first();
// TEST
$spokenlang = $photographer->languages;
die($spokenlang);
// END TEST
} else {
return App::abort(404);
}
The problem is that in my db I have 4 entries for the same photographer. but when I do this I only get the last result...
[{"id":"3","language_name":"Afrikaans","updated_at":"-0001-11-30 00:00:00","created_at":"-0001-11-30 00:00:00","native_name":"Afrikaans","ISO639_1":"af","pivot":{"language_id":"3","photographer_id":"3"}}]
Any idea on what is wrong ?
Thanks a lot for your help!!!
The third parameter to belongsToMany should be the foreign key.
In the Photographer class:
public function languages() {
return $this->belongsToMany('Language', 'languages_spoken', 'language_id', 'photographer_id');
}
...should be:
public function languages() {
return $this->belongsToMany('Language', 'languages_spoken', 'photographer_id');
}
In the Language class:
public function photographers() {
return $this->belongsToMany('Photographer', 'languages_spoken', 'language_id', 'photographer_id')
->withPivot('speakslanguages');
}
Should be:
public function photographers() {
return $this->belongsToMany('Photographer', 'languages_spoken', 'language_id')
->withPivot('column1', 'column2', 'column3'); // withPivot() takes a list of columns from the pivot table, in this case languages_spoken
}
But, since you're not even using strange keys, you don't need to pass that third parameter at all.
So this is just fine:
public function languages() {
return $this->belongsToMany('Language', 'languages_spoken');
}
And:
public function photographers() {
return $this->belongsToMany('Photographer', 'languages_spoken')
->withPivot('column1', 'column2', 'column3'); // withPivot() takes a list of columns from the pivot table, in this case languages_spoken
}