How to get specific columns in Laravel Eloquent with pagination?

netdjw

I use this table schema:

Schema::create('forms', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name', 255)->default('');
    $table->text('html')->nullable();
    $table->text('json')->nullable();

    $table->timestamps();
    $table->softDeletes();
});

This is the model:

class Form extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'name',
        'html',
        'json'
    ];

    protected $hidden = ['created_at', 'updated_at', 'deleted_at'];
}

And in the controller I want to show a list of all items of model but only the id and name fileds. Now I use this, but it show all not hidden fields:

public function index() {
    return Form::->paginate(100);
}

This function is only for the list of forms names. But here is the second one for show a form datas for modify:

public function show(string $id) {
    $item = Form::findOrFail($id);

    return response()->json($item);
}

Of course this last one function needs to be show all fields (id, name, html and json too).

Is there any best practice to show only fields what I needed in the index() function using with paginate()?

Naveed Ali

If i am not wrong then hopefully you can do it something like this for getting specific columns along with pagination:

return Form::paginate(100,['id','name',.....]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get a nested relationship database specific columns with Eloquent ::with method?

Laravel get sum of related table's columns with eloquent

Get NULL/empty columns in Laravel eloquent query

How to select specific columns in laravel eloquent

Get Specific Columns Using “With()” Function in Laravel Eloquent

Laravel 5 Eloquent scope join and select specific columns

laravel:how to get columns name of eloquent model?

How to get all images that belong to a specific album in Laravel with Eloquent?

How can I get some specific columns name from laravel table. (not all columns name)

How to get datas by month? Laravel Eloquent

how to get median for specific columns

Laravel Eloquent Pagination, how to count before pagination with filters?

How to get specific columns in Laravel [Maatwebsite/Laravel-Excel]

Laravel - How to get all columns if table is null or specific value

How to call DB functions in Laravel Eloquent and use pagination?

Laravel select specific columns with Eloquent while eager loading

Laravel - Eloquent relation - many-to-many - get intermediate table columns

how to get specific rows page number in pagination

Laravel Pagination with eloquent Queries

Laravel Eloquent get specific relationship

Laravel Auditing - How To Get Specific Column Using Eloquent Model Method

Get specific columns using “only()” function in Laravel Eloquent

Get Specific Columns in HasOne relationship in Laravel Eloquent

Laravel Eloquent: Query table, Fetch X number of rows on specific day (and allow for Pagination)

How to get only specific fields from Laravel Eloquent API Resource?

How to reverse Array of Data in Laravel when using Eloquent with Pagination?

how to filter an array to get specific columns in two different objects in laravel

How we get specific columns from multiple relations using With() in Laravel

How to do laravel eloquent table with joins, orwhere and pagination correctly?