Support the ongoing development of →
Database Eloquent
Last updated 2 years ago.

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.

class DatabaseSeeder extends Seeder {

	 * Run the database seeds.
	 * @return void
	public function run()

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

Last updated 2 years ago.

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

Last updated 2 years ago.

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 2 years ago.

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 2 years ago.

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', '', '=', '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 2 years ago.

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 and models

- listings and manufacturer

- 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 and listings

-- models and manufacturer

-- models with listing and manufacturer

Via manufacturer ...

-- just manufacturer

-- just manufacturer and models

-- just manufacturer and listings

-- just manufacturer with listings and models

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

	// create data here

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

Hope that helps

Last updated 2 years ago.

Sign in to participate in this thread!


Your banner here too?

chimmel chimmel Joined 4 Aug 2014


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

Your logo here?

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

© 2024 - All rights reserved.