Skip to content

Instantly share code, notes, and snippets.

@endihunter
Created October 17, 2016 14:42
Show Gist options
  • Save endihunter/1da38a45ac3de503721d7ea98289c31c to your computer and use it in GitHub Desktop.
Save endihunter/1da38a45ac3de503721d7ea98289c31c to your computer and use it in GitHub Desktop.
adminarchitet relationships usage
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);
?>
@dannoso
Copy link

dannoso commented Oct 17, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment