Closed MehmetOzkose closed 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
$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.
@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'));
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?
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');
}
@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
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?
@MehmetOzkose Okay
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?
Try this
$this->edit->add('gallery_id', \Input::get('id'), 'hidden');
It didn't work. In modify action and in show action the get value passes, but in 'Add' button it doesn't.
@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.
@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') !!}
@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.
Thanks. It works
@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');
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.