Skip to content

Commit

Permalink
add one to many relation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mdabdullah3 committed Jan 6, 2024
1 parent f9122bb commit 51c9704
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
1 change: 0 additions & 1 deletion app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ProductController extends Controller
*/
public function index()
{
//
}

/**
Expand Down
8 changes: 5 additions & 3 deletions app/Models/picture.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class picture extends Model
class Picture extends Model
{
use HasFactory;

protected $fillable = ['path'];
public function products(): BelongsTo

public function product(): BelongsTo
{
return $this->belongsTo(product::class);
return $this->belongsTo(Product::class);
}
}
33 changes: 33 additions & 0 deletions app/Models/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,41 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class product extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'category_id',
'subCategory_id',
'name',
'description',
'quantity',
'sprice',
'pprice',
'discount',
'size_id',
];
public function size()
{
return $this->belongsTo(size::class);
}
public function subCategory()
{
return $this->belongsTo(SubCategory::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function pictures(): HasMany
{
return $this->hasMany(picture::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function up(): void
$table->decimal('pprice', 8, 2);
$table->decimal('discount', 8, 2)->nullable();
$table->foreignId('size_id')->constrained('sizes')->onDelete('restrict');
// Remove the following line
$table->foreignId('picture_id')->constrained()->onDelete('cascade');
$table->timestamps();
});
Expand Down

0 comments on commit 51c9704

Please sign in to comment.