METHOD DATA GET WITH DYNAMIC PROPERTIES

Jan 26, 2024 Copy Link

If you are working on a project where you need to export your data to an Excel sheet, you may create a view for each Model class...I will never recommend this approach because while the model number grows the views number will increase also. So, I always use the PHP Dynamically accessing elements feature in these situations however, the problem is not in using this feature but in showing the data in a blade file

 

So, let's develop a class for doing that task:

 

class PrepareModelsForExport
{
    /**
     * Export all registered users.
     */
    public function User()
    {
        $records = User::with(['phone', 'phone.countryCode'])->get();
        $headers = ['#', 'Name', 'Email', 'Phone', 'Country Image'];
        $rows    = ['id', 'name', 'email', 'phone->number', 'phone->countryCode->image'];

        return view('user.export', compact('records', 'headers', 'rows'));
    }

    /**
     * Export all registered phones.
     */
    public function Phone()
    {
        $records = Phone::with(['user', 'countryCode'])->get();
        $headers = ['#', 'User Name', 'Phone', 'Country Image'];
        $rows    = ['id', 'user->name', 'number', 'countryCode->image'];

        return view('phone.export', compact('records', 'headers', 'rows'));
    }
}

 

According to the previous code, we have some methods in which every method represents its model and each method gets the needed records with specific headers that will be presented in the Excel sheet and the rows or the properties that we need to access from the model then passing all these data to the class that contains the `view` method, then we can access these data in the blade file:

 

@foreach ($records as $record)
<tr>
    @foreach($rows as $row)
    <td style="text-align: center"> {{ $record->{$row} }}</td>
    @endforeach
</tr>
@endforeach

 

While the view is coming to be rendered an exception will be thrown that is because the properties like `phone->countryCode->image` or any depth relationship, so after some failed tries I asked Chatgpt which reminded me of the existence of the data_get helper method that allows us to access a value using no notation then, I made a few updates again on the code:

 

@foreach ($records as $record)
<tr>
    @foreach($rows as $row)
    @if (strpos($row, '->') !== false)
    <td style="text-align: center">
        {{ data_get($record, str_replace('->', '.', $row)) }}
    </td>
    @else
    <td style="text-align: center"> {{ $record->{$row} }}</td>
    @endif
    @endforeach
</tr>
@endforeach

 

In the end, I got awesome Excel sheets for all existing and coming models with fewer lines of code 🚀

Share via

Filed in:

Tips Helpers
Mahmoud Ramadan

Mahmoud Ramadan

Mahmoud is the creator of Digging Code and a contributor to Laravel since 2020.

Most recent

  • How to generate Arabic PDF using TCPDF

    How to generate Arabic PDF using...

    FREE

  • What is SQL Injection

    What is SQL Injection

    FREE

  • Leveraging virtual generated columns in MySQL using Laravel

    Leveraging virtual generated col...

    FREE