• We just launched and are currently in beta. Join us as we build and grow the community.

Laravel: Embedding PHP Code in Blades

Jawed921

Binary Exploit Developer
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
60
Likes
150
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Getting Started

In this tutorial I'm going to limit the number of items shown per row using PHP so I'm going to embed the PHP code with the foreach. Also, I'll be using the upload project from the previous tutorial, Laravel Multiple File Upload, wherein I'm going to show the uploaded images.

Embedding our PHP Code

Next, we're going to edit the contents of upload.blade.php located in resources/views folder with the ff:

  1. <!

    DOCTYPE html>
  2. <

    html>
  3. <

    head>
  4. <

    meta charset=

    "UTF-8"

    >
  5. <

    title>

    Laravel:

    Embedding PHP Code in Blades</

    title>
  6. <

    link

    rel=

    "stylesheet"

    href=

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

    />
  7. <

    link

    rel=

    "stylesheet"

    href=

    "/css/app.css"

    >
  8. </

    head>
  9. <

    body>
  10. <

    div class

    =

    "container"

    >
  11. <

    h1 class

    =

    "page-header text-center"

    >

    Embedding PHP Code in Blades</

    h1>
  12. <

    div class

    =

    "row"

    >
  13. <

    div class

    =

    "col-md-4"

    >
  14. <

    div class

    =

    "well"

    >
  15. <

    h2 class

    =

    "text-center"

    >

    Upload Form</

    h2>
  16. <

    form method=

    "POST"

    action=

    "{{ route('upload.file') }}"

    enctype=

    "multipart/form-data"

    >
  17. {

    {

    csrf_field(

    )

    }

    }
  18. <

    input type=

    "file"

    name=

    "file[]"

    multiple><

    br>
  19. <

    button type=

    "submit"

    class

    =

    "btn btn-primary"

    >

    Upload</

    button>
  20. </

    form>
  21. </

    div>
  22. <

    div style=

    "margin-top:20px;"

    >
  23. @

    if

    (

    count

    (

    $errors

    )

    >

    0

    )
  24. @

    foreach

    (

    $errors

    ->

    all

    (

    )

    as

    $error

    )
  25. <

    div class

    =

    "alert alert-danger text-center"

    >
  26. {

    {

    $error

    }

    }
  27. </

    div>
  28. @

    endforeach
  29. @

    endif

  30. @

    if

    (

    session(

    'success'

    )

    )
  31. <

    div class

    =

    "alert alert-success text-center"

    >
  32. {

    {

    session(

    'success'

    )

    }

    }
  33. </

    div>
  34. @

    endif
  35. </

    div>
  36. </

    div>
  37. <

    div class

    =

    "col-md-8"

    >
  38. <

    h2>

    Uploaded Images</

    h2>
  39. @

    php
  40. $inc

    =

    3

    ;
  41. @

    endphp
  42. @

    foreach

    (

    $files

    as

    $file

    )
  43. @

    php
  44. $inc

    =

    (

    $inc

    ==

    3

    )

    ? 1

    :

    $inc

    +

    1

    ;
  45. if

    (

    $inc

    ==

    1

    )

    echo

    "<div class='row'>"

    ;
  46. @

    endphp
  47. <

    div class

    =

    "col-md-4"

    >
  48. <

    img src=

    "{{ $file->location

    }}"

    class

    =

    "thumbnail"

    style=

    "height:200px; width:100%"

    >
  49. </

    div>
  50. @

    php
  51. if

    (

    $inc

    ==

    3

    )

    echo

    "</div>"

    ;
  52. @

    endphp
  53. @

    endforeach
  54. @

    php
  55. if

    (

    $inc

    ==

    1

    )

    echo

    "<div class='col-md-4'></div><div class='col-md-4'></div></div>"

    ;
  56. if

    (

    $inc

    ==

    2

    )

    echo

    "<div class='col-md-4'></div></div>"

    ;
  57. @

    endphp

  58. </

    div>
  59. </

    div>
  60. </

    div>
  61. </

    body>
  62. </

    html>

Running our Project

You can test our code by deleting all existing data in our upload database and upload image files only.

Run our project by typing upload.dev in your web browser.

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top