hackroot's avatar

Creating Laravel 5.7 dynamic Sliders in Laracom Ecommerce

Hi i am trying to add dynamic sliders in Laracom Ecommerce package by @jsdecena on github. I have created all the necessary spaces but I am getting the following error, when I try accessing the project.test/admin/slides page;

Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "syntax error, unexpected 'public' (T_PUBLIC)"

C:\laragon\www\laracom\app\Http\Controllers\Admin\slides\SliderController.php
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        {
            $slider = slider::findorfail($ID);
            return view('admin.slides.edit', compact('slider'));
    }
 
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $slider = slider::find($id);
       @$this->validate($request, array(
         'title'=>'required|max:225',
         'photo'=>'required|image'
      ));
 
       $slider = slider::where('id',$id)->first();
 
       $slider->title = $request->input('title');
 
       if ($request->hasfile('photo')) {
        $photo = $request->file('photo');
        $filename = 'slide' . '-' . time() . '.' . $photo->getclientoriginalextension();
        $location = public_path('images/');
        $request->file('photo')->move($location, $filename);
 
        $oldfilename = $slider->photo;
        $sliders->photo= $filename;
        IF(!EMPTY($slider->photo)){
Arguments
"syntax error, unexpected 'public' (T_PUBLIC)"

Below is my SlidersController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Shop\Slider\Slider;
use App\Shop\Categories\Requests\UpdateCategoryRequest;

class SliderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    // index function
    public function index()
    {
        $sliders = Slider::orderby('id', 'desc')->paginate(10);
        return view('sliders.index', compact('sliders'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return  view ('admin.slides.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, array(
            'title'=>'required|max:225',
            'photo'=>'required|image',
          ));
          $slider = new Slider;
          $slider->title = $request->input('title');
          if ($request->hasFile('photo')) {
            $photo = $request->file('photo');
            $filename = 'slide' . '-' . time() . '.' . $photo->getClientOriginalExtension();
            $location = public_path('images/');
            $request->file('photo')->move($location, $filename);

            $slider->photo = $filename;
          }
          $slider->save();
          return redirect()->route('admin.slides.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        {
            $slider = slider::findorfail($ID);
            return view('admin.slides.edit', compact('slider'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateSlideRequest $request, $id)
    {
        $slider = slider::find($id);
       @$this->validate($request, array(
         'title'=>'required|max:225',
         'photo'=>'required|image'
      ));

       $slider = slider::where('id',$id)->first();

       $slider->title = $request->input('title');

       if ($request->hasfile('photo')) {
        $photo = $request->file('photo');
        $filename = 'slide' . '-' . time() . '.' . $photo->getclientoriginalextension();
        $location = public_path('images/');
        $request->file('photo')->move($location, $filename);

        $oldfilename = $slider->photo;
        $sliders->photo= $filename;
        IF(!EMPTY($slider->photo)){
          storage::delete($oldfilename);
        }
      }

      $slider->save();

      return redirect()->route('slides.index',
          $slider->id)->with('success',
          'slider, '. $slider->style.' updated');
      }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $slider = slider::findorfail($id);
        storage::delete($slider->photo);
        $slider->delete();
      
        return redirect()->route('admin.slides.index')
                ->with('success',
                 'Slide has been successfully deleted');
}

my slider model (slider.php)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Slider extends Model
{
    //
    protected $fillable = [
        'title', 'photo',
     ];
}

views/admin/slides/index.blade.php

<a href="{{ route('admin.slide.create') }}">Add New Slide</a>

<div style="margin-top:30px;">

@foreach($sliders as $slider)

    <
    <img src="{{url('images')}}/{{$slider->photo}}" alt="{{$slider->title}}" width="250" height="150">

<a href="{{ route('admin.slide.edit', $slider->id) }}" class="btn btn-block btn-info">Edit Slide</a>

{!! form::open(['method' => 'delete', 'route' => ['admin.slides.destroy', $slider->id] ]) !!}
  
  <button class="btn btn-block btn-danger" type="submit">Delete</button> 
{!! form::close() !!}

<br>

@endforeach
</div>

views/admin/create.blade.php

{{ Form::open(array('route' => 'slides.store', 'files' => true)) }}

  {{ Form::label('title', 'Title') }}

  {{ Form::text('title', null, array('class' => 'form-control')) }}


  {{ Form::label('photo', 'Photo') }}

  {{ Form::file('photo', array('class' => 'form-control')) }}


  {{ Form::submit('Add', array('class' => 'pull-right btn btn-primary')) }}

{{ Form::close() }}

views/admin/edit.blade.php;

{{ FORM::MODEL($SLIDER, ARRAY('ROUTE' => ARRAY('SLIDES.UPDATE', $SLIDER->ID), 'METHOD' => 'PUT', 'FILES' => TRUE)) }}

{{FORM::LABEL('TITLE', 'TITLE')}}
{{FORM::TEXT('TITLE', NULL, ARRAY('CLASS' => 'FORM-CONTROL'))}}
<BR>

{{FORM::LABEL('PHOTO', 'PHOTO')}}
{{FORM::FILE('PHOTO', ARRAY('CLASS' => 'FORM-CONTROL'))}}

<BR>
<IMG SRC="{{URL('IMAGES')}}/{{$SLIDER->PHOTO}}" ALT="IMAGE">

<BR><BR><BR>

{{ FORM::SUBMIT('UPDATE SLIDE', ARRAY('CLASS' => 'BTN BTN-SUCCESS')) }}

{{FORM::CLOSE()}}

Any assistance will be greatly appreciated. Thanks

0 likes
31 replies
krismanning's avatar

Is that your whole SlidersController file, cos it looks like you are missing the class closing brace at the end }

krismanning's avatar

And a typo here (remove that brace)

public function edit($id)
   {
       {
           $slider = slider::findorfail($ID);
           return view('admin.slides.edit', compact('slider'));
   }
krismanning's avatar

And another typo (remove the @ from the validate)

    public function update(UpdateSlideRequest $request, $id)
    {
        $slider = slider::find($id);
       @$this->validate($request, array(
         'title'=>'required|max:225',
         'photo'=>'required|image'
      ));
hackroot's avatar

Thanks I have put the ethe brace there however my public function update(Request $request, $id){} has been highlighted with error syntax error, unexpected 'public' (T_PUBLIC) can you kindly assist? Thanks

hackroot's avatar

Thanks

 public function update(Request $request, $id)
    {
        $slider = slider::find($id);
        $this->validate($request, array(
         'title'=>'required|max:225',
         'photo'=>'required|image'
      ));
automica's avatar

@hackroot have you done much customization to these methods or do you think there is a bug in the core code?

hackroot's avatar

I can't seem to figure out how to make it work but the slider controller and all extra code in regards to slides or slider, I just added recently

automica's avatar

@hackroot so there is slider functionality already in laracom, but you have added a new controller etc as you couldn't get existing one work?

1 like
hackroot's avatar

I saw the views/layouts/front/home-slider.blade.php However i did not see any controllers or models that makes it visible, also from the code in home-sider.blade.php I saw only a single banner there. However I want to be able to upload sliders from the admin panel so they can be visible at the front end. I should also be able to change the text for each slider. Kindly assist

1 like
hackroot's avatar

@automica, Hope you're doing well?, So i noticed that I'am suppose to add some code in the sliderController public function update(UpdateSlideRequest $request, $id) { $slide = slide::find($id); $this->validate($request, array( 'title'=>'required|max:225', 'photo'=>'required|image' ));

but this line public function update(UpdateSlideRequest $request, $id) is throwing the error

syntax error, unexpected 'public' (T_PUBLIC)

Please boss any assistance or direction will be match appreciated

hackroot's avatar

@automica Not yet I haven't been able to make it work yet, I will create the gist, right now

automica's avatar

@hackroot i think the issue is to do with your edit method

     
    public function edit($id)
   {
       {
           $slide = slide::findorfail($id);
           return view('admin.slides.edit', compact('slider'));
   }

Where you have 2 opening brackets.

As well, you should rename model file to Slide.php not SlideModel.php

Also when you call it you should use Slide not Slider or slide.

If you can try above and update your gists with new changes.

1 like
hackroot's avatar

@automica , I have updated slideController, however i still get the following error;

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Class 'App\Http\Controllers\Admin\Slides\Controller' not found
C:\laragon\www\laracom\app\Http\Controllers\Admin\Slides\SlideController.php
<?php
 
namespace App\Http\Controllers\Admin\Slides;
 
 
use App\Shop\Slides\Slide;
use Illuminate\Http\Request;
 
class SlideController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    // index function
    public function index()
    {
        $Slides = Slide::orderby('id', 'desc')->paginate(10);
        return view('slides.index', compact('slides'));
    }
 
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return  view ('admin.slides.create');
    }
 
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
Arguments
"Class 'App\Http\Controllers\Admin\Slides\Controller' not found"

My model is Slide.php now

automica's avatar

@hackroot you are missing

use App\Http\Controllers\Controller;

to tell Laravel the location of the controller you are extending

BTW this:

        $Slides = Slide::orderby('id', 'desc')->paginate(10);
        return view('slides.index', compact('slides'));

should be

        $slides = Slide::orderby('id', 'desc')->paginate(10);
        return view('slides.index', compact('slides'));
1 like
hackroot's avatar

Thank's that moved me forward, I got got a little stop with the migration of tables but i have done that successfully however I got stuck with yet another error;

ErrorException (E_ERROR)
Route [admin.slide.create] not defined. (View: C:\laragon\www\laracom\resources\views\admin\slides\index.blade.php)
Previous exceptions
Route [admin.slide.create] not defined. (0)
C:\laragon\www\laracom\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php
               ! ($expires && Carbon::now()->getTimestamp() > $expires);
    }
 
    /**
     * Get the URL to a named route.
     *
     * @param  string  $name
     * @param  mixed   $parameters
     * @param  bool  $absolute
     * @return string
     *
     * @throws \InvalidArgumentException
     */
    public function route($name, $parameters = [], $absolute = true)
    {
        if (! is_null($route = $this->routes->getByName($name))) {
            return $this->toRoute($route, $parameters, $absolute);
        }
 
        throw new InvalidArgumentException("Route [{$name}] not defined.");
    }
 
    /**
     * Get the URL for a given route instance.
     *
     * @param  \Illuminate\Routing\Route  $route
     * @param  mixed  $parameters
     * @param  bool   $absolute
     * @return string
     *
     * @throws \Illuminate\Routing\Exceptions\UrlGenerationException
     */
    protected function toRoute($route, $parameters, $absolute)
    {
        return $this->routeUrl()->to(
            $route, $this->formatParameters($parameters), $absolute
        );
    }
 
    /**
Arguments
"Route [admin.slide.create] not defined. (View: C:\laragon\www\laracom\resources\views\admin\slides\index.blade.php)"

I have modified the gist. I am trying to access the admin pages for the slide properties.

automica's avatar

@hackroot your resource

Route::resource('Slides', 'SlideController');

should be lower case

Route::resource('slides', 'SlideController');

Run the following command to check you are calling it by it’s correct name

php artisan route:list

I suspect you need to call it as

admin.slides.create
hackroot's avatar

Hello Good Morning, I had a blackout in my area, Please I can now access the admin.slides.index, however the following error is seen when I try accessing the create page

ErrorException (E_ERROR)
Class 'Form' not found (View: C:\laragon\www\laracom\resources\views\admin\slides\create.blade.php)
Previous exceptions
Class 'Form' not found (0)
C:\laragon\www\laracom\vendor\laravel\framework\src\Illuminate\View\Engines\PhpEngine.php
     * @param  array   $__data
     * @return string
     */
    protected function evaluatePath($__path, $__data)
    {
        $obLevel = ob_get_level();
 
        ob_start();
 
        extract($__data, EXTR_SKIP);
 
        // We'll evaluate the contents of the view inside a try/catch block so we can
        // flush out any stray output that might get out before an error occurs or
        // an exception is thrown. This prevents any partial views from leaking.
        try {
            include $__path;
        } catch (Exception $e) {
            $this->handleViewException($e, $obLevel);
        } catch (Throwable $e) {
            $this->handleViewException(new FatalThrowableError($e), $obLevel);
        }
 
        return ltrim(ob_get_clean());
    }
 
    /**
     * Handle a view exception.
     *
     * @param  \Exception  $e
     * @param  int  $obLevel
     * @return void
     *
     * @throws \Exception
     */
    protected function handleViewException(Exception $e, $obLevel)
    {
        while (ob_get_level() > $obLevel) {
            ob_end_clean();
        }
 
Arguments
Symfony\Component\Debug\Exception\FatalThrowableError {#1477 ▶}
1

Also I get a blank page when I try to access the edit page, I noticed the show function in the controller, How would I make use of that please. Gist Updated, Thanks boss

automica's avatar

Looks like you are using a Form helper. Which one are you using?

Is that a new package you’ve added or is it existing laracom package?

1 like
automica's avatar

@hackroot i don't really think theres much advantage in using Laravel Collective Form helper in this case, so would suggest you don't unless you really want to.

To get it to work, you'll need to check its been added to providers and you have an alias to use the helpers.

https://laravelcollective.com/docs/5.6/html

hackroot's avatar

okay i won't use it i will build the form with the default adminlte theme used here

hackroot's avatar

@automica Hello boss hope you doing well, anyway I have worked on the forms by imitating the category forms and here is my results; https://gist.github.com/brandsafric/999f14ebc494bc9999f35a8fe0f5df47. I get error when i visit the create page:

ErrorException (E_ERROR)
Class 'Form' not found (View: C:\laragon\www\laracom\resources\views\admin\slides\create.blade.php)
Previous exceptions
Class 'Form' not found (0)
C:\laragon\www\laracom\vendor\laravel\framework\src\Illuminate\View\Engines\PhpEngine.php
     * @param  array   $__data
     * @return string
     */
    protected function evaluatePath($__path, $__data)
    {
        $obLevel = ob_get_level();
 
        ob_start();
 
        extract($__data, EXTR_SKIP);
 
        // We'll evaluate the contents of the view inside a try/catch block so we can
        // flush out any stray output that might get out before an error occurs or
        // an exception is thrown. This prevents any partial views from leaking.
        try {
            include $__path;
        } catch (Exception $e) {
            $this->handleViewException($e, $obLevel);
        } catch (Throwable $e) {
            $this->handleViewException(new FatalThrowableError($e), $obLevel);
        }
 
        return ltrim(ob_get_clean());
    }
 
    /**
     * Handle a view exception.
     *
     * @param  \Exception  $e
     * @param  int  $obLevel
     * @return void
     *
     * @throws \Exception
     */
    protected function handleViewException(Exception $e, $obLevel)
    {
        while (ob_get_level() > $obLevel) {
            ob_end_clean();
        }
 
Arguments
Symfony\Component\Debug\Exception\FatalThrowableError {#1462 ▶}
1

and I get a blank page when I try visiting the edit.blade.php page meanwhile it throws the following errors at source in the browser.

Unchecked runtime.lastError: The message port closed before a response was received.
edit:1 Unchecked runtime.lastError: The message port closed before a response was received.
edit:1 Unchecked runtime.lastError: The message port closed before a response was received.
edit:1 Unchecked runtime.lastError: The message port closed before a response was received.
edit:1 Unchecked runtime.lastError: The message port closed before a response was received.
edit:1 Unchecked runtime.lastError: The message port closed before a response was received.
edit:1 Unchecked runtime.lastError: The message port closed before a response was received.
edit:1 Unchecked runtime.lastError: The message port closed before a response was received.
edit:1 Unchecked runtime.lastError: The message port closed before a response was received.
edit:1 Unchecked runtime.lastError: The message port closed before a response was received.

Please boss kindly assist. If possible I want the slides such that when one image is uploaded the it will be so as to behave like a landing page. Thanks in advance

automica's avatar

@hackroot the missing form error looks like it’s related to the laravel collective form helper. There doesn’t appear to be any reference to this in your gist files though,

Have you removed that package from your composer, and also references to it from your AppServiceProvider? You should also run composer dump-autoload and clear your view caches after doing this.

hackroot's avatar

@automica I have update the gist with the followng files;

GlobalTemplateServiceProvider.php
AppServiceProvider.php
composer.json

I also did not call the laravelCollection/html in my composer at all, Thanks

automica's avatar
automica
Best Answer
Level 54

@hackroot I've been through your files and got it all working. You did have a reference to Form Helper in your edit view. There was a quite a few things to correct as you had half changed some of the forms you'd based this off, but there were references to categories in admin.slides.index and some missing stuff all around.

Heres my branch based of laracom master. You'll have all the admin functionality here now, and image 'slides' being dropped into your home page view.

https://github.com/jsdecena/laracom/compare/master...iammikek:feature/slides

hackroot's avatar

@automica Hello Thanks a lot, now I can see the images at the front end however The images donot slide but they spreadout or stack over one another. I am not sure if that is a css issue or or it's within the laravel, qalso I tried adding a bootstrap slide sample with it's css however it did not have any effect even after i run "npm run dev" Also I had wanted the title to be on top of the slide however i noticed it was used as an "alt" in the home-slider.blade.php. or if possible can a description sector be added? Thanks again

automica's avatar

@hackroot I’ve not added any functionally to make them slide, just outputting them on to homepage.

If you want to make them slide look at https://getbootstrap.com/docs/4.0/components/carousel/

As for adding description, you should be able to extend my work to enable that quite easily. Just be careful you are consistent with using ‘description’ in all places,

1 like
Next

Please or to participate in this conversation.