4
votes

I'm trying to make a CRUD using 2 tables and a pivot table in Laravel 5.5.

I created models, controllers, and migration this way:

First Step: Managing the Lang used in the application

php artisan make:controller LangController --resource -model=Lang
php artisan make:migration create_Langs_table

Second Step: Managing the Articles created in the article

php artisan make:controller ArticleController --resource -model=Article
php artisan make:migration create_articles_table

Third step: I create the pivot table

php artisan make:migration create_article_lang_table

Fourth step: I created the routes

Route::resource('/articles', 'ArticleController');

Fifth step: I created the relations in the models:

In Article model:

public function langs(){
    return $this->belongsToMany('App\Lang')->withPivot('name','shortname','description')->withTimestamps();
}

In Lang model:

public function articles(){
    return $this->belongsToMany('App\Article')->withPivot('name','shortname','description')->withTimestamps();
}

I also modified the pivot table because it contains specific fields

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');
    }
}

From there I'm stuck when I want to create my CRUD in the ArticleController. I tried to make modifications in the ArticleController but without success. So I deleted everything.

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) {
    }
}

I want in the "index":

  • to show a table with: name, shortname, description
  • to filter by lang or display article in a specific language

In the "create"

  • I want to create an article in a lang
  • I want to create an article which is a translation of another article

I really like using Laravel but I also need more practice :) Thanks for your help

3

3 Answers

3
votes

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

Load the data and pass it to the view:

$lang = Lang::find($id);
$articles = $lang->articles()->get();
return view('articles.index', ['articles' => $articles]);

Display the data:

@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

Make sure you've added the $fillable array to the Article model. Create an article:

$lang = Lang::find($id);
$article = $lang->articles()->create(['columnInArticlesTable' => $request->something]);

Then create an translation by using the attach() method:

$lang->articles()->attach($article->id, ['name' => $request->name, 'shortname' => $request->shortname]);
1
votes

I solved partially my issue concerning this question ... Concerning the index method I have :

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'));
}

It generate the right SQL requests ... But i still have issue to get all the datas in the view ... I should be able to see each article in each langs ...

0
votes

Finally i made this in my view :

@foreach ($articles as $article)
    @for($i=0; $i < $countArticle; $i++)
        {{  $article[$i]->pivot->sectname }}<br>
    @endfor
@endforeach

And i display the data ...