How to get modified relationship laravel

laravel relationships
laravel count relationship
laravel relationship tutorial
laravel eloquent join
polymorphic relationship
laravel eloquent methods
laravel collection
belongstomany in laravel

We have the models: Question and Answer.

In the answers table, we have a boolean column with the name accepted and we have just one accepted Answer. We want to get question with its answers and accept answer separately.

I use two approaches, but I don't know which is better:

1). check if the relation is loaded if loaded filter relations and get an accepted answer like this:

if(!$this->relationLoaded('answers')){
            $this->load('answers');
        }
        return $this->answers->firstWhere('accepted',true);

2). We can use this approach too:

function accepted_answer(){
  return $this->answer()->where('accepted',true);
}

The problem of the second approach is we get an array, but we expect a single response or null. Is there any better approach to this or we can use neither 1 or 2?


Create an Accessor on Question model.

class Question extends Model
{
    public function getSelectedAnswerAttribute()
    {
        return $this->answers->where('accepted', true)->first();
    }
}

now in every Answer instance has an attribute called selected_answer.

You can access it like,

$answer->selected_answer;

Note

this 'selected_answer' is not fetching from database, it filters from already fetched answers list. so its more efficient than your 2nd approach.

and, If you want this attribute automatically bind to the Question model always. add this accessor to appends array.

Model

protected $appends = ['selected_answer'];

Can I modify a model's values on a relationship before returning the , I use two approaches, but I don't know which is better: 1). check if the relation is loaded if loaded filter relations and get an accepted answer like  Browse other questions tagged mysql laravel laravel-query-builder or ask your own question. Blog How Shapeways’ software enables 3D printing at scale


create accepted scope at your answer model.

public function scopeAccepted($query){
   return $query->where("accepted",true)->first();
}

then you can easly retreive first accepted answer.

$question->answer()->accepted();

for more further check out query scopes;

10 Hidden Laravel Eloquent Features You May Not Know, I also have a Classroom model which has a relationship that gets all t Laravel really should change that default away from a single column. How to use Laravel Eloquent Relations 0 For databases that work on basis of relations i.e rdbms you have to define structure of your database before inserting any data to it .


You should go with the second approach as you will have a single source to change later on if something changes (instead of digging through a lot of controllers)

To get the result, use ->get(); at the end of builder query.

P.S: Scopes are generally always better than writing queries manually inside the controller

Eloquent ORM - Laravel, After mutating an Eloquent record you can get the original attributes by Determine if the model or given attribute have been modified using  Eloquent: Get the Latest Row from Relationship. Another “hidden gem” of Eloquent. Let’s imagine that you want to query all rows from the table and also one related row – not all of them, but the newest one. Apparently, it’s pretty easy. Let’s take a real-life example.


Eloquent: Getting Started - Laravel, To get started, create an Eloquent model. Models If user input is blindly passed into a model, the user is free to modify any and all of the model's attributes. Laravel 5 provide great feature as model relationship. but if you need to use where clause on your relation model then how you can do it?, You can make where condition using whereHas function. it doesn't matter which relation you used like one to one, one to many, many to many, has many through etc.


Eloquent: Mutators - Laravel, To get started, let's create an Eloquent model. Models The isDirty method determines if any attributes have been changed since the model was loaded. In this tutorial, i would like to explain one to one model relationship in laravel. One to One model relationship is very simple and basic. you have to make sure that one of the table has a key that references the id of the other table. we will learn how we can create migration with foreign key schema, retrieve records, insert new records, update records etc.


Eloquent: Relationships - Laravel, You may also generate Eloquent models using the make:model command: If user input is blindly passed into a model, the user is free to modify any and all of​  One to Many relationship will use when one table associated with multiple tables. For example, a post may have multiple comments. So in this tutorial, you can understand how to create migration with a foreign key schema for one to many relationships, use sync with a pivot table, create records, get all data, delete, update and everything related to one to many relationships.