13 Replies - 391 Views - Last Post: 20 April 2020 - 08:43 AM Rate Topic: -----

#1 lfrost2015   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 08-March 15

Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 02:24 AM

I am outputting a foreach loop using the compact variable from my Products Controller in a blade file but it keeps coming up with an error in the view that doesn't make sense.

@extends('admin.master')

@section('content')


<main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main">
<h3>Products</h3>

<ul>






    <!-- INVERSE/DARK TABLE -->
       <table class="table table-dark">
           <thead>
               <tr>
                   <th>Image</th>
                   <th>Product Id</th>
                   <th>Product Name</th>
                   <th>Product Code</th>
                   <th>Product Price</th>
                   <th>Category Id</th>
                   <th>Update</th>
               </tr>
           </thead>

           <tbody>

              @foreach($products as $product)
               <tr>
                   <td style="width:50px; border: 1px solid #333;"><img class="card-img-top img-fluid" src="{{url('images',$product->image)}}" width="50px" alt="Card image cap"></td>
                   <td style="width:50px;">{{$product->id}}</td>
                   <td style="width:50px;">{{$product->product_name)}}</td>
                   <td style="width:50px;">{{$product->product_code}}</td>
                   <td style="width:50px;">{{$product->product_price}}</td>
                   <td style="width:50px;">{{$product->category_id}}</td>
                   <td><a href="{{route('ProductEditForm',$product->id)}}" class="btn btn-success btn-small">Edit</a></td>
                   {!! Form::open(['method' => 'DELETE', 'action' => ['[email protected]', $product->id]]) !!}
                      <td>{!! Form::submit('Delete Product', ['class' => 'btn btn-danger col-sm-6']) !!}</td>


                   {!! Form::close() !!}
               </tr>
               @endforeach
           </tbody>
       </table>


</main>

@endsection




Below is the Controller for this:

class ProductsController extends Controller
{
    public function index()
    {
      $products = DB::table('categories')->rightJoin('products', 'products.category_id', '=', 'categories.id')->get();
      // Fetching all products and categories
      $products = Product::all();
      return view('admin.product.index', compact('products'));
    }

    public function create()
    {
      $categories = Category::pluck('name', 'id');
      return view('admin.product.create', compact('categories'));
    }

    public function store(Request $request)
    {
      $formInput = $request->except('image');

      $this->validate($request, [
        'product_name' => 'required',
        'product_code' => 'required',
        'product_price' => 'required',
        'product_info' => 'required',
        'sale_price' => 'required',
        'image' => 'image|mimes:png,jpg,jpeg|max:10000'
      ]);

      $image = $request->image;
      if ($image) {
        $imageName = $image->getClientOriginalExtension();
        $image->move('images', $imageName);
        $formInput['image']=$imageName;
      }

      $categories = Category::all();
      Product::create($formInput);
      return redirect()->back();
    }

    public function show($id)
    {
      $product = Product::findOrFail($id);
      return view('product.show', compact('products'));
    }

    public function ProductEditForm($id)
    {
      $products = Product::findOrFail($id);
      $categories = Category::all();

      return view('admin.product.editProducts', compact('products', 'categories'));
    }

    public function editProducts(Request $request, $id)
    {
      $product_id = $request->id;
      $product_name = $request->product_name;
      $category_id = $request->category_id;
      $product_code = $request->product_code;
      $product_price = $request->product_price;
      $product_info = $request->product_info;
      $sales_price = $request->sale_price;

      DB::table('products')->where('id', $product_id)->update([
        'product_name' => $product_name,
        'category_id' => $category_id,
        'product_code' => $product_code,
        'product_price' => $product_price,
        'product_info' => $product_info,
        'sale_price' => $sales_price
      ]);

      return view('admin.product.index', compact('products', 'category'));
    }

    public function ImageEditForm($id)
    {
      $products = Product::findOrFail($id);
      return view('admin.product.ImageEditForm', compact('products'));
    }

    public function editProductImage(Request $request)
    {
      $product_id = $request->id;

      $image = $request->image;
      if($image)
      {
        $imageName = $image->getClientOriginalName();
        $image->move('images', $imageName);
        $formInput['image'] = $imageName;
      }

      DB::table('products')->where('id', $product_id)->update(['image' => $imageName]);

      return redirect()->back();
    }

    public function destroy($id)
    {
        Product::findOrFail($id)->delete();
        return redirect()->back();
    }

}




The error appears on the line with the image in although I'm not sure why. Not sure whether this is related to the view or controller?

This post has been edited by lfrost2015: 20 April 2020 - 05:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Syntax error, unexpected ')', expecting ';' or ',&

#2 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 562
  • Joined: 25-August 13

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 04:37 AM

While I attempt to reproduce your issue.. if your images are stored in the public/ directory under images (which they should be) it's wiser to use the asset() helper, e.g. asset('images/'.$product->image) instead.

Could you also post your Controller code?

This post has been edited by Splashsky: 20 April 2020 - 05:20 AM

Was This Post Helpful? 0
  • +
  • -

#3 lfrost2015   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 08-March 15

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 05:12 AM

View PostSplashsky, on 20 April 2020 - 04:37 AM, said:

While I attempt to reproduce your issue.. if your images are stored in the public/ directory under images (which they should be) it's wiser to use the asset() helper, e.g. asset('/images/'.$product->image) instead.

Could you also post your Controller code?


Just Updated the post above to include the controller code.
Was This Post Helpful? 0
  • +
  • -

#4 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 562
  • Joined: 25-August 13

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 05:16 AM

What happens if you replace the url() helper with the asset() helper I mentioned?

I also don't believe you need to run compact(), you should be able to simply pass the array in your controller methods...
return view('admin.product.index', [
    'products' => $products
]);


This post has been edited by Splashsky: 20 April 2020 - 05:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 562
  • Joined: 25-August 13

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 05:28 AM

I'd also point out that your delete form might be much more succinct if you write it like this...
<form method="POST" action="{{ action('[email protected]') }}">
    @method('DELETE') @csrf
    <input type="submit" class="btn btn-danger col-sm-6" value="DELETE">
</form>


That way it doesn't look so janky and out of place
Was This Post Helpful? 0
  • +
  • -

#6 lfrost2015   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 08-March 15

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 05:41 AM

View PostSplashsky, on 20 April 2020 - 05:16 AM, said:

What happens if you replace the url() helper with the asset() helper I mentioned?

I also don't believe you need to run compact(), you should be able to simply pass the array in your controller methods...
return view('admin.product.index', [
    'products' => $products
]);



Using the asset helper still returns the same error. I will try the improvements on this post and the one below that you suggested.
Was This Post Helpful? 0
  • +
  • -

#7 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 562
  • Joined: 25-August 13

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 06:01 AM

If you could, also post your Product model. I'm curious to see if there's any issue there

Also, what happens if you put {{ dd($product->image) }} in the loop?

This post has been edited by Splashsky: 20 April 2020 - 06:02 AM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6898
  • View blog
  • Posts: 28,550
  • Joined: 12-December 12

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 06:07 AM

Please be cautious of amending previous posts with anything other than trivial edits, it disturbs the flow of the thread and causes confusion (especially a reference to a following post).
Was This Post Helpful? 1
  • +
  • -

#9 lfrost2015   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 08-March 15

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 06:30 AM

View PostSplashsky, on 20 April 2020 - 06:01 AM, said:

If you could, also post your Product model. I'm curious to see if there's any issue there

Also, what happens if you put {{ dd($product->image) }} in the loop?


The Product model is as follows:

class Product extends Model
{
    protected $table = 'products';
    protected $primaryKey = 'id';
    protected $fillable = ['product_name', 'product_code', 'product_price', 'image', 'product_info', 'sale_price', 'category_id', 'stock'];

    public function categories()
    {
        return $this->belongsToMany('Category', 'categories');
    }

    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}



Putting {{ dd($product->image) }} in the loop returns the same error. It keeps saying the error is in the blade file but I can't see anything in the file that indicates there is a problem with it.
Was This Post Helpful? 0
  • +
  • -

#10 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 562
  • Joined: 25-August 13

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 06:32 AM

Did you place that bit of code at the top of your @section('content')?
Was This Post Helpful? 0
  • +
  • -

#11 lfrost2015   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 08-March 15

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 06:36 AM

View PostSplashsky, on 20 April 2020 - 06:32 AM, said:

Did you place that bit of code at the top of your @section('content')?


Yes and it still returned the same error. If it helps the error is generated on the image bit in the loop so not sure what is going on there.
Was This Post Helpful? 0
  • +
  • -

#12 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 562
  • Joined: 25-August 13

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 06:39 AM

Odd, if the issue was on the image then dd should have stopped the execution and showed you what was up. As weird as it sounds, try deleting that blade file and remake it. Use the asset() helper where the image is and see if anything changes.
Was This Post Helpful? 0
  • +
  • -

#13 lfrost2015   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 08-March 15

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 08:24 AM

View PostSplashsky, on 20 April 2020 - 06:39 AM, said:

Odd, if the issue was on the image then dd should have stopped the execution and showed you what was up. As weird as it sounds, try deleting that blade file and remake it. Use the asset() helper where the image is and see if anything changes.


Just redid the whole blade file and it works absolutely fine. Still not sure what caused the error to appear in the first place. Thanks for the advice and help.
Was This Post Helpful? 1
  • +
  • -

#14 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 562
  • Joined: 25-August 13

Re: Syntax error, unexpected ')', expecting ';' or ',&

Posted 20 April 2020 - 08:43 AM

Could have just been a weird cache issue or a syntactical error we were missing. Anywho, glad to help! Good luck. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1