yesterday i asked a question about Eager Loading and Form Model Binding.
Laravel Eager Loading and Form Model Binding
This is somehow a followup question.
Now i want to update the record in the Database
$user = \User::findOrFail($id);
$user->fill(\Input::all());
$user->push();
But this dose only save the data of the user itself. Not the relations
user = saved
user->profile = not saved
on my user model i have a fillable array with all fillables columns. and on other models like the profile model i just wrote protected $fillable = ['*'];
The Soultion:
call fill() on the relations not just the user model.
$user->fill($input)
$user->profile->fill($input['profile'])
You're not retrieving profile with your user model.
$user = \User::with('profile')->findOrFail($id);
That should give you the profile properties to update.
Related
I just want to get hasOne relation of New model.
Can I load it before I will save it.
I have $user = new User(['order_id' => $order_id]);
How can I get $user->order; without build new request?
As others have already pointed out the model must be saved before retrieving any relations.
My usual approach for this is using lazy eager loading:
// Create new model with data
$newModel = Model::create([
'column' => $value
]);
// Use load to retrieve any relations
return $newModel->load('myRelationship');
I have question about Laravel Eloquent.
I have following tables
Users
-id
-email
Money
-id
-user_id
-amount
-total
User has many Money
I want to use something like $user->current_money
then i want to get the total from the last Money
i need this because i want to show all user's current money in table
#foreach($users as $user)
<tr>
<td>{{$user->email}}</td>
<td>{{$user->current_money}}</td>
</td>
#endforeach
is there good practice for this?
Thanks
I like to use appends in Laravel Eloquent in order to achieve this.
in your Users model get add the $appends array. like this
protected $appends = ['current_money'];
this will look for a method getCurrentMoneyAttribute() in the User model. it should look like this.
public function getCurrentMoneyAttribute()
{
return 0.00;
}
with the implication of the you have implanted the relationship between User and Money tables. your method should look like this,
public function getCurrentMoneyAttribute()
{
return $this->money()->latest()->first()->total;
}
and when when ever you call the $user->current_money laravel execute the query and it will get the last row of the Money that is relevant to that user.
You can use Eloquent Relationships as you are asking about and a single query to accomplish this. Using append is a slippery slope because now it will add it to all user queries and may cause bloat as the app grows. As well appending it will have 2 queries versus a single one.
In the user model you define the relationship like so:
/**
* Get money for a user
*/
public function money()
{
return $this->hasMany('App\Money', 'user_id', 'id');
}
Then you can query the user like this in a single query for performance:
$user = User::where('id', $id)->with(['money'])->first();
or
$user = User::with('money')->findOrFail($id);
or now you can eager load the money as well since the relationship is now defined in the user model.
$user = Auth::user(); // or however you are pulling the user from the DB.
$user->money;
Then loop through the different money's.
Is there a way to die & dump the Model and see all relationships with dd() or var_dump()?
So i can see everything what is connected to it?
If you load model with relation, for example:
$collection = Model::with('relation', 'nested.relation')->get();
When you'll do dd($collection); you'll see all relation models there.
I have a question. Activity model and User model have many-to-many relationships. I know I can get pivot model like this:
$activities = $user->activities;
foreach($activities as $activity)
{
$pivot = $activity->pivot;
}
But now I already have two model instance:$user and $activity .Get them by id individually. Not access from their relationship. So I want to know is there a method to get their pivot model?
Its not necessary for you to call the relationship chain to access pivot, you can query individual model and access pivot too,
$model = Activity::find(1); //get the activity model by primary key
$model->pivot->activityId;
I'm using a database with images as blob data and I want to ignore the "image" field of the table/model by default but be able to use the field later. ¿Is this possible?.
You can use this in your model, for example User model:
protected $hidden = array('image');
If you use this:
$model = User::find(1);
dd($model->toArray());
You'll not see the image field but if you use this:
dd($model->image);
Then you'll see that.