I'm learning laravel but there is this error that I can solve, please help. (laravel version 5.4.17)
So there is a view where it shows all the categories and them a button to add new one. (like in the pictures)
main view for a list of categories
Here my Controller (CategoriaController.php)
<?php
namespace POS\Http\Controller;
use Illuminate\Http\Request;
use POS\Categoria;
use Illuminate\Support\Facades\Redirect;
use POS\Http\Request\CategoriaFormRequest;
use DB;
class CategoriaController extends Controller
{
public function __construct()
{
}
public function index(Request $request){
if($request){
$query=trim($request->get('SearchText'));
$categorias=DB::table('categoria')->where('nombre','LIKE','%'.$query.'%')
->where('condicion','=','1') //muestra las categorias con condicion activa
// ->orderBy('idcategoria','desc') //ordena de manera descendente
->paginate(); //paginacion que muestra registros de 10 en 10
return view('almacen.categoria.index',["categorias"=>$categorias,"searchText"=>$query]);
}
}
public function create(){
return view("almacen.categoria.create");
}
public function store(CategoriaFormRequest $request){
$categoria=new Categoria;
$categoria->nombre=$request->get('nombre');
$categoria->descripcion=$request->get('descripcion');
$categoria->condicion='1';
$categoria->save();
return Redirect::to('almacen/categoria');
}
public function show($id){
return view("almacen.categoria.show",["categoria"=>Categoria::findorfail($id)]);
}
public function edit($id){
return view("almacen.categoria.edit",["categoria"=>Categoria::findorfail($id)]);
}
public function update(CategoriaFormRequest $request,$id){
$categoria=Categoria::findorfail($id);
$categoria->nombre=$request->get('nombre');
$categoria->descripcion=$request->get('descripcion');
$categoria->update();
return Redirect::to('almacen/categoria');
}
public function destroy($id){
$categoria=Categoria::findorfail($id);
$categoria->condicion='0';
$categoria->update();
return Redirect::to('almacen/categoria');
}
}
?>
then this is my route (web.php)
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::resource('almacen/categoria','CategoriaController');
this is my Request (CategoriaFormRequest.php)
<?php
namespace POS\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CategoriaFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'nombre'=>'required|max:50',
'descripcion'=>'max:256',
];
}
}
and also this is my view (create.blade.php)
@extends ('layouts.admin')
@section ('contenido')
<div class="row">
<div class="col-lg-6 col-md-6 col sm-6 col-xs-12">
<div class="page-header">Nueva Categoría</div>
@if (count($errors)>0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li></li>
@endforeach
</ul>
</div>
@endif
{!!Form::open(array('url'=>'almacen/categoria','method'=>'POST','autocomplete'=>'off'))!!}
<div class="form-group">
<label for="nombre">Nombre</label>
<input type="text" name="nombre" class="form-control" placeholder="Nombre...">
</div>
<div class="form-group">
<label for="descripcion">Descripción</label>
<input type="text" name="descripcion" class="form-control" placeholder="Descripción...">
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Guardar</button>
<button class="btn btn-danger" type="reset">Cancelar</button>
</div>
{!!Form::close()!!}
</div>
</div>
@endsection
thanks for your time!
via Cristian Cotrina