John Davidson

php - Laravel inserting data in one to many tables

0 comments
Message:


Good morning developers, iam working on ecommerce project using laravel and angular
in laravel i made two tables ( brands - products ) and i need the brand has many products
so i added this function to the Product model


public function brand()
{
return $this->belongsTo(Brand::class);
}

and i added this function to Brand model


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

the first question is how i could handle the relation in the products table migration file, is this right or there is better way?


$table->unsignedBigInteger('brand_id');
$table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade');

the second questions is should i define the brand_id in the $fillable products property or not


protected $fillable = ['name', 'brand_id', 'display', 'ram', 'storage', 'rear_cam', 'front_cam', 'img', 'price'];

the third question is how to handle the store product function and note iam using api calls between angular and laravel


public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:100', 'display' => 'required|string|max:100', 'ram' => 'required|string|max:100', 'img' => 'required|image|mimes:jpg,png',
'storage' => 'required|string|max:100', 'rear_cam' => 'required|string|max:200', 'front_cam' => 'required|string|max:200',
'price' => 'required'
]);

if ($validator->fails()) {
$errors = $validator->errors();
return response()->json($errors);
}

$img = $request->file('img');
$ext = $img->getClientOriginalExtension();
$name = 'product-' . uniqid() . ".$ext";
$img->move(public_path('uploads/books/'), $name);

$name = $request->name;
$brand_id = $request->brand_id;
$display = $request->display;
$ram = $request->ram;
$storage = $request->storage;
$rear_cam = $request->rear_cam;
$front_cam = $request->front_cam;
$price = $request->price;
$product = Product::create(['name' => $name, 'brand_id' => $brand_id, 'display' => $display, 'img' => $name, 'ram' => $ram, 'storage' => $storage, 'rear_cam' => $rear_cam, 'front_cam' => $front_cam, 'price' => $price]);

$product->brand()->sync($request->brand_id);

$success = 'Product created successfully';

return response()->json($success);
}

when iam trying to store a product it says there is error with the sync function, what function should i use instead of it



My Answer:
belongsTo('App\Brand');_}_and this function to the Brand model_public function products()_{_ return $this->hasMany('App\Product');_}_now in my controller i want to insert data into both tables at the same time, here is an example of how to do it:$brand = new Brand; $brand->name = 'Nike'; $brand->save(); $product = new Product; $product->name = 'Shoes'; $product->price = 100; $product->brand_id = $brand->id; $product->save();_this way the product will be associated with the brand automatically because of the relationship we defined in the models. Hope this helps! Let me know if you have any questions.

Rate this post

5 of 5 based on 5307 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us