94 lines
2.4 KiB
PHP
94 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Home;
|
|
use App\Parser;
|
|
use Illuminate\Http\Request;
|
|
use Spatie\QueryBuilder\AllowedFilter;
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
|
|
|
class HomeController extends Controller
|
|
{
|
|
/**
|
|
* Show the application dashboard.
|
|
*
|
|
* @return \Illuminate\Contracts\Support\Renderable
|
|
*/
|
|
public function index()
|
|
{
|
|
$homes = QueryBuilder::for(Home::class)
|
|
->allowedFilters([
|
|
AllowedFilter::scope('price_min'),
|
|
AllowedFilter::scope('price_max'),
|
|
|
|
AllowedFilter::scope('surface_min'),
|
|
AllowedFilter::scope('surface_max'),
|
|
|
|
AllowedFilter::scope('garden_surface_min'),
|
|
AllowedFilter::scope('garden_surface_max'),
|
|
])
|
|
->defaultSort('-created_at') // https://docs.spatie.be/laravel-query-builder/v2/features/sorting/#basic-usage
|
|
->paginate(10)
|
|
->appends(request()->query());
|
|
|
|
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');
|
|
}
|
|
|
|
public function update($id, Request $request)
|
|
{
|
|
return view('homes.update', ['home' => Home::where('id', $id)->firstOrFail()]);
|
|
}
|
|
|
|
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'));
|
|
}
|
|
|
|
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
|
|
*
|
|
* @return false|string
|
|
*/
|
|
public function fetch(Request $request)
|
|
{
|
|
return json_encode(Parser::factory($request->input('url'))->parse());
|
|
}
|
|
}
|