Parcourir la source

add models Auteur Editeur Livre

Alice il y a 8 ans
Parent
commit
be41b75107

+ 8 - 7
bisame/app/Http/Controllers/PostController.php

@@ -3,18 +3,19 @@
 namespace App\Http\Controllers;
 
 use App\Repositories\PostRepository;
+use App\Repositories\TagRepository;
 use App\Http\Requests\PostRequest;
 
 class PostController extends Controller
 {
 
-    protected $postRepository;
+	protected $postRepository;
 
-    protected $nbrPerPage = 4;
+	protected $nbrPerPage = 4;
 
-    public function __construct(PostRepository $postRepository)
+	public function __construct(PostRepository $postRepository)
 	{
-		$this->middleware('auth', ['except' => 'index']);
+		$this->middleware('auth', ['except' => ['index', 'indexTag']]);
 		$this->middleware('admin', ['only' => 'destroy']);
 
 		$this->postRepository = $postRepository;
@@ -22,7 +23,7 @@ class PostController extends Controller
 
 	public function index()
 	{
-		$posts = $this->postRepository->getPaginate($this->nbrPerPage);
+		$posts = $this->postRepository->getWithUserAndTagsPaginate($this->nbrPerPage);
 		$links = $posts->setPath('')->render();
 
 		return view('posts.liste', compact('posts', 'links'));
@@ -39,7 +40,7 @@ class PostController extends Controller
 
 		$post = $this->postRepository->store($inputs);
 
-		if(isset($inputs['tags'])) 
+		if(isset($inputs['tags']))
 		{
 			$tagRepository->store($post, $inputs['tags']);
 		}
@@ -60,7 +61,7 @@ class PostController extends Controller
 		$links = $posts->setPath('')->render();
 
 		return view('posts.liste', compact('posts', 'links'))
-		->with('info', 'Résultats pour la recherche du mot-clé : ' . $tag);
+			->with('info', 'Résultats pour la recherche du mot-clé : ' . $tag);
 	}
 
 }

+ 19 - 0
bisame/app/Http/Controllers/QueryController.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Http\Controllers;
+use DB;
+use App\Models\Editeur;
+class QueryController extends Controller
+{
+
+
+    public function index()
+    {
+
+    $editeurs = DB::table('editeurs')->get();
+    foreach ($editeurs as $editeur) {
+    echo $editeur->nom, '<br>';
+}
+        return view('query');
+    }
+}

+ 1 - 1
bisame/app/Http/Requests/PostRequest.php

@@ -16,7 +16,7 @@ class PostRequest extends Request
 	{
 		return [
 			'titre' => 'required|max:80',
-			'contenu' => 'required'
+			'contenu' => 'required',
 			'tags' => ['Regex:/^[A-Za-z0-9-éèàù]{1,50}?(,[A-Za-z0-9-éèàù]{1,50})*$/']
 
 		];

+ 2 - 0
bisame/app/Http/routes.php

@@ -38,4 +38,6 @@ Route::group(['middleware' => 'web'], function () {
     Route::get('post/tag/{tag}', 'PostController@indexTag');
     Route::resource('corpus', 'CorpusController');
     Route::resource('sentence', 'SentenceController');
+    Route::resource('tag', 'TagController');
+    Route::resource('query', 'QueryController');
 });

+ 15 - 0
bisame/app/Models/Auteur.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Auteur extends Model
+{
+
+    public function livres()
+    {
+        return $this->belongsToMany('App\Models\Livre');
+    }
+
+}

+ 15 - 0
bisame/app/Models/Editeur.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Editeur extends Model
+{
+
+    public function livres()
+    {
+        return $this->hasMany('App\Models\Livre');
+    }
+
+}

+ 20 - 0
bisame/app/Models/Livre.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Livre extends Model
+{
+
+    public function auteurs()
+    {
+        return $this->belongsToMany('App\Models\Auteur');
+    }
+
+    public function editeur()
+    {
+        return $this->belongsTo('App\Models\Editeur');
+    }
+
+}

+ 13 - 21
bisame/app/Repositories/PostRepository.php

@@ -1,30 +1,20 @@
-<?php
-
-namespace App\Repositories;
+<?php namespace App\Repositories;
 
 use App\Models\Post;
 
-class PostRepository 
-{
+class PostRepository {
 
-    protected $post;
+	protected $post;
 
-    public function __construct(Post $post)
+	public function __construct(Post $post)
 	{
 		$this->post = $post;
 	}
 
-	public function getPaginate($n)
-	{
-		return $this->post->with('user')
-		->orderBy('posts.created_at', 'desc')
-		->paginate($n);
-	}
-
 	private function queryWithUserAndTags()
 	{
 		return $this->post->with('user', 'tags')
-		->orderBy('posts.created_at', 'desc');		
+			->orderBy('posts.created_at', 'desc');
 	}
 
 	public function getWithUserAndTagsPaginate($n)
@@ -35,20 +25,22 @@ class PostRepository
 	public function getWithUserAndTagsForTagPaginate($tag, $n)
 	{
 		return $this->queryWithUserAndTags()
-		->whereHas('tags', function($q) use ($tag)
-		{
-		  $q->where('tags.tag_url', $tag);
-		})->paginate($n);
+			->whereHas('tags', function($q) use ($tag)
+			{
+				$q->where('tags.tag_url', $tag);
+			})->paginate($n);
 	}
 
 	public function store($inputs)
 	{
-		$this->post->create($inputs);
+		return $this->post->create($inputs);
 	}
 
 	public function destroy($id)
 	{
-		$this->post->findOrFail($id)->delete();
+		$post = $this->post->findOrFail($id);
+		$post->tags()->detach();
+		$post->delete();
 	}
 
 }

+ 2 - 4
bisame/app/Repositories/TagRepository.php

@@ -1,7 +1,7 @@
 <?php
 namespace App\Repositories;
 
-use App\Tag;
+use App\Models\Tag;
 use Illuminate\Support\Str;
 
 class TagRepository
@@ -31,12 +31,10 @@ class TagRepository
 				$tag_ref = new $this->tag([
 					'tag' => $tag,
 					'tag_url' => $tag_url
-				]);	
-
+				]);
 				$post->tags()->save($tag_ref);
 
 			} else {
-			
 				$post->tags()->attach($tag_ref->id);
 
 			}

