Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Pass Model Data Into Laravel Blade #1348

Closed
Gugulethu-Nyoni opened this issue Aug 30, 2017 · 8 comments
Closed

How to Pass Model Data Into Laravel Blade #1348

Gugulethu-Nyoni opened this issue Aug 30, 2017 · 8 comments

Comments

@Gugulethu-Nyoni
Copy link

Gugulethu-Nyoni commented Aug 30, 2017

Summary of problem or feature request

Hi there. Firstly, a million thanks for the great laravel datatables framework Yajra

I have been digging around for a solution to manipulate $posts model data in the row.details.blade.php view

I'm still fresh on Laravel particularly datatables and I figured that yajrabox is the best datatables approach for a database driven app.

The objective is pretty straight forward but I'm struggling to get it right in the yajrabox datatables context:

Here is what I need to do.

I have a column in model (table) storing data this way NO|YES|NO|YES

So on the row.details.blade.php I need to explode that data and for example display icons based on whether the exploded array item is a YES or NO.

Take note that I need to apply this on the details-template and not on the main table or row.

I have tried to find out how to explode {{variable}} with no success.

I have also tried parsing Posts model data to view using with, withModel and compact with no success.

Here is what I have currently:

The Eloquent Controller

public function getRowDetails()
    {
            
   return view('datatables.eloquent.row-details');
    }

    public function getRowDetailsData()
    {
        $user_id=Auth::id();
        $posts = Post::select(['id', 'title', 'country', 'region', 'datatoexplode']) ->where('user_id', '=', $user_id);

        return Datatables::of($posts)->make(true);
    }


The Details template in blade

<script id="details-template" type="text/x-handlebars-template">
    <table class="table">
        <tr>
            <td>Title:</td>
            <td>{{title}}</td>
        </tr>
        <tr>
            <td>Region:</td>
            <td>{{region}}</td>
        </tr>
        <tr>
            <td>Features:</td>
            <td>{{datatoexplode}}</td>
        </tr>
    </table>
</script>

My aim is to explode the {{datatoexplode}} variable so I can access the |YES|NO|YES|YES| items and iterate them and format before rendering the html output. If I can get that in PHP that would be great.

Any help will be much appreciated.

System details

  • Operating System : Mac Captein
  • PHP Version: 7.
  • Laravel Version 5.1.46 (LTS)
  • Laravel-Datatables Version : Laravel Datatables Demo App
@lk77
Copy link
Contributor

lk77 commented Aug 30, 2017

why not using addColumn method ?
like this :

->addColumn('datatoexplode', function($post){

        return view('app.post._datatoexplode', compact('post'));
})

or directly exploding without returning a view if you want :

 ->addColumn('datatoexplode', function($post){

        return explode($post->datatoexplode, '|');
})

be aware that you will need to disable searcheable and orderable on this column or do it yourself with filterColumn and orderColumn. And you will need rawColumns(['datatoexplode']) if you want to use html.

But i think you have a modelisation problem here, if you have to explode data from the base, it's better to have an enum.

@yajra yajra added the question label Aug 31, 2017
@Gugulethu-Nyoni
Copy link
Author

Thanks a lot for the quick responses much appreciated.

I see ->addColumn would be a great option if I were implementing this on the parent row. Remember I'm implementing the explode on a variable that will go to the details row (Unless if that can still work for the child row as well) as exemplified in the code below:

<script id="details-template" type="text/x-handlebars-template">
    <table class="table">
        <tr>
            <td>Title:</td>
            <td>{{title}}</td>
        </tr>
        <tr>
            <td>Region:</td>
            <td>{{region}}</td>
        </tr>
        <tr>
            <td>Features:</td>
            <td>{{datatoexplode}}</td>
        </tr>
    </table>
</script>

I am exploring rawColumns and enum options as well. Links to any relevant resources would be immensely appreciated.

Thanks again. I must mention that this is my first major attempt at Laravel and Datatables.

@yajra
Copy link
Owner

yajra commented Aug 31, 2017

I think this demo is what you need for ref?

@Gugulethu-Nyoni
Copy link
Author

Thanks again Yajra for a lighting fast response. However instead of using the master and details tables I'm using the Row Details demo. The one this url Link

So it's essentially working with data from the same table and row for example: fetching user details and showing name and surname on the parent row and then showing Email and Telephone and datatoexplode variables of the same user on the child row. And in my case I want to explode the datatoexplode variables and cast raw html for formatting. Thanks again.

@lk77
Copy link
Contributor

lk77 commented Aug 31, 2017

i see, you do not want to explode the data in a more readable format in one column, but you want to explode in multiple columns ?

@capricorn05
Copy link

capricorn05 commented Jan 29, 2018

hi,
is there any easy way to do something like attached. I
hebe app functionality 1
have look at examples but cannot see anything.
I would like to keep the sort colums at the top also

Copy link

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the stale label Dec 19, 2023
Copy link

This issue was closed because it has been inactive for 7 days since being marked as stale.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants