Giter Site home page Giter Site logo

Comments (9)

philipbrown avatar philipbrown commented on July 23, 2024

Hmm, this might not be an issue after all. It only seems to want to do it during seeding the database.

from magniloquent.

philipbrown avatar philipbrown commented on July 23, 2024

Ahhh, so it doesn't work in the seeding because I had Eloquent::unguard(); set

from magniloquent.

w3irdrobot avatar w3irdrobot commented on July 23, 2024

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.

w3irdrobot avatar w3irdrobot commented on July 23, 2024

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.

philipbrown avatar philipbrown commented on July 23, 2024

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.

w3irdrobot avatar w3irdrobot commented on July 23, 2024

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.

philipbrown avatar philipbrown commented on July 23, 2024

Awesome :)

from magniloquent.

w3irdrobot avatar w3irdrobot commented on July 23, 2024

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.

philipbrown avatar philipbrown commented on July 23, 2024

Excellent :) Thanks Alex!

from magniloquent.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.