如何在laravel框架中将一个表的引用ID插入另一个表?

时间:2017-08-29 08:59:02

标签: php mysql laravel-5.4

控制器代码:

public function saveImages(Request $imagesform, $dataId)
{
    if (Input::hasFile('images')) {
        $name = $this->getImageId() . "." . $imagesform->file('images')
                ->getClientOriginalExtension();

        $image = $imagesform->file('images');
        $resize = Image::make($image)
            ->resize(700, 300)
            ->encode($imagesform->file('images')->getClientOriginalExtension());

        $hash = md5($resize->__toString());

        $path = "public/" . $name;
        Storage::put($path, $resize->__toString());

    }

    $insertImages = new photosModel;
    $insertImages->imagesid = $this->getimagesId();
    $insertImages->deviceCategoryId = $dataId;
    $insertImages->uploadedImages = $name;
    //$insertImages->productPricing()->associate($priceInfo->id);
    $insertImages->save();

    echo("success");

    return $this->formImages($dataId);

}

这是我的控制器,用于在productPriceDetails表的参考ID的帮助下存储图像。我想得到那个身份。

table1的模型:

class priceInfo extends Model
{
    protected $table = "productPriceDetails";

    protected $connection = "mysql";

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

这是我的productPriceDetails模型。我需要使用此表的主键作为productPhotos模型中的外键。

表2的

模型:

class photosModel extends Model
{
    protected $table = "ProductPhotos";

    protected $connection = "mysql";

    protected $primaryKey = 'imagesid';

    public $incrementing = false;

    public function productPricing()
    {
        return $this->belongsTo('App\priceInfo', 'id');
    }
}

在这里,我定义了productPricing()函数;

0 个答案:

没有答案