Call to undefined method create() error

user9334443

I want to save my images with using save_image() function in media model..

Here is my media model;

class Media extends Model
{
public function save_image($file)
    {
        $realname = str_slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME));
        $extension = $file->getClientOriginalExtension();
        $new_name = str_slug($realname) . "-" . time() . "." . $extension;
        $file->move(public_path('uploads'), $new_name);

        $image = DB::create([
            'image' => $new_name,
            'image_path' => "uploads/" . $new_name,
            'image_alt_name' => $realname
        ]);
        return $image;
    }
}

in my controller;

public function storeMedia(Request $request)

{
    $this->validate($request,[
        'image'=> 'required|mimes:jps,png,gif,jpeg'
    ]);

    $image=$request->file('image');
    $media = new Media();
    $media->save_image($image);

    return response()->json([
        'url'=> env('APP_URL')."/".$image->image_path,
        'alt' => $image->image_alt_name,
        'id' => $image->id,
    ]);
}

Are we have to say DB::table('media') at the first ? I mean, we are already on media model, is that necesary ?

Adnan Mumtaz

You are not specifying the table name in your code.

Here is the Solution

$image = DB::table('media')->create([
        'image' => $new_name,
        'image_path' => "uploads/" . $new_name,
        'image_alt_name' => $realname
    ]);

OR

$image = Media::create([
        'image' => $new_name,
        'image_path' => "uploads/" . $new_name,
        'image_alt_name' => $realname
    ]);

You can also use $this->create

Hope this helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Call to undefined method error

Symfony create new object many to one error call undefined method

Call to undefined method error in Laravel

PHP Error: Fatal error: Call to undefined method

CakePHP error: Call to undefined method FlashComponent::error()

Laravel Seeder, call to undefined method ::create

Call to undefined method can't create createDeleteForm

Fatal Error - Call to undefined method "Customersss::throwError()"

Fatal error: Call to undefined method PDOStatement::lastInsertId()

Fatal error: Call to undefined method db::_results()

CakePHP call to undefined method stdClass::read() error

Fatal error: Call to undefined method MongoCollection::insertMany()?

Error: Call to undefined method DateFormatterTest::getMock()

Fatal error: Call to undefined method connectDB::prepare()

PHP Classes - Fatal error: Call to undefined method

Mediawiki Error: Call to undefined method User::saveToCache()

Fatal Error :: Call to undefined method in codeigniter

Fatal error Call to undefined method VmVendorPDF::convertHTMLColorToDec()

Fatal error: Call to undefined method DB::getInstance()

xampp - Fatal error: Call to undefined method

PHP: Fatal error: Call to undefined method mysqli

Fatal error: Call to undefined method DOMDocument::getElementsById()

Call to undefined method stdClass::isEmpty() error

Error: Call to undefined method get() in PHPUnit Symfony

php error Call to an undefined static method with PHPStan

JQueryValidation plugin error: "Uncaught TypeError: Cannot call method 'call' of undefined"

PHP - Fatal Error: Call to undefined method - but method exist

Fatal error: Uncaught Error: Call to undefined method why?

Call to undefined method Directory::create() when seeding in Laravel