How do I reload a relation collection in laravel

0 votes

In laravel, after using attach() or detach() to add or remove something from a relation, the collection has not changed. So if I have a model whose realation contains [1, 2], after this:

$model->relation()->detach(1);
$model->relation()->attach(3);

it will still contain [1, 2]! How do I refresh it?

Nov 4, 2020 in PHP by kartik
• 37,520 points
6,825 views

1 answer to this question.

0 votes

You can easily tell laravel to load a relation with a single command:

$model->load('relation');

Will tell it to refresh the relation collection, and $model->relation will now show the correct values.

Also unloading a relation will be like this:

$model->unsetRelation('relation')
answered Nov 4, 2020 by anonymous
• 82,880 points

Related Questions In PHP

0 votes
1 answer

How do I convert a PDF document to a preview image in PHP?

Hello @kartik, You need ImageMagick and GhostScript <?php $im = new imagick('file.pdf[0]'); $im->setImageFormat('jpg'); header('Content-Type: image/jpeg'); echo ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,880 points
4,756 views
0 votes
1 answer

How do I recursively delete a directory and its entire contents files as well as sub dirs in PHP?

Hello @kartik, The user-contributed section in the manual ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,880 points
1,084 views
0 votes
1 answer

How do I check whether a checkbox is checked in jQuery?

Hello @kartik, The checked property of a checkbox DOM element ...READ MORE

answered Aug 28, 2020 in PHP by Niroj
• 82,880 points
865 views
0 votes
1 answer

How do I make a redirect in PHP?

Hello @kartik, Use the header() function to send an HTTP Location header: header('Location: '.$newURL); Contrary to ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,880 points
693 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
22,960 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,969 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,900 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
44,400 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

Hello @kartik, In SQL Server, use MERGE MERGE INTO YourTable ...READ MORE

answered Jul 21, 2020 in PHP by Niroj
• 82,880 points
929 views
0 votes
1 answer

How do I escape a single quote in SQL Server?

Hello @kartik, Single quotes are escaped by doubling ...READ MORE

answered Jul 21, 2020 in PHP by Niroj
• 82,880 points
6,192 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP