Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
02 adding product update 03 add comments to the product (model, migration, controller, view) 04 cosmetic changes to existing mapping and style files
- Loading branch information
yo
committed
May 21, 2019
1 parent
233036b
commit 64e3d54
Showing
17 changed files
with
644 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Comment extends Model | ||
{ | ||
protected $guarded = []; | ||
// protected $fillable = ['comment_string']; | ||
|
||
public function product() { | ||
return $this->belongsTo(Product::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use App\Comment; | ||
use App\Product; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
class ProductCommentsController extends Controller | ||
{ | ||
public function index() { // testing deletion of product comments upon product removal | ||
return $comments = Comment::all(); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function store(Product $product) { | ||
|
||
// $validator = Validator::make($request->all(), [ | ||
// 'product_id' => 'required|integer', | ||
// 'user_id' => 'required|integer', | ||
// 'guest_name' => 'nullable|string', | ||
// 'comment_string' => 'required|string', | ||
// ]); | ||
|
||
// if ($validator->fails()) { | ||
// return back()->withErrors($validator)->withInput(); | ||
// } | ||
|
||
$comment = Comment::create([ | ||
'product_id' => $product->id, | ||
'user_id' => request('user_id'), | ||
'guest_name' => request('guest_name'), | ||
'comment_string' => request('comment_string'), | ||
]); | ||
|
||
// return back(); | ||
return redirect('/products/' . $product->id . '#comment_' . $comment->id); | ||
} | ||
|
||
public function update(Comment $comment) { | ||
// dd($comment); | ||
// dd(request()->all()); | ||
|
||
$comment->update([ | ||
'comment_string' => request('comment_string'), | ||
]); | ||
|
||
// return redirect()->route('productsShow', ['product' => $comment->product_id]); | ||
return redirect('/products/' . $comment->product_id . '#comment_' . $comment->id); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @param \App\Comment $comment | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function destroy(Comment $comment) | ||
{ | ||
// dd($comment); | ||
$product_id = $comment->product_id; | ||
$comment->delete(); | ||
|
||
return redirect()->route('productsShow', ['product' => $product_id]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
/* @var $factory \Illuminate\Database\Eloquent\Factory */ | ||
|
||
use App\Comment; | ||
use Faker\Generator as Faker; | ||
|
||
$factory->define(Comment::class, function (Faker $faker) { | ||
return [ | ||
// | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
database/migrations/2019_05_20_010314_create_comments_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateCommentsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('comments', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->unsignedInteger('product_id'); | ||
$table->unsignedInteger('user_id')->nullable(); | ||
// ??? $table->foreign('user_id')->references('id')->on('users'); | ||
$table->string('guest_name')->nullable(); | ||
$table->text('comment_string'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('comments'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ public function run() | |
|
||
$this->call([ | ||
ProductsTableSeeder::class, | ||
UsersTableSeeder::class, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Seeder; | ||
|
||
class UsersTableSeeder extends Seeder | ||
{ | ||
/** | ||
* Run the database seeds. | ||
* | ||
* @return void | ||
*/ | ||
public function run() | ||
{ | ||
DB::table('users')->insert([ | ||
'name' => 'Admin Name', | ||
'email' => 'admin@gmail.com', | ||
// 'role_id' => 1, | ||
'password' => bcrypt('111111'), | ||
]); | ||
} | ||
} |
Oops, something went wrong.