+ 3 - 0
bisame/composer.json

@@ -50,5 +50,8 @@
     },
     "config": {
         "preferred-install": "dist"
+    },
+    "psr-0": {
+        "Repositories": "app/"
     }
 }

+ 2 - 2
bisame/config/app.php

@@ -147,8 +147,8 @@ return [
         Illuminate\Translation\TranslationServiceProvider::class,
         Illuminate\Validation\ValidationServiceProvider::class,
         Illuminate\View\ViewServiceProvider::class,
-	Barryvdh\Debugbar\ServiceProvider::class,
-	Collective\Html\HtmlServiceProvider::class,
+		Barryvdh\Debugbar\ServiceProvider::class,
+	    Collective\Html\HtmlServiceProvider::class,
 
 
         /*

+ 76 - 0
bisame/database/migrations/2016_03_07_175637_create_livres_table.php

@@ -0,0 +1,76 @@
+<?php
+
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateLivresTable extends Migration {
+
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('auteurs', function(Blueprint $table) {
+            $table->increments('id');
+            $table->timestamps();
+            $table->string('nom', 100)->unique();
+        });
+        Schema::create('editeurs', function(Blueprint $table) {
+            $table->increments('id');
+            $table->timestamps();
+            $table->string('nom', 100)->unique();
+        });
+        Schema::create('livres', function(Blueprint $table) {
+            $table->increments('id');
+            $table->timestamps();
+            $table->string('titre', 100);
+            $table->integer('editeur_id')->unsigned();
+            $table->text('description');
+        });
+        Schema::create('auteur_livre', function(Blueprint $table) {
+            $table->increments('id');
+            $table->integer('auteur_id')->unsigned();
+            $table->integer('livre_id')->unsigned();
+        });
+        Schema::table('livres', function(Blueprint $table) {
+            $table->foreign('editeur_id')->references('id')->on('editeurs')
+                ->onDelete('restrict')
+                ->onUpdate('restrict');
+        });
+        Schema::table('auteur_livre', function(Blueprint $table) {
+            $table->foreign('auteur_id')->references('id')->on('auteurs')
+                ->onDelete('restrict')
+                ->onUpdate('restrict');
+        });
+        Schema::table('auteur_livre', function(Blueprint $table) {
+            $table->foreign('livre_id')->references('id')->on('livres')
+                ->onDelete('restrict')
+                ->onUpdate('restrict');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('livres', function(Blueprint $table) {
+            $table->dropForeign('livres_auteur_id_foreign');
+        });
+        Schema::table('auteur_livre', function(Blueprint $table) {
+            $table->dropForeign('auteur_livre_auteur_id_foreign');
+        });
+        Schema::table('auteur_livre', function(Blueprint $table) {
+            $table->dropForeign('auteur_livre_livre_id_foreign');
+        });
+        Schema::drop('auteur_livre');
+        Schema::drop('livres');
+        Schema::drop('auteurs');
+        Schema::drop('editeurs');
+    }
+
+}

+ 41 - 1
bisame/database/seeds/DatabaseSeeder.php

@@ -16,8 +16,48 @@ class DatabaseSeeder extends Seeder
     $this->call('UserTableSeeder');
     $this->call('PostTableSeeder');
     $this->call('TagTableSeeder');
-    $this->call('PostTagTableSeeder');
+        $this->call('PostTagTableSeeder');
 
     Model::reguard();
+
+
+    for ($i = 1; $i < 41; $i++) {
+
+    DB::table('editeurs')->insert(['nom' => str_random(rand(8, 22))]);
+
+    DB::table('auteurs')->insert(['nom' => str_random(rand(8, 22))]);
+
+    }
+
+
+    for ($i = 1; $i < 81; $i++) {
+
+        DB::table('livres')->insert([
+
+            'titre' => str_random(rand(8, 22)),
+
+            'editeur_id' => rand(1, 40)
+
+        ]);
+
     }
+
+
+    for ($i = 1; $i < 41; $i++) {
+
+        $number = rand(2, 8);
+
+        for ($j = 1; $j <= $number; $j++) {
+
+            DB::table('auteur_livre')->insert([
+
+                'livre_id' => rand(1, 40),
+
+                'auteur_id' => $i
+
+            ]);
+
+        }
+}
+}
 }

+ 6 - 0
bisame/database/seeds/UserTableSeeders.php

@@ -17,5 +17,11 @@ class UserTableSeeder extends Seeder {
 				'admin' => rand(0, 1)
 			]);
 		}
+		DB::table('users')->insert([
+                                'name' => 'alice' . $i,
+                                'email' => 'alice.millour' . $i . '@abtela.eu',
+                                'password' => bcrypt('password'),
+                                'admin' => '1'
+			]);
 	}
 }

+ 14 - 10
bisame/resources/views/posts/add.blade.php

@@ -5,17 +5,21 @@
 	<div class="col-sm-offset-3 col-sm-6">
 		<div class="panel panel-info">
 			<div class="panel-heading">Ajout d'un article</div>
-			<div class="panel-body"> 
+			<div class="panel-body">
 				{!! Form::open(['route' => 'post.store']) !!}
-					<div class="form-group {!! $errors->has('titre') ? 'has-error' : '' !!}">
-						{!! Form::text('titre', null, ['class' => 'form-control', 'placeholder' => 'Titre']) !!}
-						{!! $errors->first('titre', '<small class="help-block">:message</small>') !!}
-					</div>
-					<div class="form-group {!! $errors->has('contenu') ? 'has-error' : '' !!}">
-						{!! Form::textarea ('contenu', null, ['class' => 'form-control', 'placeholder' => 'Contenu']) !!}
-						{!! $errors->first('contenu', '<small class="help-block">:message</small>') !!}
-					</div>
-					{!! Form::submit('Envoyer !', ['class' => 'btn btn-info pull-right']) !!}
+				<div class="form-group {!! $errors->has('titre') ? 'has-error' : '' !!}">
+					{!! Form::text('titre', null, ['class' => 'form-control', 'placeholder' => 'Titre']) !!}
+					{!! $errors->first('titre', '<small class="help-block">:message</small>') !!}
+				</div>
+				<div class="form-group {!! $errors->has('contenu') ? 'has-error' : '' !!}">
+					{!! Form::textarea ('contenu', null, ['class' => 'form-control', 'placeholder' => 'Contenu']) !!}
+					{!! $errors->first('contenu', '<small class="help-block">:message</small>') !!}
+				</div>
+				<div class="form-group {{ $errors->has('tags') ? 'has-error' : '' }}">
+					{!! Form::text('tags', null, array('class' => 'form-control', 'placeholder' => 'Entrez les tags séparés par des virgules')) !!}
+					{!! $errors->first('tags', '<small class="help-block">:message</small>') !!}
+				</div>
+				{!! Form::submit('Envoyer !', ['class' => 'btn btn-info pull-right']) !!}
 				{!! Form::close() !!}
 			</div>
 		</div>

+ 5 - 0
bisame/resources/views/query.blade.php

@@ -0,0 +1,5 @@
+<?php
+$editeurs = DB::table('editeurs')->get();
+foreach ($editeurs as $editeur) {
+    echo $editeur->nom, '<br>';
+}

+ 4 - 2
bisame/tips-php.txt

@@ -27,6 +27,8 @@ php artisan db:seed
 /* MODELS */
 php artisan make:model App\Models\MonModele
 
+/* SEEDS */
+php artisan migrate:refresh --seed 
 
-
-
+/* DEBUGGAGE */
+dd($myvar) pour logger la valeur