简体   繁体   中英

PHP Error — Use of undefined constant x - assumed 'x'

how to get data from table and pass it in controller in json format and get those data in view using angularjs

I want to get data from json encoded variable from controller and show it to view page using angularjs

view page using angularjs

<div class="card"  ng-app = "myApp">
  <table class="table table-hover" ng-controller = "menuController">
     <thead>
        <tr>
           <th>Name</th>
           <th>Parent menu</th>
           <th>Order</th>
           <th>Status</th>
           <th class="text-center">Actions</th>
        </tr>
     </thead>
     <tbody>
        <tr ng-repeat = "x  in values">
           <td>{{x.name}}</td>
           <td>{{x.parent_name}}</td>
           <td>{{x.orders}}</td>
           <td>{{x.status}}</td>
           <td class="text-right">
              <button type="button" class="btn btn-icon-toggle"
                      data-toggle="tooltip" data-placement="top"
                      data-original-title="Edit row">
                <i class="fa fa-pencil"></i>
              </button>
              <button type="button" class="btn btn-icon-toggle"
                      data-toggle="tooltip" data-placement="top"
                      data-original-title="Copy row">
                 <i class="fa fa-copy"></i>
              </button>
              <button type="button" class="btn btn-icon-toggle"
                      data-toggle="tooltip" data-placement="top" 
                      data-original-title="Delete row">
                <i class="fa fa-trash-o"></i>
              </button>
           </td>
        </tr>
     </tbody>
  </table>
</div>
  var app = angular.module('myApp');
  app.controller('menuController',function ($scope,$http) {
     $scope.values = ["Milk", "Bread", "Cheese"];
  });

PHP controller code

public function getmenu(){

    $records=Menu::all();

    return json_encode($records);
    }

route code

Route::group(['prefix'=>'admin'],function(){
    Route::get('/form','HomeController@form');
});

ErrorException (E_ERROR)
Use of undefined constant x - assumed 'x' (this will throw an Error in a future version of PHP) (View:form.blade.php)

I think it might be because you are serving your view which contains the angular code with Laravel, and in the file form.blade.php you use the angular syntax, which is the same as the blade one.

To solve that, you can try to remove blade word from the view filename, so it would become form.php or (alternative way instead of modifying the filename) each time you have to print something with the JavaScript framework instead of blade, use: @{{ variableToPrint }} .

So for example, part of your loop would become:

<tr ng-repeat="x in values">
       <td>@{{x.name}}</td>
       <td>@{{x.parent_name}}</td>
       <td>@{x.orders}}</td>
       <td>@{{x.status}}</td>
       <!-- ... -->

You get the error as blade uses the same syntax to evaluate and print values, so if you write: {{ x.name }} blade finds a string literal x which gets interpreted as a constant. If you prefix the @ sign, blade will recognize that as an instruction not to be parsed, but that needs to be left as it is, it would just remove the @ , leaving you with the correct code for you JavaScript loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM