0

i have for each loop in my view i want to check if the target is equal to some value and insert another value i`

        @foreach ($modules as $module)


                @if ($module->name === "h_car_positions")  <?php $module->name = "history_car_positions" ?>

                @else  <?php $module_name = $module->name; ?>

                  @endif


            <tr>
                <td>{{ $module->id }}</td>
                <td><a href="{{ url(config('laraadmin.adminRoute') . '/modules/'.$module->id) }}">{{ $module->name }}</a></td>
                <td>{{ $module->name_db }}</td>

                <td>{{ Module::itemCount( $module_name ) }}</td>
                <td>
                    <a href="{{ url(config('laraadmin.adminRoute') . '/modules/'.$module->id)}}#fields" class="btn btn-primary btn-xs" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-edit"></i></a>
                    <a href="{{ url(config('laraadmin.adminRoute') . '/modules/'.$module->id)}}#access" class="btn btn-warning btn-xs" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-key"></i></a>
                    <a href="{{ url(config('laraadmin.adminRoute') . '/modules/'.$module->id)}}#sort" class="btn btn-success btn-xs" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-sort"></i></a>
                    <a module_name="{{ $module->name }}" module_id="{{ $module->id }}" class="btn btn-danger btn-xs delete_module" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-trash"></i></a>
                </td>
            </tr>
        @endforeach

i get undefined variable for that variable :$module_name

2 Answers2

1

You are mixing blade syntax with php, so you should do it this way:

@php
  if($module->name == "h_car_positions"){
      $module_name = "history_car_positions";
  } else {
      $module_name = $module->name;
  }
@endphp
nakov
  • 13,938
  • 12
  • 60
  • 110
  • i could get the new variable but i get this result on item count {{ Module::itemCount(Cars) }} – mohamed mohamed Apr 16 '19 at 16:19
  • That's probably because you have `$module->name` with value of `Cars`. – nakov Apr 16 '19 at 16:34
  • i meant that the {{ Module::itemCount ($module_name) }} printed in the front end and the if statment didnt make any change i get $module_name = h_car_positions i need it to be history_car_positions in the for each – mohamed mohamed Apr 16 '19 at 16:41
  • if ($module->name === "H_car_positions") {{$module_name = 'history_car_positions'}} endif now i'm doing it like that the if statment is true but it's printing history_car_positions and the variable not signed – mohamed mohamed Apr 16 '19 at 17:05
  • Can you edit your original question with the new view that you are using, because I cannot understand what you are saying. And I cannot help you like this. – nakov Apr 16 '19 at 20:58
  • @mohamedmohamed I saw your edit, and I see that you are not using the code that I am sharing with you. Any reason why? – nakov Apr 16 '19 at 22:08
  • it's giving me the exact same result – mohamed mohamed Apr 16 '19 at 22:25
  • You have a typo if your condition is true, you are setting `$module->name` to the new value, instead of the `$module_name` so fix that and you will not get the undefined error. – nakov Apr 16 '19 at 22:26
  • also tried this before ,it's really driving me crazy – mohamed mohamed Apr 16 '19 at 22:29
  • okay friend, I am just pointing out what is the problem now, if you are sure that you have tried I have nothing else to say. Good luck fixing your problem. – nakov Apr 16 '19 at 22:30
0
    @foreach ($modules as $module)

                @php


    $module_name = $module->name;
  switch ($module_name) {
    case "H_car_positions":
       $module_name = "history_car_positions";
        break;

    default:
        $module_name =  $module->name ;
        $module_count =Module::itemCount( $module_name );
}
@endphp 

                <tr>
                    <td>{{ $module->id }}</td>
                    <td><a href="{{ url(config('laraadmin.adminRoute') . '/modules/'.$module->id) }}">{{ $module->name }}</a></td>
                    <td>{{ $module->name_db }}</td>

                    <td>{{ $module_count }}</td>