LittleBigThing's avatar

Saving filename to database

Hey everyone,

Laravel 5.3 is giving me a headache on almost every step. To be honest it's starting to slowly pissing me off. Every new version brings some unnecessary changes that force you to learn something you already know from the beginning. The worst thing is, something that was working on L5.2 doesn't work on L5.3... seriously???!!! Sorry, I'm a little bit angry, because I've lost almost two days trying to solve such a simple thing like image upload and save it's name into database... Let's get to the point. I have a controller AdminSliderController:

public function store(CreateSlideRequest $request)
{
    $dt        = Carbon::now();
    $date      = $dt->format('Ymd');
    $time      = $dt->format('His');
    $slideImage = $date . "_" . $time . "_" . $request->file('slide')->getClientOriginalName();
    $slide = new Slide([
        'slide'         =>  $slideImage
    ]);
    $slide->save();
    $request->file('slide')->move(base_path() . '/public/img/slider/', $slideImage);
    return redirect('admin/slider')->with('info', 'Slide added succesfully!');
}

When I try to check the content of variable $slideImage, I get what I want, filename with it's extension:

$slideImage = $date . "_" . $time . "_" . $request->file('slide')->getClientOriginalName();
return $slideImage;

BUT when I try to save this content in database table field slide I get an error saying, that the input slide is empty! So I've checked if it's true:

public function store(CreateSlideRequest $request)
{
    $dt        = Carbon::now();
    $date      = $dt->format('Ymd');
    $time      = $dt->format('His');
    $slideImage = $date . "_" . $time . "_" . $request->file('slide')->getClientOriginalName();
    $slide = new Slide([
        'slide'         =>  $slideImage
    ]);
    return $slide;
    ...
}

and guess what... variable $slide is null. Why? Am I doing something wrong? Why does it work without any problems in Laravel 5.2, but it doesn't in Laravel 5.3???

0 likes
2 replies
WebKenth's avatar

Try this instead

$slide = new Slide::create([ 'slide' => $slideImage ]);
LittleBigThing's avatar

Nope...

Parse error: syntax error, unexpected 'create' (T_STRING), expecting variable (T_VARIABLE) or '$'

EDIT: I've found the solution to my problem. In my **{!! Form(open) !!} I forgot to add 'files' => true.

Please or to participate in this conversation.