hasOne relationship not saving

Published 3 months ago by Sidirgot

Hello, well i been trying to figure out this issue for the past hour without any luck!!

I have a product table and a Manufacturers table in my db with a hasOne relationship declared like this:

Product Model

public function manufacturers()
 {
      return $this->hasOne('App\Manufacturers');
 }

Manufacturer Model

class Manufacturers extends Model
{
   public function product()
   {
         return $this->belongsTo('App\Product');
       }
}

Now i am getting the manufacturer option from a select field i have and the value comes as the manufacturer name and i am trying to save the relationship like this

$product->Manufacturers()->save($request->manufacturer);

And i been getting this error

"Type error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, string given, called in C:\xampp\htdocs\backend-ecom\app\Http\Controllers\Backend\ProductController.php on line 79

Anyone has any idea, it's been driving me crazy!

Best Answer (As Selected By Sidirgot)
Snapey

As you have a belongsTo relationship in your Manufacturers model (should be Manufacturer) then you should have a product_id column in the manufacturers table, and no foreign key in the products table.

If you do this $product->Manufacturers() then you need to match the case of the relationship not the model, eg $product->manufacturers()

However, I assume a manufacturer can have many products, so I would expect more like

Product.php (table=products) has manufacturer_id column

public function manufacturer()
{
    return $this->belongsTo(Manufacturer::class);
}

Manufacturer.php (table=manufacturers)

public function products()
{
    return $this->hasMany(Product::class);
}
Bakanyaka

You need to retrieve Manufacturers model first. For example:

$product->Manufacturers()->save(App\Manufacturers::where('name', $request->manufacturer)->first());
Sidirgot

@Bakanyaka well to tell you the trouth what i did was this

$manufacturer = Manufacturers::where('name',$request->manufacturer)->first();
    $product->manufacturer_id = $manufacturer->id;

But i thought it was wrong and maybe there was another way to go!

Thanks for the reply mate

Snapey
Snapey
3 months ago (897,215 XP)

As you have a belongsTo relationship in your Manufacturers model (should be Manufacturer) then you should have a product_id column in the manufacturers table, and no foreign key in the products table.

If you do this $product->Manufacturers() then you need to match the case of the relationship not the model, eg $product->manufacturers()

However, I assume a manufacturer can have many products, so I would expect more like

Product.php (table=products) has manufacturer_id column

public function manufacturer()
{
    return $this->belongsTo(Manufacturer::class);
}

Manufacturer.php (table=manufacturers)

public function products()
{
    return $this->hasMany(Product::class);
}

Please sign in or create an account to participate in this conversation.