0

I am trying to write php code in between single quote in php file. But I am getting error

Parse error: syntax error, unexpected 'if' (T_IF)

my code snippet

    foreach($posts as $r){
        $nestedData['action'] = '
        <a href="'.url('/admin/post/').'/'.$r->id.'" class="btn btn-sm btn-secondary" title="View Details"><i class="fa fa-eye" aria-hidden="true"></i></a>
        &nbsp;&nbsp;
        '.if(Auth::user()->havePermission('post-delete')){.'
        <form method="POST" action="'.url('/admin/post').'/'.$r->id.'" accept-charset="UTF-8" id="frm_'.$r->id.'" class="form form-delete-btn" title="Delete">
        <input name="_method" type="hidden" value="DELETE">
        <input name="_token" type="hidden" value="'.csrf_token().'">
        <button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete'.$r->id.'"><i class="fa fa-trash" aria-hidden="true"></i></button>
        </form>
        '.}.'
         <div>
...............
         </div>';
}
Aayush Dahal
  • 31
  • 1
  • 8

4 Answers4

2

You are trying to use an if clause, which will not be supported. If you have to use the if clause, you could separate out the statement into 2 statements or use a ternary operator:

Using If Clause

foreach($posts as $r){
        $nestedData['action'] = '
        <a href="'.url('/admin/post/').'/'.$r->id.'" class="btn btn-sm btn-secondary" title="View Details"><i class="fa fa-eye" aria-hidden="true"></i></a>
        &nbsp;&nbsp;';
    if(Auth::user()->havePermission('post-delete')){
        $nestedData['action'] .= '<form method="POST" action="'.url('/admin/post').'/'.$r->id.'" accept-charset="UTF-8" id="frm_'.$r->id.'" class="form form-delete-btn" title="Delete">
            <input name="_method" type="hidden" value="DELETE">
            <input name="_token" type="hidden" value="'.csrf_token().'">
            <button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete'.$r->id.'"><i class="fa fa-trash" aria-hidden="true"></i></button>
            </form>';
    }
        $nestedData['action'] .= ' <div>
    ...............
         </div>';
}

Using Ternary Operator

foreach($posts as $r){
        $nestedData['action'] = '
        <a href="'.url('/admin/post/').'/'.$r->id.'" class="btn btn-sm btn-secondary" title="View Details"><i class="fa fa-eye" aria-hidden="true"></i></a>
        &nbsp;&nbsp;'. ((Auth::user()->havePermission('post-delete')) ?'<form method="POST" action="'.url('/admin/post').'/'.$r->id.'" accept-charset="UTF-8" id="frm_'.$r->id.'" class="form form-delete-btn" title="Delete">
            <input name="_method" type="hidden" value="DELETE">
            <input name="_token" type="hidden" value="'.csrf_token().'">
            <button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete'.$r->id.'"><i class="fa fa-trash" aria-hidden="true"></i></button>
            </form>' : '') .' <div>
    ...............
         </div>';
}
Saroj Shrestha
  • 2,696
  • 4
  • 21
  • 45
1

You are writing html code in controller.

It's better you make one blade view file. for ex- action.blade.php under resources/views/post/action.blade.php

Then you can get data of that view in controller

$nestedData['action'] = \View::make('posts.action')->with('r',$r)->render();

Just copy and paste the rest. As follows in action.blade.php

<a href="{{url('/admin/post/')}}/{{$r->id}}" class="btn btn-sm btn-secondary" title="View Details"><i class="fa fa-eye" aria-hidden="true"></i></a>
&nbsp;&nbsp;

@if(\Auth::user()->havePermission("post-destroy"))
<form method="POST" action="{{url('/admin/post/')}}/{{$r->id}}" accept-charset="UTF-8" id="frm_{{$r->id}}" class="form form-delete-btn" title="Delete">
    <input name="_method" type="hidden" value="DELETE">
    @csrf
    <button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete{{$r->id}}"><i class="fa fa-trash" aria-hidden="true"></i></button>
</form>
@endif
<div> ... </div>

Here $data is data required in view like $posts, etc..

and now you have view in variable $nestedData['action']. So, you can pass it to your view no need to write whole html in controller.

Nirali
  • 1,776
  • 2
  • 12
  • 23
  • I am quite new to this, could you elaborate on this. I am using server side data table i.e, it is being called from ajax.and it is in loop. Is that still possible with this view thing? – Aayush Dahal May 04 '18 at 05:40
  • @AayushDahal i updated my code please check. Yes it will behave same as your code just the difference is in your code html code written in controller and you store your html code in $nestedData['action'] and in above approch html code written in blade file which is proper way and it also store same in $nestedData['action']. did you get it? or still has any confusion? – Nirali May 04 '18 at 05:55
0

It looks like you might have ' or " mismatched prior to your if. It's likely a Syntax.

Gann14
  • 1,128
  • 1
  • 8
  • 12
0

you should put (!Auth::user()->havePermission('post-delete')) ?: replace for .if(Auth::user()->havePermission('post-delete')){.

<?php
foreach($posts as $r) {
    $nestedData['action'] = '
        <a href="' . url('/admin/post/') . '/' . $r->id . '" class="btn btn-sm btn-secondary" title="View Details"><i class="fa fa-eye" aria-hidden="true"></i></a>
        &nbsp;&nbsp;
        ' . (!Auth::user()->havePermission('post-delete')) ?: '
        <form method="POST" action="' . url('/admin/post') . '/' . $r->id . '" accept-charset="UTF-8" id="frm_' . $r->id . '" class="form form-delete-btn" title="Delete">
        <input name="_method" type="hidden" value="DELETE">
        <input name="_token" type="hidden" value="' . csrf_token() . '">
        <button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete' . $r->id . '"><i class="fa fa-trash" aria-hidden="true"></i></button>
        </form>

         <div>';


       $nestedData['action'] .= ' <div>
...............
     </div>';
}?>

You will want to use the a ternary operator which acts as a shortened IF/Else statement:

Hưng hoàng
  • 360
  • 2
  • 5
  • 18