2020-07-23 12:26:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Home;
|
|
|
|
use App\Parser;
|
|
|
|
use Illuminate\Http\Request;
|
2020-07-23 17:21:26 +02:00
|
|
|
use Spatie\QueryBuilder\AllowedFilter;
|
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
2020-07-23 12:26:10 +02:00
|
|
|
|
|
|
|
class HomeController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Show the application dashboard.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Support\Renderable
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2020-07-23 17:21:26 +02:00
|
|
|
$homes = QueryBuilder::for(Home::class)
|
|
|
|
->allowedFilters([
|
2020-09-04 10:18:47 +02:00
|
|
|
AllowedFilter::scope('comment'),
|
|
|
|
|
2020-07-23 17:21:26 +02:00
|
|
|
AllowedFilter::scope('price_min'),
|
|
|
|
AllowedFilter::scope('price_max'),
|
2020-07-24 11:06:49 +02:00
|
|
|
|
|
|
|
AllowedFilter::scope('surface_min'),
|
|
|
|
AllowedFilter::scope('surface_max'),
|
|
|
|
|
|
|
|
AllowedFilter::scope('garden_surface_min'),
|
|
|
|
AllowedFilter::scope('garden_surface_max'),
|
2020-07-23 17:21:26 +02:00
|
|
|
])
|
|
|
|
->defaultSort('-created_at') // https://docs.spatie.be/laravel-query-builder/v2/features/sorting/#basic-usage
|
2020-07-24 11:06:49 +02:00
|
|
|
->paginate(10)
|
2020-07-23 17:21:26 +02:00
|
|
|
->appends(request()->query());
|
|
|
|
|
2020-07-23 12:26:10 +02:00
|
|
|
return view('homes.home', ['homes' => $homes]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function show($id, Request $request)
|
|
|
|
{
|
|
|
|
return view('homes.view', ['home' => Home::where('id', $id)->firstOrFail()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add()
|
|
|
|
{
|
|
|
|
return view('homes.add');
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:33:48 +02:00
|
|
|
public function update($id, Request $request)
|
|
|
|
{
|
|
|
|
return view('homes.update', ['home' => Home::where('id', $id)->firstOrFail()]);
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:26:10 +02:00
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$inputs = $request->all();
|
|
|
|
$home = new Home();
|
|
|
|
foreach ($inputs as $input_name => $input_value) {
|
|
|
|
if ($input_name === '_token') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$home->$input_name = $input_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$home->save();
|
|
|
|
|
|
|
|
return redirect(route('home'));
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:33:48 +02:00
|
|
|
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'));
|
|
|
|
}
|
2020-07-23 12:26:10 +02:00
|
|
|
/**
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
|
|
|
* @return false|string
|
|
|
|
*/
|
|
|
|
public function fetch(Request $request)
|
|
|
|
{
|
|
|
|
return json_encode(Parser::factory($request->input('url'))->parse());
|
|
|
|
}
|
|
|
|
}
|