laravel 5.5 带枢轴的 CRUD table
laravel 5.5 CRUD with pivot table
我正在尝试使用 2 tables 和 Laravel 5.5 中的枢轴 table 创建一个 CRUD。
我是这样创建模型、控制器和迁移的:
第一步:管理应用程序中使用的语言
php artisan make:controller LangController --resource -model=Lang
php artisan make:migration create_Langs_table
第二步:管理文章中创建的文章
php artisan make:controller ArticleController --resource -model=Article
php artisan make:migration create_articles_table
第三步:我创建枢轴table
php artisan make:migration create_article_lang_table
第四步:我创建了路线
Route::resource('/articles', 'ArticleController');
第五步:我在模型中创建了关系:
在文章模型中:
public function langs(){
return $this->belongsToMany('App\Lang')->withPivot('name','shortname','description')->withTimestamps();
}
在 Lang 模型中:
public function articles(){
return $this->belongsToMany('App\Article')->withPivot('name','shortname','description')->withTimestamps();
}
我还修改了数据透视表 table 因为它包含特定字段
class CreateLangSectorTable extends Migration
{
public function up()
{
Schema::create('article_lang', function (Blueprint $table) {
$table->integer('lang_id');
$table->integer('article_id')->index('FK_ARTICLE');
$table->string('name', 80)->nullable();
$table->string('shortname', 40)->nullable();
$table->text('description', 65535)->nullable();
$table->primary(['lang_id','sector_id']);
});
}
public function down()
{
Schema::dropIfExists('langs_sectors');
}
}
当我想在 ArticleController 中创建我的 CRUD 时,我被卡住了。我试图在 ArticleController 中进行修改但没有成功。所以我删除了所有内容。
class ArticleController extends Controller
{
// Display a listing of the resource.
public function index() {
}
// Show the form for creating a new resource.
public function create() {
}
// Store a newly created resource in storage.
public function store(Request $request) {
}
// Display the specified resource.
public function show($id) {
}
// Show the form for editing the specified resource.
public function edit($id) {
}
// Update
public function update(Request $request, $id) {
}
// Remove the specified resource from storage.
public function destroy($id) {
}
}
我想在 "index":
- 显示 table 包含:名称、短名称、描述
- 按语言过滤或以特定语言显示文章
在"create"
- 我想用语言创建文章
- 我想创建一篇文章,它是另一篇文章的翻译
我真的很喜欢使用 Laravel 但我还需要更多练习 :)
感谢您的帮助
I want in the "index" to show a table with: name, shortname, description and to filter by lang or display article in a specific language
加载数据并将其传递给视图:
$lang = Lang::find($id);
$articles = $lang->articles()->get();
return view('articles.index', ['articles' => $articles]);
显示数据:
@foreach ($articles as $article)
{{ $article->pivot->name }}
{{ $article->pivot->shortname }}
@endforeach
In the "create" I want to create an article in a lang and I want to create an article which is a translation of another article
确保您已将 $fillable
数组添加到 Article
模型。创建文章:
$lang = Lang::find($id);
$article = $lang->articles()->create(['columnInArticlesTable' => $request->something]);
然后使用 attach()
方法创建翻译:
$lang->articles()->attach($article->id, ['name' => $request->name, 'shortname' => $request->shortname]);
我部分解决了我关于这个问题的问题......关于我拥有的索引方法:
public function index() {
$countLang = Lang::count();
$countArticle = Article::count();
for ($i=1; $i<=$countLang; $i++) {
$lang = Lang::find($i);
$article[$i] = $lang->article()->get();
}
return view('admin.pages.articles.index', compact('articles', 'countLang', 'countSector'));
}
它生成了正确的 SQL 请求...但是我仍然无法在视图中获取所有数据...我应该能够看到每种语言的每篇文章...
最后我在我看来做了这个:
@foreach ($articles as $article)
@for($i=0; $i < $countArticle; $i++)
{{ $article[$i]->pivot->sectname }}<br>
@endfor
@endforeach
然后我显示数据...
我正在尝试使用 2 tables 和 Laravel 5.5 中的枢轴 table 创建一个 CRUD。
我是这样创建模型、控制器和迁移的:
第一步:管理应用程序中使用的语言
php artisan make:controller LangController --resource -model=Lang
php artisan make:migration create_Langs_table
第二步:管理文章中创建的文章
php artisan make:controller ArticleController --resource -model=Article
php artisan make:migration create_articles_table
第三步:我创建枢轴table
php artisan make:migration create_article_lang_table
第四步:我创建了路线
Route::resource('/articles', 'ArticleController');
第五步:我在模型中创建了关系:
在文章模型中:
public function langs(){
return $this->belongsToMany('App\Lang')->withPivot('name','shortname','description')->withTimestamps();
}
在 Lang 模型中:
public function articles(){
return $this->belongsToMany('App\Article')->withPivot('name','shortname','description')->withTimestamps();
}
我还修改了数据透视表 table 因为它包含特定字段
class CreateLangSectorTable extends Migration
{
public function up()
{
Schema::create('article_lang', function (Blueprint $table) {
$table->integer('lang_id');
$table->integer('article_id')->index('FK_ARTICLE');
$table->string('name', 80)->nullable();
$table->string('shortname', 40)->nullable();
$table->text('description', 65535)->nullable();
$table->primary(['lang_id','sector_id']);
});
}
public function down()
{
Schema::dropIfExists('langs_sectors');
}
}
当我想在 ArticleController 中创建我的 CRUD 时,我被卡住了。我试图在 ArticleController 中进行修改但没有成功。所以我删除了所有内容。
class ArticleController extends Controller
{
// Display a listing of the resource.
public function index() {
}
// Show the form for creating a new resource.
public function create() {
}
// Store a newly created resource in storage.
public function store(Request $request) {
}
// Display the specified resource.
public function show($id) {
}
// Show the form for editing the specified resource.
public function edit($id) {
}
// Update
public function update(Request $request, $id) {
}
// Remove the specified resource from storage.
public function destroy($id) {
}
}
我想在 "index":
- 显示 table 包含:名称、短名称、描述
- 按语言过滤或以特定语言显示文章
在"create"
- 我想用语言创建文章
- 我想创建一篇文章,它是另一篇文章的翻译
我真的很喜欢使用 Laravel 但我还需要更多练习 :) 感谢您的帮助
I want in the "index" to show a table with: name, shortname, description and to filter by lang or display article in a specific language
加载数据并将其传递给视图:
$lang = Lang::find($id);
$articles = $lang->articles()->get();
return view('articles.index', ['articles' => $articles]);
显示数据:
@foreach ($articles as $article)
{{ $article->pivot->name }}
{{ $article->pivot->shortname }}
@endforeach
In the "create" I want to create an article in a lang and I want to create an article which is a translation of another article
确保您已将 $fillable
数组添加到 Article
模型。创建文章:
$lang = Lang::find($id);
$article = $lang->articles()->create(['columnInArticlesTable' => $request->something]);
然后使用 attach()
方法创建翻译:
$lang->articles()->attach($article->id, ['name' => $request->name, 'shortname' => $request->shortname]);
我部分解决了我关于这个问题的问题......关于我拥有的索引方法:
public function index() {
$countLang = Lang::count();
$countArticle = Article::count();
for ($i=1; $i<=$countLang; $i++) {
$lang = Lang::find($i);
$article[$i] = $lang->article()->get();
}
return view('admin.pages.articles.index', compact('articles', 'countLang', 'countSector'));
}
它生成了正确的 SQL 请求...但是我仍然无法在视图中获取所有数据...我应该能够看到每种语言的每篇文章...
最后我在我看来做了这个:
@foreach ($articles as $article)
@for($i=0; $i < $countArticle; $i++)
{{ $article[$i]->pivot->sectname }}<br>
@endfor
@endforeach
然后我显示数据...