Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

I'm going to assume your using the app/database/seeds/DatabaseSeeder.php which loads the other seeder classes.

Try adding Eloquent::unguard(); to that file. It disables the mass assignment protection which should allow your seeder to run without the error.

https://github.com/laravel/laravel/blob/master/app/database/se...

class DatabaseSeeder extends Seeder {

	/**
	 * Run the database seeds.
	 *
	 * @return void
	 */
	public function run()
	{
		Eloquent::unguard();

		// $this->call('UserTableSeeder');
	}

}
Last updated 1 year ago.
0

That's already there. Probably should've mentioned. So yeah. Really lost.

Last updated 1 year ago.
0

Humm, ok.

What about the loading order in the seeder file?, you have a foreign key with manufacturer so those records would need to be created before anything that is referencing those records.

Trying to think of other things it could be ....

Last updated 1 year ago.
0

Yeah, I have that much logical sense at least... It's ManufacturerTableSeeder, ModelTableSeeder, ListingsTableSeeder. The Manufacturers and Models seed fine. The Listings seed fine too, but like I said, only in isolation.

Last updated 1 year ago.
0

Are my relationships set up ok? I feel like they might be a little trying... the Manufacturer/Listing one, the Manufacturer hasManyThrough Model, but then this is what I have for the manufacturer() method in my Listing model:

public function manufacturer()
{
	//return $this->belongsTo('Manufacturer', 'models.manufacturer_id');
	return $this->belongsTo('Model', 'model_id')
		->join('manufacturers', 'manufacturers.id', '=', 'models.manufacturer_id');
	/*
	$manufacturer_id = $this->model->manufacturer_id;
	return Manufacturer::find($manufacturer_id)->name;*/
}

See the mess and what I ended up with in the middle. Was using the top line first, then that ended up not working... forget how... so just to get the name of the manufacturer I did the latter lines, but I wanted to rightfully establish the relationship so someone suggested I use that middle route. But that's saying belongsTo Model, right? When it already belongs to a Model... this isn't the Listing-Model relationship, it's the Listing-Manufacturer, so is that correct? How would you do it? I figured since it's not many-to-many a pivot table isn't necessary.

Last updated 1 year ago.
0

I didn't get a chance to revisit your code sooner. It's been a bit crazy here.

So I created a dev-site then set up the migrations and did some testing on the design. I didn't see anything wrong with the listings->manufacturer(join) as it was a way to return the manufacturer data without the model data rows.

Here is what I ended up with.

Data structure

Models (ties to) Listings
Models (ties to) Manufacturer

Listings (ties to) Models
Listings (ties to Manufacture) via Models->join

Manufacturer (ties to) Listings
Manufacturer (ties to) Models

Ok, so that's relationships - now to get the data :

Via listings ...

- just listings
Listings::get();

- listings and models
Listings::with('models')->get();

- listings and manufacturer
Listings::with(manufacturer)->get();

- listings with models and manufacturer
Listings::with('models')->with('models. manufacturer)->get();

- or just the child relation which will load the models also load the model 
Listings::with('models. manufacturer)->get();

Via models ..

-- just models 
Models::get();

-- models and listings
Models::with('listings')->get();

-- models and manufacturer
Models::with(manufacturer)->get();

-- models with listing and manufacturer
Models::with('listings')->with(manufacturer)->get();

Via manufacturer ...

-- just manufacturer
Manufacturers::get();

-- just manufacturer and models
Manufacturers::with('models')->get();

-- just manufacturer and listings
Manufacturers::with('listings')->get();

-- just manufacturer with listings and models
Manufacturers::with('models')->with('listings')->get();

I think that covers most of the data fetching paths.

This all looks ok to me. You have several ways to get the data, with or without the related rows.

For the seeder error, try this to bypass the "Integrity constraint violation":

public function run()
{
                // disable the foreign key check 
	DB::statement('SET FOREIGN_KEY_CHECKS=0;');

	// detele / truncate tabledata
    	DB::table('listings')->delete();

	// create data here
                Listing::create([…

	// renable the foreign keys
	DB::statement('SET FOREIGN_KEY_CHECKS=1;');
}

Hope that helps

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

chimmel chimmel Joined 4 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.