Created
October 17, 2016 14:42
-
-
Save endihunter/1da38a45ac3de503721d7ea98289c31c to your computer and use it in GitHub Desktop.
adminarchitet relationships usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello. | |
Nothing can say more than an example :) | |
Let's say you have a model Post and a Post::user() belongsTo relationship as like as Post::tags() serves as many to many relationship to another tags table. | |
<?php | |
namespace App; | |
class Post extends Model | |
{ | |
// Post owner | |
public function user() | |
{ | |
return $this->belongsTo(User::class); | |
} | |
// Post tags | |
public function tags() | |
{ | |
return $this->belongsToMany(Tag::class, 'post_tags'); | |
} | |
} | |
to App\Http\Terranet\Administrator\Modules\Posts::form() method add: | |
// create dropdown element (dot notation means relationship :) ) | |
$user = FormElement::select('user.id'); | |
// add select options | |
$user->getInput()->setOptions( | |
\App\User::pluck('name', 'id')->toArray() | |
); | |
// create tags checkboxes | |
$tags = FormElement::multiCheckbox('tags.tag_id'); | |
// add checkboxes | |
$tags->getInput()->setOptions( | |
\App\Tag::pluck('title', 'id')->toArray() | |
); | |
// don't forget to push new element to final form: | |
return $this->scaffoldForm()->push($user)->push($tags); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. Great example. But seems I miss the FormElement::multiCheckbox component. I get "Unknown type: multiCheckbox".
I downloaded the latest package but there isn't.