Laravel:用一种方法创建两个表

时间:2018-07-17 05:32:21

标签: database laravel-5 web

我有两个桌子(照片和服务) 服务具有photo_id作为外键,我想同时创建包含photo_id的照片和服务。

这是我的控制器

public function store(Request $request)
{
    $target_dir = public_path('uploading/');

    if($request->hasFile('img')){

        $imageFileType = strtolower($_FILES["img"]["type"]);
        $uploadOk = 1;

        $imageFileType = explode('/', $imageFileType)[1];

        $target_file_name = md5(date('YmdHis') . basename($_FILES["img"]["name"]));

        $target_file = $target_dir . $target_file_name . '.' . $imageFileType;


        $check = getimagesize($_FILES["img"]["tmp_name"]);



        if ($check != false) {
            $uploadOk = 1;
        } else {
            $uploadOk = 0;
        }


        if ($imageFileType != "svg" && $imageFileType != "png" && $imageFileType != "jpeg"
            && $imageFileType != "gif") {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }
        $i = 0;

        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
        } else {
            if (move_uploaded_file($_FILES["img"]["tmp_name"], $target_file)) {

                $photo = Photo::create(['photo' => $target_file_name. '.' . $imageFileType]);
                Service::create([
                    'title'=>$request->title,
                    'description'=>$request->description,
                    'photo_id'=>$photo->id]);

                echo "The file has been uploaded.";

            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
    } 
}

以下任何事情  $ photo =照片:: create();没有执行,我也不知道为什么。

我尝试dd($ photo),回显$ photo,但它什么也没返回。

当我将service :: create()放在if()之外时,它将返回$ photo undefiend

任何人都可以看到我的代码有任何问题,这将非常有帮助。

1 个答案:

答案 0 :(得分:0)

据我所知,要将图像处理分离到控制器中的单独功能,可以轻松地在控制器中定义一个私有方法,并从存储方法中调用它,例如$this->cropAndSaveImage($requestData),请注意您可以将请求数据作为参数传递以获取图像详细信息,

或者第二种方法,您可以保存图像上传,通过ajax调用直接裁剪到特定的控制器方法,然后将表单提交到另一个控制器方法。

只分享您的一篇博客文章可能会对您有所帮助。 https://www.techalyst.com/links/read/72/facebook-like-profile-photo-upload-in-laravel-5-using-jquery-croppic-plugin

    public function store(Request $request)
{
    $target_file_name = $this->cropAndSaveImage($request);
    $title = $request->title;
    $description = $request->description;
    Service::create(['title'=>$title,'description'=>$description,'image'=>$target_file_name . '.' . $imageFileType]);



}


private function cropAndSaveImage(Request $request)
{
     $target_dir = public_path('uploading/');
     $target_file_name = '';
    if($image = $request->hasFile('img')) {


        $imageFileType = strtolower($_FILES["img"]["type"]);
        $uploadOk = 1;


        $imageFileType = explode('/', $imageFileType)[1];


        $target_file_name = md5(date('YmdHis') . basename($_FILES["img"]["name"]));



        $target_file = $target_dir . $target_file_name . '.' . $imageFileType;



        $check = getimagesize($_FILES["img"]["tmp_name"]);



        if ($check != false) {
            $uploadOk = 1;
        } else {
            $uploadOk = 0;
        }


        if ($imageFileType != "svg" && $imageFileType != "png" && $imageFileType != "jpeg"
            && $imageFileType != "gif") {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }


        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
        } else {
          move_uploaded_file($_FILES["img"]["tmp_name"], $target_file);


        }

        return $target_file_name;


    }
}