skip to Main Content

I need to create numeros files in Laravel each having it’s own incremental number:
page1.blade.php; page2.blade.php … ecc incrementing the number of the page automatically.
I can’t create the pages manually because i have to create more than 192 pages.
I know that’s not the best way, but i have to do it like so even if i don’t want to.

I want to create the pages using the function:
$page_number = $articles->id / 8
how can i do it?
$articles is the result of a query from mysql database.

2

Answers


  1. First of fall – are you sure you’re using the right approach? I am struggling to find a reason why would you use so many blade files when they’re supposed to be only templates and not the pages themselves.

    If you still think that this is the right approach, then you can implement it like this –

    // Query the articles from the database
    $articles = Article::all();
    
    foreach ($articles as $article) {
        $pageNumber = $article->id / 8;
        $filename = 'page' . $pageNumber . '.blade.php';
        File::put(resource_path('views/' . $filename), '');
    }
    

    Please make sure you import the File facade for it work (use IlluminateSupportFacadesFile;)

    Login or Signup to reply.
  2. Welcome to SO.

    You don’t ever want to create that many files for articles. You want to create a template file and just provide data over there.

    @extends('layouts.app')
    
    @section('content')
    Provided data for articles
    
    @forelse($articles as $article)
    {{ $article->title }}
    {{ $article->body }}
    @empty
    Couldn't find any articles. Try later!
    @endforelse
    @endsection
    

    This is what templates look like. Read more here.

    This is how you can get the data.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search