kopia lustrzana https://github.com/pixelfed/pixelfed
				
				
				
			Add Comment model, migration and controller
							rodzic
							
								
									842af8669b
								
							
						
					
					
						commit
						b3540f189d
					
				|  | @ -0,0 +1,18 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace App; | ||||
| 
 | ||||
| use Illuminate\Database\Eloquent\Model; | ||||
| 
 | ||||
| class Comment extends Model | ||||
| { | ||||
|     public function profile() | ||||
|     { | ||||
|       return $this->belongsTo(Profile::class); | ||||
|     } | ||||
| 
 | ||||
|     public function status() | ||||
|     { | ||||
|       return $this->belongsTo(Status::class); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,42 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace App\Http\Controllers; | ||||
| 
 | ||||
| use Illuminate\Http\Request; | ||||
| use Auth; | ||||
| use App\{Comment, Profile, Status}; | ||||
| use Vinkla\Hashids\Facades\Hashids; | ||||
| 
 | ||||
| class CommentController extends Controller | ||||
| { | ||||
|     public function store(Request $request) | ||||
|     { | ||||
|       if(Auth::check() === false) { abort(403); } | ||||
|       $this->validate($request, [ | ||||
|         'item'    => 'required|alpha_num', | ||||
|         'comment' => 'required|string|max:500' | ||||
|       ]); | ||||
| 
 | ||||
|       try { | ||||
|         $statusId = Hashids::decode($request->item)[0]; | ||||
|       } catch (Exception $e) { | ||||
|         abort(500); | ||||
|       } | ||||
| 
 | ||||
|       $user = Auth::user(); | ||||
|       $profile = $user->profile; | ||||
|       $status = Status::findOrFail($statusId); | ||||
| 
 | ||||
|       $comment = new Comment; | ||||
|       $comment->profile_id = $profile->id; | ||||
|       $comment->user_id = $user->id; | ||||
|       $comment->status_id = $status->id; | ||||
|       $comment->comment = e($request->comment); | ||||
|       $comment->rendered = e($request->comment); | ||||
|       $comment->is_remote = false; | ||||
|       $comment->entities = null; | ||||
|       $comment->save(); | ||||
| 
 | ||||
|       return redirect($status->url()); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,39 @@ | |||
| <?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->bigInteger('profile_id')->unsigned(); | ||||
|             $table->bigInteger('user_id')->unsigned()->nullable(); | ||||
|             $table->bigInteger('status_id')->unsigned(); | ||||
|             $table->text('comment')->nullable(); | ||||
|             $table->text('rendered')->nullable(); | ||||
|             $table->json('entities')->nullable(); | ||||
|             $table->boolean('is_remote')->default(false); | ||||
|             $table->timestamp('rendered_at')->nullable(); | ||||
|             $table->timestamps(); | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Reverse the migrations. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function down() | ||||
|     { | ||||
|         Schema::dropIfExists('comments'); | ||||
|     } | ||||
| } | ||||
		Ładowanie…
	
		Reference in New Issue
	
	 Daniel Supernault
						Daniel Supernault