Laravel 5 One-to-many relationship master and parent data CRUD

Mohaimin Moin

I have a purchase table with date and comments and a purchasedetail table with purchaseId as a foreign key of purchase table. Have the relation in both model table is :

Purchase model:-

use Illuminate\Database\Eloquent\Model;
class Purchase extends Model {
public function purchasedetails() {
    return $this->hasMany('App\Models\Purchasedetail', 'PurchaseId', 'Id');
}
  }

and the
Purchasedetail model :-

use Illuminate\Database\Eloquent\Model;
class Purchasedetail extends Model {
public function purchase() {
    return $this->belongsTo('App\Models\Purchase', 'PurchaseId', 'Id');
}
}

How can I create, update and delete both table data with relations? My main problem is when I send data to controller and try to create, master page data is created but it can not create parent table data or update it. I am using Laravel 5.

Some code of my controller:

$purchase = new Purchase();
    $purchase->Date = Input::get('Date');
    $purchase->Comment = Input::get('Comment');
    $purchase->purchasedetails = Input::get('Purchasedetail');
$purchase-> save();

if (count($purchase->purchasedetails) != 0) {
foreach ($purchase->purchasedetails as $v) {
    $purchasedetail = new Purchasedetail();
    $purchasedetail->ItemId = $v->ItemId;
return $v->ItemId;
    }
}
$purchase-> Purchasedetail()->save($purchasedetail);

I get this error in return:

ErrorException in helpers.php line 703: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array. That means $purchase->save() can not possible. Here i get an array list from Input::get('Purchasedetail').

So what can I do now?

Peter Kota

Try the following:

$purchase = new Purchase();
$purchase->Date = Input::get('Date');
$purchase->Comment = Input::get('Comment');

$purchase-> save();

$purchase->purchasedetails = Input::get('Purchasedetail');
foreach ($purchase->purchasedetails as $row) {
    $purchasedetail = new Purchasedetail();
    $purchasedetail->ItemId = $row['ItemId'];
    $purchasedetail->GradeId = $row['GradeId'];
    $purchasedetail->Quantity = $row['Quantity'];
    $purchase->purchasedetails()->save($purchasedetail);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

laravel update, remove one to many relationship eloquent

From Dev

Core data and one to many relationship

From Dev

one to many relationship help in laravel 4

From Dev

Laravel save one to many relationship

From Dev

Laravel Eloquent - Save/Update Related One-To-Many Relationship Data

From Dev

one to many relationship in Laravel5 is not working properly

From Dev

Laravel 5 Deleting a one-to-many relationship

From Dev

Laravel: one to many relationship

From Dev

Laravel - One to Many relationship is not working one way

From Dev

Empty data returning from many to many relationship laravel 5

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel Many to many relationship get specific data

From Dev

entity framework 5, delete one to many relationship?

From Dev

How express the many to many relationship with single parent table in EF 5

From Dev

Laravel Eloquent - Fetch data only if there is a match with data one to many relationship

From Dev

Laravel Removing Pivot data in many to many relationship

From Dev

Laravel 5 one to many eloquent relationship

From Dev

Laravel 5 return column values in one to many relationship

From Dev

Laravel 5.1: Many-to-many alongside one-to-many relationship

From Dev

Laravel 5 | Many to Many Relationship not working

From Dev

Laravel - One to Many relationship is not working one way

From Dev

one to many relationship in laravel

From Dev

Laravel Many-to-one relationship

From Dev

Laravel insert in one to many relationship

From Dev

How to query only one foreign row in a one-to-many relationship in Laravel 5

From Dev

How to get data for one to many relationship using Eloquent in Laravel?

From Dev

One to Many to One relationship in laravel eloquent

From Dev

Seeding Relationship one to many in Laravel

From Dev

Laravel Get data One to Many Relationship with conditions

Related Related

  1. 1

    laravel update, remove one to many relationship eloquent

  2. 2

    Core data and one to many relationship

  3. 3

    one to many relationship help in laravel 4

  4. 4

    Laravel save one to many relationship

  5. 5

    Laravel Eloquent - Save/Update Related One-To-Many Relationship Data

  6. 6

    one to many relationship in Laravel5 is not working properly

  7. 7

    Laravel 5 Deleting a one-to-many relationship

  8. 8

    Laravel: one to many relationship

  9. 9

    Laravel - One to Many relationship is not working one way

  10. 10

    Empty data returning from many to many relationship laravel 5

  11. 11

    Laravel 5 Eloquent count many to many relationship

  12. 12

    Laravel Many to many relationship get specific data

  13. 13

    entity framework 5, delete one to many relationship?

  14. 14

    How express the many to many relationship with single parent table in EF 5

  15. 15

    Laravel Eloquent - Fetch data only if there is a match with data one to many relationship

  16. 16

    Laravel Removing Pivot data in many to many relationship

  17. 17

    Laravel 5 one to many eloquent relationship

  18. 18

    Laravel 5 return column values in one to many relationship

  19. 19

    Laravel 5.1: Many-to-many alongside one-to-many relationship

  20. 20

    Laravel 5 | Many to Many Relationship not working

  21. 21

    Laravel - One to Many relationship is not working one way

  22. 22

    one to many relationship in laravel

  23. 23

    Laravel Many-to-one relationship

  24. 24

    Laravel insert in one to many relationship

  25. 25

    How to query only one foreign row in a one-to-many relationship in Laravel 5

  26. 26

    How to get data for one to many relationship using Eloquent in Laravel?

  27. 27

    One to Many to One relationship in laravel eloquent

  28. 28

    Seeding Relationship one to many in Laravel

  29. 29

    Laravel Get data One to Many Relationship with conditions

HotTag

Archive