mercredi 31 mai 2017

Issues with saving an image with an article

I'm pretty sure this is a noob question, even though I'm used to PHP, but not to Laravel

My goal can't be any more simple, I'd like to be able to write an article and add an image to it, but even though I made the upload system work (that wasn't a piece of cake), I'm having issues with saving the filename itself.

Here's how I proceeded:

use App\Photo;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;

public function store(Request $request)
{

    $file = $request->file('image');
    $originalname = $file->getClientOriginalName();
    $path = 'uploads/' . $originalname;
    Image::make($file)->save($path);

    $product = new Photo(array(
        'name' => $request->get('name'),
        'image' => $originalname
    ));

    $product->save();

    return \Redirect::route('photo.index', array($product->id))->with('message', 'Product added!');
}

And here is my migration file, if this can help:

public function up()
{
    Schema::create('photos', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('image');
        $table->timestamps();
    });
}

So I wanted to save the filename as a string inside the database so I could call it later, like with $product->image, however I'm getting the following error:

SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value (SQL: insert into `photos` (`name`, `updated_at`, `created_at`) values (sisdjfposd, 2017-05-31 22:42:18, 2017-05-31 22:42:18))

So I know what it means, and I don't like it because it was supposed to have a value: if I add die($originalname);before the line $product = new Photo array(, i get my filename so logically the variable isn't empty.

So why would I have this error? Am i missing something?

Thank you in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire