syntax error, unexpected '<' in View

Published 2 months ago by bipin

hello guys i m using laravel 5.4 and i m trying php in view but its throwing error unexpected '<' here is my view

      <ul>
       
     <?php

      $a = '';
       $b = '';
        $c = '';

         foreach($result as $m){
                    if($a!==$m->namecategory){
                    <li value=""> {{$m->namecategory}}
              
              $a[]=namecategory;
              }
                       <ul> 
                   
                               
                         if($b!==namesubcategory){
                              <li value="">{{$m->namesubcategory}}</li>

                                 $b=namesubcategory;
                                 }
                                 <ul>

                              if($c!==namesubling){
                                  <li value="">{{$m->namesubling}}</li>
                                  $c=namesubling;
                                  }
                                 </ul>


                              </li>
                 }
                   ?>
                        </ul>

in this code i m try to intially initiate $a,$b,$c after comparing i m try to store value in a,b,c is there other way to achieve these thing in laravel

topvillas

Don't put complex logic in your view. Do all that in your controller (or preferably another class) and pass variables to the view and do simple checks.

bipin

but this time i need logic in my view i will we appreciateif you help me

willvincent

Exactly, don't put a full block of PHP into your template, that's very bad practice.

That said, the specific problem(s) causing the error are the markup within your opening and closing php tags. That syntax is just entirely flawed.

bipin

@willvincent is there other way to achieve this??

tayfunkayahan

use '@foreach', '@if' functions in your blade template. https://laravel.com/docs/5.4/blade

Cronix

I agree with what everyone said, but the issue specific to your error is where you are doing things like:

if($a!==$m->namecategory){
    <li value=""> {{$m->namecategory}}
              
    $a[]=namecategory;
}

since this is within a php block and not html, you need to echo out your html, or close the php tags before <li value=""> and then reopen the php tags after. It's trying to parse <li value=""> as php, not html, and php doesn't know what < is in that context. That gets messy though. Better to echo it out. Best to not do all of that logic in the view though.

Snapey
Snapey
2 months ago (519,655 XP)

On your other thread I suggested reorganising your models so that you have nested relationships. You would not then have all this messy testing in your view..

PacoW

I hope these hints will be helpful.

  • <li value="">. Try to do echo '<li value="">'; instead and do this for all HTML elements inside the PHP block. I suspect this is causing the error you got: PHP didn't expect the <.
  • As far as I can see opening and closing HTML tags do not match: <li><ul></li></ul> or something like that. When nesting, the last opening tag should be closed first like this: <ul><li></li></ul>. Using indentation correctly should prevent errors like these.

I think it is mentioned before, but don't try to use PHP blocks inside a view. Use Blade instead: you can do the logic within double curly braces and things like @foreach.

Also, views are for displaying things. Of course there has to be some logic like Hello {{ $user->name }}! or @foreach ($posts as $post) <li>{{ $post->title }}</li> @endforeach. If you find yourself doing too much logic inside a view, you might want to move your logic to a model or controller and pass the result to the view with with('result', $result) or compact($result1, $result2) (examples).

Good luck!

bipin

@Cronin after everyone reply i thought i should do this logic in controller so i had written this logic can anyone rectify its please its also throwing error here is my controller

    $a=[];
  $b=[];
  $c=[];
 foreach ($users as $u) {
      if ($a!==namecategory) {

          $a=$u->namecategory
        }
             if ($b!==namesubcategory) {
                 $b=$u->namesubcategory
                 }
                    if ($c!==namesubling) {
                        $c=$u->namesubling
                      }  
         
   }


    return view('myform',compact('a','b','c'));
bipin

@Snapey actually i had created a model previsouly but i m not getting desire result if you help me it would be great here is my model view product1

  class product1 extends Model
{
protected $table='subling';
  protected $fillable=
     ['idsubling','namesubling','idsubcategory_subcategory'];
    public function subCategories()
 {
   return $this-
>belongsTo(Subcategory1::class,'idcategory_mastercategory','idsubcategory');
 }  
 }
 class Subcategory1 extends Model
 {
 protected $table = 'subcategory';
 protected $fillable=
 ['idsubcategory','idsubcategory_subcategory','namesubcategory'];

model subcategory

 public function category()
 {
return $this-

belongsTo(Category1::class,'idsubcategory_subcategory','idsubcategory'); }

 public function products()
{
 return $this-
 >hasMany(Product1::class,'idsubcategory_subcategory','idsubling');
}
 }

model category

   class Category1 extends Model
{
protected $table = 'mastercategory';
protected $fillable=['idcategory','namecategory'];

public function subCategories()
  {
return $this-

hasMany(Subcategory1::class,'idcategory_mastercategory','idsubcategory'); }

} it giving me result like this

  mobile and accessory
         mobile
              iphone
   mobile and accessory
         mobile cover
              iphone cover

but i want like this

 mobile and accessory
         mobile
              iphone
         mobile cover
              iphone cover

  
Snapey
Snapey
2 months ago (519,655 XP)

put three backticks ``` before and after your code blocks

You can edit your previous post

What does your controller look like, what does the view look like?

bipin

@Snapey view

       <ul>
   
         @foreach($result as $m)
                <li value=""> {{$m->namecategory}}
              
                  <ul>
                   @foreach($m->idsubcategory as $m)
                       <li value="">{{$m->namesubcategory}}

                                <ul>
                       @foreach($m->idsubling as $m)
                              <li value="">{{$m->namesubling}}</li>
                                 @endforeach
                               </ul>


                              </li>
                  @endforeach
                        </ul>

                     </li>
          @endforeach
             </ul>

and controller

    $result = Category1::all();

    return view('myform',compact('result')); 

if i use nested foreach its throwing error Invalid argument supplied for foreach() (View: and if i use single foreach then its give me result like this

   mobile and accessory
         mobile
             iphone
    mobile and accessory
         mobile cover
                iphone cover

but i want like this

 mobile and accessory
     mobile
          iphone
     mobile cover
          iphone cover 
Snapey
Snapey
2 months ago (519,655 XP)

I'm not sure of the details because your models are not clear and your naming is a bit all over the place.

You presumably have three models which correspond to product, category and subcategory?

Product belongs to subcategory

subcategory belongs to category.

In the controller, get your categories (the top level) and eager load all the subcategories and the products

$categories = Category::with('subcategories.products')->get();

You can then output them in a nested manner

    <ul>
    foreach($categories as $category)
    
        <li>{{ $category->name }}
            
            <ul>
            @foreach($category->subcategory as $subcat)
                <li>{{ $subcat->name }}
                    <ul>
                    @foreach($subcat->products as $product)
                        <li>{{ $product->name }}</li>
                    @endforeach 
                    </ul>
                </li>
            @endforeach 
            </ul>
        </li>
    @endforeach 
    </ul>
Jaytee

No no no no no no no no no no no no noooooooooooo.

Who uses a beautiful framework but doesn't use the features that makes it shine? :/

Sign In or create a forum account to participate in this discussion.