serverfireteam / panel

An easily configurable admin panel for Laravel applications.
MIT License
427 stars 144 forks source link

Creating a crud #106

Closed MehmetOzkose closed 9 years ago

MehmetOzkose commented 9 years ago

I am newbie to Laravel. I have two mysql tables named galleries and gallery_images. No problem with the galleries crud.But i want to show the gallery_images related to galleries, when a gallery name clicked in crud. Do I have to create seperate files out of laravelpanel or is there an easy way to solve this problem in laravelpanel? Thanks to everyone.

AlirezaAlgo commented 9 years ago

@MehmetOzkose read this question Q: How can I display/edit data which is fetched from more than one table in CRUD ? in fag http://laravelpanel.com/docs/master/faq

MehmetOzkose commented 9 years ago

$grid = DataGrid::source(Product::with('category','material');

class Product extends \Eloquent { protected $table = 'products';

public function category() {
    return $this->belongsTo('Category', 'category_id');
}

public function material() {
    return $this->belongsTo('Material', 'material_id');
}

}

In example category,material and products are the cruds listing the all records created by panel. So I need a model file like above to establish a relation with these three cruds. Is it right?Maybe I need more experience in laravel. Thank you.

alenoosh commented 9 years ago

@MehmetOzkose Ok as I've understood you want to create a relation between two cruds, it's not straightforward but to do that you should :

1) Add a link in your 'galleries' crud :

    $imagesUrl = url('/panel/galleryImages/all?id={{ $id }}');
    $this->grid->add('<a href="' . $imagesUrl . '">View Gallery Images</a>', '');

2) Create the galleryImages crud and remove this crud from the 'links' table.

3) In your galleryImages model :

    public function galleries()
    {
        return $this->belongsTo('App\galleries');
    }

4) In your galleryImages crud you have to add a condition to list just the images of that specific gallery :

        $imagesFilter = \App\galleryImages::with('galleries');               
        $this->filter = \DataFilter::source($imagesFilter);

        $images = \App\galleryImages::whereHas('galleries', function($q) {
            $q->where('id', '=', \Input::get('id')); // id of gallery
        })->with('galleries');

    $this->filter = \DataFilter::source($images);
        .
        .

5) You also should edit the 'Add' button in 'galleryImages' crud to include the 'gallery_id' parameter and then in edit section of galleryImages crud, you should pass the gallery_id as a hidden parameter to get that parameter and save it :

    $this->edit->add('gallery_id', '', 'hidden')->insertValue(\Input::get('id'));
MehmetOzkose commented 9 years ago

Thanks for the reply. I updated my files as you told but i got "Exception in CrudController.php line 70: This url is not set yet!" error. I googled but couldn't find something that is working. Is it a route error?

MehmetOzkose commented 9 years ago

i deleted these lines in vendor/serverfireteam/panel/controllers/CrudController.php

else if (!in_array($this->entity, $configs)) { throw new \Exception('This url is not set yet!'); }

and then

modified galleryImages model so the problem was solved;
public function galleries() { return $this->belongsTo('App\galleries','gallery_id'); }

AlirezaAlgo commented 9 years ago

@MehmetOzkose are you sure you need to remove

else if (!in_array($this->entity, $configs)) { throw new \Exception('This url is not set yet!'); }

You just need to put in link table

MehmetOzkose commented 9 years ago

But i don't want gallery_imagesController link to be seen on left side menu. Is there a way to hide a link that is inserted into links table?

AlirezaAlgo commented 9 years ago

@MehmetOzkose Okay

MehmetOzkose commented 9 years ago

i can't send the input get value to edit view file by this way: $this->edit->add('gallery_id', '', 'hidden')->insertValue(\Input::get('id'));

How can I do that?

AlirezaAlgo commented 9 years ago

Try this

$this->edit->add('gallery_id', \Input::get('id'), 'hidden');
MehmetOzkose commented 9 years ago

It didn't work. In modify action and in show action the get value passes, but in 'Add' button it doesn't.

alenoosh commented 9 years ago

@MehmetOzkose The 'Add' button does not get a parameter by default. So you should override the datagrid view of rapyd in order to add the parameter. You can add a folder inside the 'resources/views/vendor/' folder named 'rapyd' and make a copy of 'datagrid.blade.php' file and edit it as required.

MehmetOzkose commented 9 years ago

@alenoosh thanks for the reply. You mean to add a parameter as get value into datagrid.blade.php. Can you please give me an example? resources/views/vendor/rapyd/datagrid.blade.php

{!! url('panel/'.$current_entity.'/edit') !!}

alenoosh commented 9 years ago

@MehmetOzkose Yes exactly I mean to add a parameter to 'edit' url in the datagrid.blade.php file inside the resources/views/vendor/rapyd directory :

{!! url('panel/'.$current_entity.'/edit?id=' . $id) !!}

You should set the value of $id previously.

MehmetOzkose commented 9 years ago

Thanks. It works

mxmtsk commented 8 years ago

@MehmetOzkose how did you solve the problem that the ID didn't get passed on to the hidden field when "adding" a new image?

$this->edit->add('gallery_id', \Input::get('id'), 'hidden');