Comments (9)
Hmm, this might not be an issue after all. It only seems to want to do it during seeding the database.
from magniloquent.
Ahhh, so it doesn't work in the seeding because I had Eloquent::unguard();
set
from magniloquent.
I can't seem to recreate it. Here's the test code I just used to try to raise the error. Is yours similar?
class UserTableSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
DB::table('users')->delete();
$alex = User::create(
[
'name' => 'Alex Sears',
'password' => Hash::make('alex')
]
);
$post = new Post(
[
'name' => 'Test Post 1'
]
);
$alex->posts()->save($post);
}
}
from magniloquent.
If you're looking to update the owning user of a post, then you would want to use the associate()
method. Since the foreign key should not be null in a one-to-many relationship like this, you should not be able to save a post without a user ID. Therefore, when creating a new post use the save()
method.
$post = new Post(['name' => 'Test Post']);
$user->posts()->save($post);
When updating the owner of a post, use the associate()
method to update the foreign key on the Post model.
$user = User::find(3);
$post = Post::find(1);
$post->user()->associate($user);
$post->save();
from magniloquent.
My User
model has a belongsToMany
relationship with a Cribbb
model.
I create a new User
and then try and add it to a Cribbb
like this:
class UserTableSeeder extends Seeder {
public function run()
{
$user = User::create(array(
'username' => 'philipbrown'.time(),
'first_name' => 'Philip',
'last_name' => 'Brown',
'email' => 'phil@'.time().'.com',
'password' => 'qwerty'
));
$cribbb = Cribbb::find(1);
$user->cribbbs()->save(array());
}
}
Looks like it comes from the save
method on https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
from magniloquent.
I have an idea for fixing it, but I won't be home til a bit later. I'll push them up tonight.
On Nov 29, 2013, at 12:52 PM, Philip Brown [email protected] wrote:
My User model has a belongsToMany relationship with a Cribbb model.
I create a new User and then try and add it to a Cribbb like this:
class UserTableSeeder extends Seeder {
public function run()
{
$user = User::create(array(
'username' => 'philipbrown'.time(),
'first_name' => 'Philip',
'last_name' => 'Brown',
'email' => 'phil@'.time().'.com',
'password' => 'qwerty'
));$cribbb = Cribbb::find(1);
$user->cribbbs()->save(array());
}}
Looks like it comes from the save method on https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php—
Reply to this email directly or view it on GitHub.
from magniloquent.
Awesome :)
from magniloquent.
Just pushed up a fix for it. It will pull it out of the attributes and then pass it to the parent::save()
method like it should do.
from magniloquent.
Excellent :) Thanks Alex!
from magniloquent.
Related Issues (16)
- Attributes without rules don't get saved HOT 1
- Validation auto hash double hashes database seeders HOT 2
- Issue with Mockery HOT 4
- Support for Laravel 4.1 HOT 3
- Weird isValid behaviour HOT 2
- Support for multiple database connections HOT 4
- On Update, no returned validation messages HOT 2
- Nice names in validation HOT 4
- Make mergeRules protected? HOT 1
- If a 'unique' rule is included all other rules are lost HOT 1
- Saving a single value HOT 2
- What i'm missing in autoHash()? HOT 1
- Extending this class breaks Laravel remember_token functionality HOT 6
- composer.json and "dev-master" HOT 3
- autoHash - Doesn't Hash Password When Set To Same Plaintext HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from magniloquent.