Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

upload image in laravel 8 - API

I want to upload image for my posts and have polymorphism relation one to one (because I have other tables and they need image too ) between posts and images

And when I want to send request and store the image in database , I get this error:

BadMethodCallException: Call to undefined method App\Models\Image::move()

I'm creating an API so :

My postman :

enter image description here

My relations :

Image model :

class Image extends Model
{
    use HasFactory;

    protected $fillable = [
        'image'
    ];

    public function imageable(){
        return $this->morphTo();
    }

}

Post model :


class Post extends Model
{
    use HasFactory;
    use \Conner\Tagging\Taggable;


    protected $fillable = [
        'user_id' ,
        'category_id' ,
        'title' ,
        'body' ,
        'study_time',
        'likes',
        'status',
        'tags',
    ];

    public function image(){
        return $this->morphOne(Image::class , 'imageable');
    }

}

And the PostController , store() method :

    public function store(Request $request )
    {
        $data = $request->all();

        $validator = Validator::make($data, [
            'user_id'=>'required',
            'category_id'=>'required',
            'title' => 'required|max:150|unique:posts',
            'body' => 'required',
            'study_time'=>'required',
            'tags'=>'nullable|string',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors(), 'error']);
        }

        //separate tags
        $tags = explode(",", $request->tags);


        $image = new Image;
        $getImage = $request->file('image');
        $imageName = time().'.'.$getImage->extension();
        $image->move(public_path('images'), $imageName);



        $post = Post::create($data);

        $post->image()->save($image);

        //save tags
        $post->tag($tags);


        return response()->json([
        "success" => true,
        "message" => "successfull",
        "data" => $post
        ]);
    }


Where is my mistake?

like image 494
calisa Avatar asked Mar 19 '26 11:03

calisa


1 Answers

After a month of this challenge, I was finally able to solve it :}

To make the code better and cleaner, I added another column to my image table : path

it's for saving path of image , and another column : image

and i added the path to my fillable in Image model and i edited the code to this :

public function store(Request $request )
    {
        $data = $request->all();

        $validator = Validator::make($data, [
            'user_id'=>'required',
            'category_id'=>'required',
            'title' => 'required|max:150|unique:posts',
            'body' => 'required',
            'study_time'=>'required',
            'tags'=>'nullable|string',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors(), 'error']);
        }

        //separate tags
        $tags = explode(",", $request->tags);

        $image = new Image;
        $getImage = $request->image
        $imageName = time().'.'.$getImage->extension();
        $imagePath = public_path(). '/images/posts';

        $image->path = $imagePath;
        $image->image = $imageName;

        $getImage->move($imagePath, $imageName);

        $post = Post::create($data);

        $post->image()->save($getImage);

        $post->tag($tags);


        return response()->json([
        "success" => true,
        "message" => "successfully",
        "data" => $post
        ]);
    }


like image 179
calisa Avatar answered Mar 22 '26 06:03

calisa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!