I
I
igordata12020-08-07 10:47:51
Laravel
igordata1, 2020-08-07 10:47:51

Why is the image not saved in another table?

Why is the image not saved in another table? I have a many-to-many relationship. The product is saved, but the pictures are not saved in the database. Images are not saved in a folder on the site either.

public function create(Request $request)
    {
        $categories = Category::whereNull('category_id')->with('childrenCategories')->get();
 
        return view('product.create', compact('categories' ));
    }
 
 
    public function store(Request $request)
    {
//        dd('stop');
 
        $this->validate($request, [
            'title' => 'required',
            'slug'   =>  'required|unique:products',
            'text'  =>  'required',
            'path'  =>  'nullable|image',
 
        ]);
 
        $product = new Product();
        $product->title = $request->input('title');
        $product->slug = $request->input('slug');
        $product->text = $request->input('text');
        $product->keywords = $request->input('keywords');
        $product->description = $request->input('description');
        $product->published = $request->input('published');
        $product->category_id = $request->input('category_id');
//        $product->product_id = $request->input('product_id');
        $product->price = $request->input('price');
        $product->authorized_price = $request->input('authorized_price');
//        $product->path = $request->input('path');
        $product->short_description = $request->input('short_description');
        $product->save();
//                dd('stop');
 
 
        $path =public_path().'uploads/product_images';
        $file = $request->file('file');
//        dd($path);
 
        foreach ($file as $f) {
            $filename = str_random(20) .'.' . $f->getClientOriginalExtension() ?: 'png';
            $img = ImageInt::make($f);
            $img->resize(500,500)->save($path . $filename);
            Image::create(['title' => $request->title, 'path' => $filename]);
        }
 
 
 
 
        return redirect('/product/create')->with('info', 'Данные сохранены');
    }
 
<?php
 
namespace App;
 
use Illuminate\Database\Eloquent\Model;
 
class Product extends Model
{
    public function category()
    {
        return $this->belongsTo(Category::class);
    }
 
 
    public function images()
    {
        return $this->hasMany(Image::class);
    }
 
}
 
 
 
 
 
 
ErrorException
Invalid argument supplied for foreach()
http://bossphp.x:8080/products/product/store
 
Сейчас такая ошибка появилась
 
    public function store(Request $request)
    {
//        dd('stop');
 
        $this->validate($request, [
            'title' => 'required',
            'slug'   =>  'required|unique:products',
            'text'  =>  'required',
            'path'  =>  'nullable|image',
 
        ]);
 
        $product = new Product();
        $product->title = $request->input('title');
        $product->slug = $request->input('slug');
        $product->text = $request->input('text');
        $product->keywords = $request->input('keywords');
        $product->description = $request->input('description');
        $product->published = $request->input('published');
        $product->category_id = $request->input('category_id');
//        $product->product_id = $request->input('product_id');
        $product->price = $request->input('price');
        $product->authorized_price = $request->input('authorized_price');
        $product->short_description = $request->input('short_description');
        $product->save();
//                dd('stop');
 
        $image = new Image();
 
        $path =public_path().'uploads/product_images';
        $file = $request->file('file');
//        dd($path);
 
        foreach ($file as $f) {
            $filename = str_random(20) .'.' . $f->getClientOriginalExtension() ?: 'png';
            $img = ImageInt::make($f);
            $img->resize(500,500)->save($path . $filename);
            Image::create(['title' => $request->title, 'path' => $filename]);
        }
        $image->path = $request->input('path');
        $image->save();
 
 
 
 
        return redirect('/product/create')->with('info', 'Данные сохранены');
    }
 
 
Добавил новый объект в метод . Ошибка пропала, но картинка не сохраняется. Как найти причину не сохранения картинки?


the code stopped here
$path =public_path().'uploads/product_images';
$image = new Image();
//                dd('stop');

        $path =public_path().'uploads/product_images';
        dd('stop');

        $file = $request->file('file');
//        dd($path);

        foreach ($file as $f) {
            //        dd($path);

            $filename = str_random(20) .'.' . $f->getClientOriginalExtension() ?: 'png';
            //        dd($path);

            $img = ImageInt::make($f);
            //        dd($path);

            $img->resize(500,500)->save($path . $filename);
            //        dd($path);

            Image::create(['title' => $request->title, 'path' => $filename]);
            //        dd($path);

        }
        //        dd($path);

        $image->path = $request->input('path');
        //        dd($path);

        $image->save();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2020-08-07
@igordata1

[email protected]:/var/www$ art tinker
Psy Shell v0.10.4 (PHP 7.4.8 — cli) by Justin Hileman
>>> public_path().'uploads/product_images';
=> "/var/www/publicuploads/product_images"
>>> public_path('uploads/product_images');
=> "/var/www/public/uploads/product_images"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question