Home>

I want to display HTML elements only when the URL is specific with blade.
A specific URL is a URL in the form of "article ID/edit", such as "http://127.0.0.1:8000/articles/10/edit".

The article ID is passed from the controller to the view in an associative array.
I confirmed with dd that the value of the associative array is properly passed to the view (the result of dd is described below).
The URL is also formally displayed correctly as "http://127.0.0.1:8000/articles/10/edit".

However, conditional branching using the view helper function ("@if (url ()->current () === url ("/articles/{{$article->id}}/edit "))") Seems not to work well.

How do I write the (conditional branch) part of the helper function and it works?

View
<!-Post image->
    @if (url ()->current () === url ("/ articles/{{$article->id}}/edit"))
    
        
          <span><i aria-hidden = "true"></i></span>
          <!-It is impossible to display the path of the previously registered file by default in input file. ->
          <input type = "file" name = "image_url" value = "{{$article->image_url ?? old ('image_url')}}" multiple>
        
    
     Current thumbnail: 
     image_url)}}" alt = "thumbnail">
    @endif
controller
  public function edit (Article $article)
    {
        $tagNames = $article->tags->map (function ($tag) {
            return ['text' =>$tag->name];
        });
        $allTagNames = Tag :: all ()->map (function ($tag) {
            return ['text' =>$tag->name];
        });
        // return view ('articles.edit', ['article' =>$article]);
         dd ($article);
        return view ('articles.edit', [
            'article' =>$article,
            'tagNames' =>$tagNames,
            'allTagNames' =>$allTagNames,
        ]);
    }
Result of dd
App \ Article {# 1376 ▼
  #fillable: array: 2 [▶]
  #connection: "mysql"
  #table: "articles"
  #primaryKey: "id"
  #keyType: "int"
  + increasing: true
  #with: []
  #withCount: []
  #perPage: 15
  + exists: true
  + wasRecentlyCreated: false
  #attributes: array: 7 [▼
    "id" =>10
    "Title" =>"Hello"
    "body" =>"" ""
      Hello.
      It's morning.
      The chicken is ringing.
      "" "
    "user_id" =>1
]
... abbreviation ...
}
What I tried
  • I went through various ways to find a solution, but I couldn't get any useful information. I didn't understand the following site.
    http://cly7796.net/wp/php/try-the-path-and-the-url-of-the-helper-function-of-laravel/
  • As shown below, I tried to write the optional parameter part as {id?}, But the HTML in the if statement was not displayed.
    @if (url ()->current () === url ("/ articles /"{id?}/ edit ")))
    https://readouble.com/laravel/5.5/ja/routing.html
  • Answer # 1

    @ifIn the syntax, you have to use the normal php grammar.
    Not the blade grammar.

    ↓ For example, is it like this?

    @if (url ()->current () === url ("/ articles /". $Article->id. "/ Edit"))

    I'm sorry if I removed it.

Related articles