mercredi 29 novembre 2017

how to save data to pivot table in laravel 5.5?

I have 4 tables:

advertises
id, title, description, thumbnail, category_id, subcategory_id
categories
id, name
subcategories
id, name, category_id
category_subcategory
cateogory_id
subcategory_id

I want to create and save an advertise with its category and subcategory, for example: advertise title: buy a phone with javascript i display a category which is in this case electronics, and the category electronics has subcategories: buy, sell. the user selects the buy subcategory so the advertise will be like this: buy a phone->electronics->buy. now i save the category_id and the subcategory_id in the advertise table, but there are many categories that have the subcategories buy and sell. the subcategory table row looks like:

id, name, category_id
1, buy, 1
2, sell, 1

how to save the data in the database? and how to retrieve the the advertise?

I made an image gallery for each advertise, I made a function that uploads an image to an advertise like a thumbnail, and another function that uploads the other images when you access the edit route, but i want the user to be able to upload more photos when he creates the advertise not after the advertise is created. the images are saved in a separated table:

images
id, name, advertise_id

how to save the images when the user creates the advertise?

this is the advertise model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Advertise extends Model
{
    protected $fillable = ['title', 'description', 'image', 'price'];

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function category()
    {
        return $this->belongsToMany('App\Category_Subcategory');
    }

    public function subcategory()
    {
        return $this->belongsTo('App\SubCategory');
    }

    public function location()
    {
        return $this->belongsTo('App\Location');
    }

    public function image()
    {
        return $this->hasMany('App\Image');
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire