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

RESTful Nested Resource - Filter dataTypeContent array #714

Closed
ihsanberahim opened this issue Feb 12, 2017 · 8 comments
Closed

RESTful Nested Resource - Filter dataTypeContent array #714

ihsanberahim opened this issue Feb 12, 2017 · 8 comments

Comments

@ihsanberahim
Copy link

  • Laravel Version: 5.3.*
  • Voyager Version: 0.10.13
  • PHP Version: 7.1
  • Database Driver & Version: mysql 5.7

Description:

Issue 1 - Routing & View (solved)
Issue 2 - Filter dataTypeContent array

$dataTypeContent in views/vendor/voyager/{slug}/browse.blade.php

my book-pages resource under books resources.

books/{book}/book-pages

I have no idea how to filter the dataTypeContent by the book.

@ihsanberahim ihsanberahim changed the title RESTful Nested Resource - Filter dataTypeArray RESTful Nested Resource - Filter dataTypeContent array Feb 12, 2017
@marktopper
Copy link
Contributor

What you mean on filtering dataTypeContent by the book?

@ihsanberahim
Copy link
Author

Sorry if i mistaken.

i found it in the browse.blade.php

<tbody>
@foreach($dataTypeContent as $data)
   <tr>

books hasMany book-pages

i want to filter book-pages by the book. For example:

books/1/book-pages

So, book-pages listed should have book_id = 1

@marktopper
Copy link
Contributor

I see.

In your view replace

@foreach($dataTypeContent as $data)

with

@foreach($dataTypeContent->filter(function ($item) {
        return $item->name == 'something';
    }) as $data)

Returning false in the filter-method will remove that item from the browse list.

@marktopper
Copy link
Contributor

By the way, to get the ID, you can use request()->segment(3).

Which will split the URI admin/books/1/book-pages between / and take the 3th, which is in this case 1. 🎉

@ihsanberahim
Copy link
Author

Hi @marktopper, I never know the request got the segment method as i a newbie. I appreciate that tips. Thank you!

Btw, i found controller level dataTypeContent filter.

  • create the resource controller
    php artisan make:controller BookPageController --resource

  • prepare the controller@index

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use TCG\Voyager\Http\Controllers\Traits\BreadRelationshipParser;
use TCG\Voyager\Models\DataType;
use TCG\Voyager\Voyager;

class BookPageController extends Controller
{
    use BreadRelationshipParser;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
       // GET THE SLUG, ex. 'posts', 'pages', etc.
        // $slug = $this->getSlug($request);
        $slug = 'book-pages';

        // GET THE DataType based on the slug
        $dataType = DataType::where('slug', '=', $slug)->first();

        // Check permission
        Voyager::can('browse_'.$dataType->name);

        $getter = $dataType->server_side ? 'paginate' : 'get';

        // Next Get or Paginate the actual content from the MODEL that corresponds to the slug DataType
        if (strlen($dataType->model_name) != 0) {
            $model = app($dataType->model_name);


            $relationships = $this->getRelationships($dataType);

           //here i filter the book-pages list by the book id
            if ($model->timestamps) {
                $dataTypeContent = call_user_func([$model->with($relationships)->where('book_id', request()->route('book'))->latest(), $getter]);
            } else {
                $dataTypeContent = call_user_func([$model->with($relationships)->where('book_id', request()->route('book'))->orderBy('id', 'DESC'), $getter]);
            }
            
            //Replace relationships' keys for labels and create READ links if a slug is provided.
            $dataTypeContent = $this->resolveRelations($dataTypeContent, $dataType);
        } else {
            // If Model doesn't exist, get data from table name
            $dataTypeContent = call_user_func([DB::table($dataType->name), $getter]);
        }

        $view = 'voyager::bread.browse';

        if (view()->exists("voyager::$slug.browse")) {
            $view = "voyager::$slug.browse";
        }

        return view($view, compact('dataType', 'dataTypeContent'));
    }
  • remove index from the resource
    Route::resource('books.book-pages', $namespacePrefix.'VoyagerBreadController',
    [
        'except' => 'index'
    ]);
  • point resource index to the controller method
    Route::get('books/{book}/book-pages', 'BookPageController@index')->name('books.book-pages.index');

@marktopper
Copy link
Contributor

Glad you figured it out and thanks for posting the way to do this using the controller.
This will be useful for other people in the future.

@ihsanberahim
Copy link
Author

Welcome. 👌

@github-actions
Copy link
Contributor

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 18, 2020
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

2 participants