Ajoute le champs commentaire & la mise à jour

Fix #3
This commit is contained in:
Clement 2020-07-23 15:33:48 +02:00
parent 82ec35ab4e
commit f853cf72c7
7 changed files with 188 additions and 9 deletions

View File

@ -29,6 +29,11 @@ class HomeController extends Controller
return view('homes.add');
}
public function update($id, Request $request)
{
return view('homes.update', ['home' => Home::where('id', $id)->firstOrFail()]);
}
public function store(Request $request)
{
$inputs = $request->all();
@ -45,6 +50,21 @@ class HomeController extends Controller
return redirect(route('home'));
}
public function storeupdate($id, Request $request)
{
$home = Home::where('id', $id)->firstOrFail();
$inputs = $request->all();
foreach ($inputs as $input_name => $input_value) {
if ($input_name === '_token') {
continue;
}
$home->$input_name = $input_value;
}
$home->save();
return redirect(route('home'));
}
/**
* @param \Illuminate\Http\Request $request
*

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdateHomesWithComment extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('homes', static function (Blueprint $table) {
$table->text('comment')->nullable()->comment('User comment');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('homes', static function (Blueprint $table) {
$table->dropColumn('comment');
});
}
}

View File

@ -30,7 +30,11 @@
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" name="description" id="description" rows="3"></textarea>
<textarea class="form-control" name="description" id="description" rows="6"></textarea>
</div>
<div class="form-group">
<label for="comment">Mon commentaire</label>
<textarea class="form-control" name="comment" id="comment" rows="3"></textarea>
</div>
<div class="form-group" id="photo-wrapper"></div>
<div class="row">

View File

@ -19,7 +19,7 @@
@foreach($homes as $home)
<div class="col-sm-6">
<div class="card">
<img class="card-img-top" src="{{ $home->pictures[0] }}">
<img class="card-img-top" src="{{ asset($home->pictures[0]) }}">
<div class="card-body">
<h5 class="card-title">{{ $home->title }}</h5>
<p class="card-text">{{ $home->excerpt() }}</p>

View File

@ -0,0 +1,112 @@
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ $home->title }}</div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
<form method="post">
@csrf
<div class="row">
<div class="form-group mx-sm-3 mb-2">
<label for="url" class="sr-only">URL</label>
<input type="text" name="url" class="form-control" id="url" placeholder="URL" value="{{ $home->url }}">
</div>
</div>
<div class="form-group">
<label for="title">Titre</label>
<input type="text" class="form-control" name="title" id="title" value="{{ $home->title }}" readonly>
</div>
<div class="form-group">
<label for="lat">Latitude</label>
<input type="text" name="map[lat]" id="lat" value="{{ $home->map['lat'] }}">
<label for="lng">Longitude</label>
<input type="text" name="map[lng]" id="lng" value="{{ $home->map['lng'] }}">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" name="description" id="description" rows="6">{{ $home->description }}</textarea>
</div>
<div class="form-group">
<label for="comment">Mon commentaire</label>
<textarea class="form-control" name="comment" id="comment" rows="3">{{ $home->comment }}</textarea>
</div>
<div class="form-group" id="photo-wrapper"></div>
<div class="row">
<div class="col-lg-4">
<div class="form-group">
<label for="city">Ville</label>
<input type="text" class="form-control" name="city" id="city" value="{{ $home->city }}">
</div>
</div>
<div class="col-lg-4">
<div class="form-group">
<label for="price">Prix</label>
<input type="number" class="form-control" name="price" id="price" value="{{ $home->price }}">
</div>
</div>
<div class="col-lg-4">
<div class="form-group">
<label for="surface">Surface</label>
<input type="number" class="form-control" id="surface" name="surface" value="{{ $home->surface }}">
</div>
</div>
<div class="col-lg-4">
<div class="form-group">
<label for="garden_surface">Surface de jardin</label>
<input type="number" class="form-control" id="garden_surface" name="garden_surface" value="{{ $home->garden_surface }}">
</div>
</div>
<div class="col-lg-4">
<div class="form-group">
<label for="rooms">Nombre de pièces</label>
<input type="number" class="form-control" id="rooms" name="rooms" value="{{ $home->rooms }}">
</div>
</div>
<div class="col-lg-4">
<div class="form-group">
<label for="energy">Énergie</label>
<select class="form-control" id="energy" name="energy">
<option @if($home->energy === "A") selected @endif>A</option>
<option @if($home->energy === "B") selected @endif>B</option>
<option @if($home->energy === "C") selected @endif>C</option>
<option @if($home->energy === "D") selected @endif>D</option>
<option @if($home->energy === "E") selected @endif>E</option>
<option @if($home->energy === "F") selected @endif>F</option>
<option @if($home->energy === "G") selected @endif>G</option>
</select>
</div>
</div>
<div class="col-lg-4">
<div class="form-group">
<label for="ges">GES</label>
<select class="form-control" id="ges" name="ges">
<option @if($home->ges === "A") selected @endif>A</option>
<option @if($home->ges === "B") selected @endif>B</option>
<option @if($home->ges === "C") selected @endif>C</option>
<option @if($home->ges === "D") selected @endif>D</option>
<option @if($home->ges === "E") selected @endif>E</option>
<option @if($home->ges === "F") selected @endif>F</option>
<option @if($home->ges === "G") selected @endif>G</option>
</select>
</div>
</div>
<div class="col-lg-4">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection

View File

@ -25,7 +25,7 @@
<div class="carousel-inner">
@foreach($home->pictures as $i => $pic)
<div class="carousel-item @if($i === 0) active @endif">
<img class="d-block w-100" src="{{ $pic }}">
<img class="d-block w-100" src="{{ asset($pic) }}">
</div>
@endforeach
</div>
@ -40,26 +40,35 @@
</div>
</div>
</div>
<div class="row">
@if(!empty($home->comment))
<div class="card mt-4 mb-4">
<div class="card-header">Mon commentaire</div>
<div class="card-body">
{!! nl2br($home->comment) !!}
</div>
</div>
@endif
<div class="row mb-4">
<div class="col-lg-12">
{{ $home->description }}
{!! nl2br($home->description) !!}
</div>
</div>
<div class="row">
<div class="row mb-4">
<div class="col-lg-12"><h3>Informations :</h3></div>
<div class="col-lg-3"><strong>Ville : </strong> {{ $home->city }}</div>
<div class="col-lg-3"><strong>Prix : </strong> {{ $home->price }}</div>
<div class="col-lg-3"><strong>Surface : </strong> {{ $home->surface }} </div>
<div class="col-lg-3"><strong>Nombre de pièces : </strong>{{ $home->rooms }}</div>
</div>
<div class="row">
<div class="row mb-4">
<div class="col-lg-12"><h3>Informations complémentaires :</h3></div>
<div class="col-lg-3"><strong>Surface de jardin : </strong> {{ $home->garden_surface ?? '' }}</div>
<div class="col-lg-3"><strong>Énergie : </strong> {{ $home->energy ?? '' }}</div>
<div class="col-lg-3"><strong>GES : </strong> {{ $home->ges ?? '' }}</div>
</div>
<div class="row">
<div class="col-lg-3 offset-8"><a href="{{ $home->url }}" class="btn btn-success">Voir l'origine </a></div>
<div class="col-lg-3 offset-5"><a href="{{ route('home.update', $home->id) }}" class="btn btn-light">Mettre à jour</a></div>
<div class="col-lg-3"><a href="{{ $home->url }}" class="btn btn-success">Voir l'origine </a></div>
</div>
</div>
</div>

View File

@ -27,5 +27,7 @@ Route::group(['middleware' => ['auth']], static function () {
Route::get('/home/add', 'HomeController@add')->name('home.add');
Route::post('/home/add', 'HomeController@store')->name('home.store');
Route::get('/home/{id}', 'HomeController@show')->name('home.show');
Route::get('/home/update/{id}', 'HomeController@update')->name('home.update');
Route::post('/home/update/{id}', 'HomeController@storeupdate')->name('home.store.update');
Route::post('/ajax/fetch', 'HomeController@fetch')->name('ajax.fetch');
});