Home > other >  How to create numuerous pages in laravel at once from mysql ids
How to create numuerous pages in laravel at once from mysql ids

Time:12-19

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 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.

CodePudding user response:

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 Illuminate\Support\Facades\File;)

CodePudding user response:

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.

  • Related