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