Missing required parameters for [Route: berita.update] - laravel

I'm trying to get the parameter id_berita, it appears in url, but when i want to show the previous data it can't....
Controller
class KontenController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth:admin');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$berita = Berita::all();
return view('backpages/berita', compact('berita'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('backpages.berita_input'); //untuk menampilkan form add
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
request()->validate([
'id_berita' => 'required',
'id_kategori' => 'required',
'username' => 'required',
'tanggal' => 'required',
'judul' => 'required',
'isi' => 'required',
]);
Berita::create($request->all());
$request->session()->flash('pesan','Berita '.$request['id_berita'].' berhasil disimpan.');
return redirect()->route('berita.index');
}
/**
* Display the specified resource.
*
* #param \App\Berita $berita
* #return \Illuminate\Http\Response
*/
public function show(Berita $berita)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Berita $berita
* #return \Illuminate\Http\Response
*/
public function edit(Berita $berita)
{
// return view('backpages.berita_edit',compact('berita'));
$berita = Berita::find($berita);
// return view('backpages.berita_edit')->with('berita',$berita);
return view('backpages.berita_edit')->with('berita', $berita);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Berita $berita
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Berita $berita)
{
request()->validate([
'username' => 'required',
'tanggal' => 'required',
'judul' => 'required',
'isi' => 'required',
]);
$berita->update($request->all());
$request->session()->flash('pesan','Berita '.$request['judul'].' berhasil
diperbarui.');
return redirect()->route('berita.index');
}
/**
* Remove the specified resource from storage.
*
* #param \App\Berita $berita
* #return \Illuminate\Http\Response
*/
public function destroy(Berita $berita)
{
//
}
}
berita_edit.blade.php for update process
<div class="card card-primary">
<div class="card-header">
<h3 class="card-title">Edit Berita</h3>
</div>
<!-- /.card-header -->
<!-- form start -->
<form action="{{route('berita.update')}}" method="POST">
{{ csrf_field() }}
{{method_field('PUT')}}
<div class="card-body">
<div class="form-group">
<label for="exampleInputPassword1">Username</label>
<input type="text" class="form-control" name="username" placeholder="Username" value="{{ $berita->username }}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Tanggal</label>
<input type="date" class="form-control" name="tanggal" placeholder="Tanggal" value="{{ $berita->tanggal }}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Judul</label>
<input type="text" class="form-control" name="judul" placeholder="Judul" value="{{ $berita->judul }}">
</div>
<div class="form-group">
<label>Isi</label>
<textarea class="form-control" rows="3" name="isi" value="{{ $berita->isi }}" placeholder="Enter ..."></textarea>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
For displaying data
<div class="card-body table-responsive p-0">
<table class="table table-hover text-nowrap">
<thead>
<tr>
<th>id_Berita</th>
<th>id_Kategori</th>
<th>Username</th>
<th>Tanggal</th>
<th>Judul</th>
<th>Isi</th>
<th colspan="2" style="text-align:left">Opsi</th>
</tr>
</thead>
<tbody>
#foreach($berita as $data)
<tr>
<td>{{$data -> id_berita}}</td>
<td>{{$data -> id_kategori}}</td>
<td>{{$data -> username}}</td>
<td>{{$data -> tanggal}}</td>
<td>{{$data -> judul}}</td>
<td>{{$data -> isi}}</td>
<td>
<a class="btn btn-block btn-info" href="{{ route('berita.edit', $data -> id_berita) }}"?>Edit</a>
</td>
<td>
<a class="btn btn-block btn-info" href="#"?>Hapus</a>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>

You have to change action like this
<form action="{{route('berita.update',['berita'=>$berita->id])}}" method="POST">
<a class="btn btn-block btn-info" href="{{ route('berita.edit', ['berita'=>$data->id_berita]) }}"?>Edit</a>

Related

How to add "not found" result on search data on laravel 5.8

Is there a way to add result "not found" if my data is not in the database ?
In my search button I want to add a result that: if the search doesn't conclude the data inside the defined form, the search must output data not found...
The search is working...
Here is my controller
pegawaicontroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Pegawai;
class pegawaicontroller extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$pegawais = Pegawai::orderBy('id', 'ASC')->paginate(5);
return view('pegawai.index', compact('pegawais'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('pegawai.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'kategori' => 'required',
'jabatan' => 'required',
'nama_pegawai' => 'required',
'alamat' => 'required',
'gambar_pegawai' => 'required'
]);
// upload file
$filename = time() . '.png';
$request->file('gambar_pegawai')->storeAs('public/images', $filename);
$pegawai = new Pegawai;
$pegawai->kategori = $request->input('kategori');
$pegawai->jabatan = $request->input('jabatan');
$pegawai->nama_pegawai = $request->input('nama_pegawai');
$pegawai->alamat = $request->input('alamat');
$pegawai->gambar_pegawai = $filename;
$pegawai->save();
// $pegawai = Pegawai::create($request->all());
return redirect()->route('pegawai.index')->with('message','Data berhasil dibuat!');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$pegawai = Pegawai::findOrFail($id);
return view('pegawai.show', compact('pegawai'));
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$pegawai = Pegawai::findOrFail($id);
return view('pegawai.edit', compact('pegawai'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'kategori' => 'required',
'jabatan' => 'required',
'nama_pegawai' => 'required',
'alamat' => 'required',
'gambar_pegawai' => 'required'
]);
// script delete tapii gagal
// if ($request->produk()->gambar_pegawai){
// Storage::delete('images/' . $request->produk()->gambar_pegawai);
// }
// update file
$filename = time() . '.png';
$request->file('gambar_pegawai')->storeAs('public/images', $filename);
$pegawai = Pegawai::find($id);
$pegawai->kategori = $request->input('kategori');
$pegawai->jabatan = $request->input('jabatan');
$pegawai->nama_pegawai = $request->input('nama_pegawai');
$pegawai->alamat = $request->input('alamat');
$pegawai->gambar_pegawai = $filename;
$pegawai->save();
// $pegawai = Pegawai::findOrFail($id)->update($request->all());
return redirect()->route('pegawai.index')->with('message', 'Data berhasil diubah!');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$pegawai = Pegawai::findOrFail($id)->delete();
return redirect()->route('pegawai.index')->with('message', 'Data berhasil dihapus!');
}
public function search(Request $request)
{
$q = $request->input('searchpegawai');
$pegawais = Pegawai::where('nama_pegawai', 'LIKE', '%'.$q.'%')
->orWhere('alamat', 'LIKE', '%'.$q.'%')
->paginate(5);
return view('pegawai.index')->with('pegawais', $pegawais)->with('query', $q);
}
}
Here Is The Index blade where the search function are...
index.blade.php
#extends('template.adminlte')
#section('content')
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0 text-dark">Menu Pegawai</h1>
</div><!-- /.col -->
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item">Home</li>
<li class="breadcrumb-item active">pegawai</li>
</ol>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<a href="{{ route('pegawai.create') }}" class="btn btn-info btnsm float-left">
<i class="fas fa-plus-circle"> Tambah Pegawai</i>
</a><br><br>
<form class="form-inline ml-3 float-sm-right">
<form action="searchpegawai" method="GET" role="Search" class="form-inline ml-3">
<div class="input-group input-group-sm float-sm-right">
<input class="form-control form-control-navbar float-sm-right" type="search" placeholder="Search" aria-label="Search" name=searchpegawai>
<div class="input-group-append float-sm-right">
<button class="btn btn-navbar" type="submit">
<i class="fas fa-search"></i>
</button>
</div>
</div>
</form>
#if ($message = Session::get('message'))
<div class="alert alert-success martop-sm">
<p>{{ $message }}</p>
</div>
#endif
<table class="table martop-sm">
<thead>
<th>No</th>
<!-- <th>ID</th> -->
<th>Kategori</th>
<th>Jabatan Pegawai</th>
<th>Nama pegawai</th>
<th>Alamat</th>
<th>Foto Pegawai</th>
<th>Aksi</th>
</thead>
<tbody>
#foreach ($pegawais as $key => $pegawai)
<tr>
<td>{{ $pegawais->firstItem() + $key }}</td>
<!-- <td>{{ $pegawai->id }}</td> -->
<td>{{ $pegawai->kategori }}</td>
<td>{{ $pegawai->jabatan }}</td>
<td>{{ $pegawai->nama_pegawai }}</td>
<td>{{ $pegawai->alamat }}</td>
<td><img src="{{ asset('storage/images/' . $pegawai->gambar_pegawai) }}" alt="" width="100"></td>
<td>
<form action="{{ route('pegawai.destroy', $pegawai->id) }}" method="post">
{{csrf_field()}} {{ method_field('DELETE') }}
Ubah
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Apakah anda yakin ingin menghapusnya?');">Hapus</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
{{ $pegawais -> links() }}
</div>
</section>
</div>
#endsection
use forelse in blade
#forelse($pegawais as $key => $pegawai)
<tr>
<td>{{ $pegawais->firstItem() + $key }}</td>
<!-- <td>{{ $pegawai->id }}</td> -->
<td>{{ $pegawai->kategori }}</td>
<td>{{ $pegawai->jabatan }}</td>
<td>{{ $pegawai->nama_pegawai }}</td>
<td>{{ $pegawai->alamat }}</td>
<td><img src="{{ asset('storage/images/' . $pegawai->gambar_pegawai) }}" alt="" width="100"></td>
<td>
<form action="{{ route('pegawai.destroy', $pegawai->id) }}" method="post">
{{csrf_field()}} {{ method_field('DELETE') }}
Ubah
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Apakah anda yakin ingin menghapusnya?');">Hapus</button>
</form>
</td>
</tr>
#empty
<tr><td align="center" >Result not found.</td></tr>
#endforelse
ref link https://laravel.com/docs/8.x/blade#loops

How to insert a row in Laravel given that one field does not have a default value?

I try to insert the session user id in form but it was show me error this:
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into stocks (product_name, product_code, details, price, cost, quntity, updated_at, created_at) values (basmati Rice, 23432, dskljsdlkks;lk; ;ks;lvk, 40, 75, 100kg, 2019-09-09 08:43:12, 2019-09-09 08:43:12))
I think error on this line of code which I used for store the session id
<input type="hidden" name="user_id"
value="{{ $request->session()->put('user',$request->input('id')) }}"
> class="form-control">
This is my create blade page, which I make a form and try to insert the form fields.
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-md-12">
#if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
#if(\Session::has('success'))
<div class="alert alert-success">
<p>{{ \Session::get('success') }}</p>
</div>
#endif
<div class="card">
<div class="card-header">
<h5 class="title">Add New Product on Stock</h5>
</div>
<div class="card-body">
<form method="post" action="{{url('stock')}}">
{{csrf_field()}}
<div class="row">
<div class="col-md-6 pr-1">
<div class="form-group">
<label>Product Name</label>
<input type="text" name="user_id"
value="{{Auth::user()->id }}" class="form-control">
<input type="text" name="product_name" class="form-control" placeholder="Enter Product Name" />
</div>
</div>
<div class="col-md-6 pl-1">
<div class="form-group">
<label>Product Code</label>
<input type="text" name="product_code" class="form-control" placeholder="Enter product_code" />
</div>
</div>
</div>
</br>
<div class="row">
<div class="col-md-4 pr-1">
<div class="form-group">
<label>Price</label>
<input type="text" name="price" class="form-control" placeholder="Enter price" />
</div>
</div>
<div class="col-md-4 px-1">
<div class="form-group">
<label>Cost</label>
<input type="text" name="cost" class="form-control" placeholder="Enter cost" />
</div>
</div>
<div class="col-md-4 pl-1">
<div class="form-group">
<label>Quantity</label>
<input type="text" name="quntity" class="form-control" placeholder="Enter quntity" />
</div>
</div>
</div>
</br>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Details</label>
<input type="text" name="details" class="form-control" placeholder="Enter details" />
</div>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" />
</div>
</form>
</div>
</div>
#endsection
this is my controller file of stock
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Stock;
use Auth;
class StockController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$stocks = Stock::all()->toArray();
return view('stock.index', compact('stocks'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('stock.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'user_id' =>'required',
'product_name' => 'required',
'product_code' => 'required',
'details' => 'required',
'price' => 'required',
'cost' => 'required',
'quntity' => 'required'
]);
$stock = new Stock([
'user_id' => Auth::user()->user_id,
'product_name' => $request->get('product_name'),
'product_code' => $request->get('product_code'),
'details' => $request->get('details'),
'price' => $request->get('price'),
'cost' => $request->get('cost'),
'quntity' => $request->get('quntity')
]);
$stock->save();
return redirect()->route('stock.index')->with('success', 'Data Added');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$stock = Stock::find($id);
return view('stock.edit', compact('stock', 'id'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'product_name' => 'required',
'product_code' => 'required',
'details' => 'required',
'price' => 'required',
'cost' => 'required',
'quntity' => 'required'
]);
$stock = Stock::find($id);
$stock->product_name = $request->get('product_name');
$stock->product_code = $request->get('product_code');
$stock->details = $request->get('details');
$stock->price = $request->get('price');
$stock->cost = $request->get('cost');
$stock->quntity = $request->get('quntity');
$stock->save();
return redirect()->route('stock.index')->with('success', 'Data Updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$stock = Stock::find($id);
$stock->delete();
return redirect()->route('stock.index')->with('success', 'Data Deleted');
}
}
You don't have to use the $request->input('id') to get the user id .
Simply use
Session::flash(Auth::user()->id);
For your code ,
<input type="hidden" name="user_id"
value="{{Auth::user()->id, Session::flash(auth::user()->id) }}"
> class="form-control">
And in your controller ,
when you are trying to save the user_id variable ,
EDIT ::::
$stock = new Stock([
'user_id' => Auth::user()->id,
'product_name' => $request->get('product_name'),
'product_code' => $request->get('product_code'),
'details' => $request->get('details'),
'price' => $request->get('price'),
'cost' => $request->get('cost'),
'quntity' => $request->get('quntity')
]);
Use the session variable to store ,.
Or you can directly store the user_id using Auth::user()->id in that line ,
'user_id' => Auth::user()->user_id,
change this:
$request->session()->put('user',$request->input('id'))
to like this:
Session::set('user',$request->input('id'));
Make attention, you need pass the $request to your view, so you can use the request data. Otherwise create a variable $user_id and pass it to your view blade. in this case you can do:
Session::set('user',$user_id);

PostTooLarge Exception Laravel

I have uploaded 15mb image and it throws exception of PostTooLarge Exception instead of exception i have to show flash error message but could not get it.
below is the handler i have used for Handler.php it works great but not display flash message.
if ($exception instanceof \Illuminate\Http\Exceptions\PostTooLargeException)
{
return redirect()->route('users.add')->withFlashError('Image Size too large!');
}
then i tried validate of laravel for image in my controller which is as below
$validator = Validator::make($request->all(), [
'image' => 'max:4000',
]);
if ($validator->fails())
{
return redirect()->route('user')->withFlashError('Image size exceeds 4MB');
}
but still no luck
below is blade file with form:
<form method="post" action="{{route('users.submit')}}" id="adduser" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-form-label">User name*</label>
<input type="text" class="form-control" name="name" placeholder="User name" value="{{ old('name') }}">
</div>
<div class="form-group col-md-6">
<label class="col-form-label">User email*</label>
<input type="text" class="form-control" name="email" value="{{ old('email') }}" placeholder="User email" autocomplete="off">
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-form-label">Password*</label>
<input id="password" type="password" class="form-control" name="password" placeholder="Password">
</div>
<div class="form-group col-md-6">
<label class="col-form-label">Confirm password</label>
<input type="password" class="form-control" name="confirmpassword" placeholder="Confirm password">
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-form-label">Phone number*</label>
<input type="text" class="form-control" name="mobile" value="{{ old('mobile') }}" placeholder="Phone Number">
</div>
<div class="col-md-6">
<label class="col-form-label">User role*</label>
<select name="role" class="form-control valid">
<option value="">Select Role</option>
<option {{ old('role')==3?'selected':'' }} value="3">Author</option>
<option {{ old('role')==5?'selected':'' }} value="5">Product Admin</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="col-md-6">
<label class="col-form-label">User image</label>
<div class="card-body">
<div class="was-validated">
<label class="custom-file">
<input type="file" name="image" accept=".jpg, .png,.jpeg" id="file" class="custom-file-input">
<span class="custom-file-control"></span>
</label>
<ul class="preview" style="margin-top: 10px;">
</ul>
</div>
</div>
</div>
</div>
</div>
<button style="float: right;" type="submit" class="btn btn-primary" id="validateForm">Save</button>
</form>
and below is controller code :
<?php
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Validator;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$users = User::whereNotIn('role', array(1,4))->orderBy('id','DESC')->get();
return view('admin.users.index',compact('users'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.users.add');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data=$request->all();
$validate = Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users',
]);
if ($validate->fails()) {
return redirect('users/add')->withFlashError('Email already exists')->withInput();
}
if(isset($_FILES['image']))
{
$validator = Validator::make($request->all(), [
'image' => 'max:4000',
]);
if ($validator->fails())
{
//return redirect()->route('user')->withFlashError('Image size exceeds 4MB');
//return redirect()->route('user')->withErrors(['error' => 'Image size exceeds 4MB']);
//return redirect('users/add')->withFlashError('Image size exceeds 4MB')->withInput();
return redirect()->back()->withErrors(['error' => 'Image size exceeds 4MB']);
}
if(basename($_FILES['image']['name']) != "")
{
$target_path = base_path().'/public/user/';
$time=round(microtime(true));
$newfilename = $target_path .$time . '.jpg';
$target_path = $target_path .time().basename($_FILES['image']['name']);
if (move_uploaded_file($_FILES['image']['tmp_name'], $newfilename))
{
$user_data['image'] =$time.'.jpg';
}
}
else
{
$user_data['image'] = "";
}
}
$insert_data = array
(
"name" => $data['name'],
"email" => $data['email'],
"password" => bcrypt($data['password']),
"mobile" => $data['mobile'],
"image" => $user_data['image'],
"role" => $data['role'],
"status" => 1
);
User::create($insert_data);
return redirect()->route('users')->withFlashSuccess('User added successfully');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$user = User::find($id);
$user->delete();
return redirect()->route('users')->withFlashSuccess('User deleted successfully');
}
public function delete($id)
{
$news = User::find($id);
$news->delete();
}
public function status($id)
{
$store=User::find($id);
if($store->status==1)
{
$status=0;
}
else
{
$status=1;
}
User::whereId($id)->update(array('status'=>$status));
return redirect()->route('users')->withFlashSuccess('User status changed');
}
public function saverole(Request $request)
{
$data=$request->all();
$string_role = implode(', ', $data['role']);
User::whereId($data['user_id'])->update(array('role'=>$string_role));
return redirect()->route('users')->withFlashSuccess('User role changed');
}
}
Change your validate line of code to this:
$this->validate(request(), [
'image' => 'max:4000'
],
[
'image.max' => 'Image size exceeds 4MB'
]);
And inside your view file, run this code to display the errors:
#if($errors->any())
<h4>{{$errors->first()}}</h4>
#endif
Because the php.ini stands first while throwing exception, it will not carry forward that expectation to your defined errors.

How to autofill form with id from the previous Form

I'm trying to autofill form with id of techinician from the previous form after clicking add button.
I have a list of technicians and each technician has multiple "tarification tache"associated with him then
when i press the add button i am directed to the form to add "tarification tache" in this form i have to choose the technician. So I want the form to be autofilled with the ID of the technician in question and this from the previous form.
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
#if(count($errors))
<div class="alert alert-danger" role="alert">
<ul>
#foreach($errors ->all() as $message)
<li>{{$message}}</li>
#endforeach
</ul>
</div>
#endif
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
var getMetiersByTechnicienUrl = "{{url('/metiersbytechnicien')}}";
var getTachesByMetierUrl = "{{url('/tachesbymetier')}}";
//console.log(getMetiersByTechnicienUrl,getTachesByMetierUrl,
getTarificationsByTacheUrl);
function getMetiersByTechnicien(val) {
if(val.length>0) {
var technicien_id = val;
$.get(getMetiersByTechnicienUrl+'/'+technicien_id,function(res) {
var html = '<option value="">-Select-</option>' ;
$.each(res.metiers,function(index,item) {
html+='<option
value="'+item.id+'">'+item.libelle_metier+'</option>';
});
$('#metiers').html(html);
});
}
}
function getTachesByMetier(val) {
if(val.length>0) {
var metier_id = val;
$.get(getTachesByMetierUrl+'/'+metier_id,function(res) {
var html = '<option value="">-Select-</option>' ;
$.each(res.taches,function(index,item) {
html+='<option
value="'+item.id+'">'+item.libelle_tache+'</option>';
});
$('#taches').html(html);
});
}
}
</script>
<div class="container">
<div class="row"></div>
<div class="col-md-12">
Z
<div class="col-md-10">
<h1>Tarification tache</h1>
<form action=" {{url ('tarification') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="technicien">Technicien</label>
<select onchange="getMetiersByTechnicien(this.value)"
name="technicien_id"
id="technicien" class="form-control">
<option value="">-Select-</option>
#foreach($technicien as $t)
<option value="{{$t->id }}">
{{$t->user->nom}}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="col-md-4">
<label>Metier: </label>
<select onchange="getTachesByMetier(this.value)" style="width:
200px"
class="productm form-control" id="metiers">
<option value="">-Select-</option>
</select>
</div>
<div class="col-md-4">
<label>tache: </label>
<select style="width: 200px" class="productname form-
control"
name="tache_id" id="taches">
<option value="">-Select-</option>
</select>
</div>
<div class="col-md-4">
<label>tarification: </label>
<input style="width: 200px" class="productname form-
control" type="text"
name ="Tarif" class="form-control" value="{{old('tarif')}}">
</div>
</div>
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control
btn btn-primary">
</div>
</div>
</div>
</div>
#endsection
tarificationcontroller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\tarificationtache;
use App\technicien;
use App\tache;
use App\metier;
class TarificationController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$Listtache=tache::orderBy('libelle_tache')->get();
$Listtarification=tarificationtache::all();
return view('tarification.index',['tarification'=>$Listtarification]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$technicien = technicien::orderBy('id','desc')->get();
$taches = Tache::orderBy('libelle_tache', 'asc')->get();
$metiers = Metier::orderBy('libelle_metier', 'asc')->get();
return view('tarification.create')->with('taches', $taches)-
>with('technicien', $technicien)-
>with('metiers', $metiers);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
*#return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$tarification = new tarificationtache();
$tarification ->tache_id = $request->input('tache_id');
$tarification ->Tarif =$request->input('Tarif');
$tarification->save();
$tarification->techniciens()->attach($request->technicien_id);
return redirect('technicien'); }
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$tache=Tache::find($id);
return view('tache.edit',['libelle_tache'=>$tache],['Tarif'=>$tache],
['metier_id'=>$tache]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$tache =Tache::find($id);
$tache->delete();
return redirect('tache');
}
public function getTarificationsByTache($tache_id)
{
$t =tache::find($tache_id);
return response()->json(['tarifications' => $t->tarificationtache]);
}

Laravel 5.2 FatalErrorException in casasController.php line 51

i need your help. i'm building a webApp in Laravel5.2. This is the probles i can't solve...FatalErrorException in casasController.php line 51; i really dont know what to do...the odject is correctly declared in namespace and its saved once validate!
This is my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Oggetto;
use App\Categoria;
use DB;
use Session;
class casasController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$oggetti = DB::table('casas')->get();
$categoriasSelect = DB::table('categorias')->get();
return view('pages.casa')->with('categoriasSelect', $categoriasSelect)->with('oggetti', $oggetti);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'nome' => 'required',
'categoria' => 'required',
'descrizione' => 'required'
]);
$oggetto = new Oggetto;
$oggetto->nome = $request->nome;
$oggetto->categoria = $request->categoria;
$oggetto->descrizione = $request->descrizione;
$status = $request->nome;
$oggetto->save();
Session::flash('success', 'Il tuo oggetto per la casa รจ stato salvato con successo!');
return view('pages.casa')->with('oggetto', $oggetto);
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
These are my routes:
Route::get('/', 'pagesController#index');
Route::group(array('prefix' => 'luogo'), function(){
Route::get('casa',['as' => 'opzione1', 'uses' => 'casasController#index']);
Route::get('parco',['as' => 'opzione2', function(){
return view('pages.parco');
}]);
Route::get('stazione',['as' => 'opzione3', function(){
return view('pages.stazione');
}]);
});
Route::post('nome', ['as' => 'nomeInput', 'uses' => 'pagesController#form']);
//Authentication Routes(login) -->built in
Route::get('auth/login', ['as' => 'login', 'uses' => 'Auth\AuthController#getLogin']);
Route::post('auth/login', ['as' => 'loginPost', 'uses' => 'Auth\AuthController#postLogin']);
Route::get('auth/logout', [ 'as' => 'logout', 'uses' => 'Auth\AuthController#getLogout']);
//Registration Routes(new users) -->built in
Route::get('auth/register', ['as' => 'register', 'uses' => 'Auth\AuthController#getRegister']);
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::post('categoria', ['as' => 'categ', 'uses' => 'categoriasController#newCat']);
Route::resource('oggetto/{status?}', 'casasController', ['except' => ['show']]);
This is my view:
#extends('main')
#section('content')
<h1 class="text-center">Benvenuto a Casa</h1>
<h3 class="text-center"> Aggiungi un oggetto</h3>
<div class="row">
<div class="col-md-8 col-md-offset-2">
#if(count($errors) > 0)
#foreach($errors->all() as $error)
<div class="alert alert-danger">{{$error}}</div>
#endforeach
#elseif(Session::has('success'))
<div class="alert alert-success">{{Session::get('success')}}</div>
#endif
<table class="table table-striped">
<thead>
<tr>
<th>Nome</th>
<th>Categoria</th>
<th>Descrizione</th>
<th colspan="2">Azioni</th>
</tr>
</thead>
<tbody>
#foreach($oggetti as $oggetto)
<tr>
<td>{{$oggetto->nome}}</td>
<td>{{$oggetto->categoria}}</td>
<td>{{$oggetto->descrizione}}</td>
<td><button type="button" class="btn btn-info btn-block">Editare</button></td>
<td><button type="button" class="btn btn-danger btn-block">Cancellare</button></td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
<div class="row" id="formOggetto" style="display: none; margin-top: 25px">
<div class="col-md-8 col-md-offset-2">
<form action="{{action('casasController#store')}}" method="POST">
<input type="hidden" name="_token" value="{{csrf_token()}}"/>
<div class="form-group">
<label for="nome">Nome</label>
<input type="text" class="form-control" id="nome" name="nome" placeholder="Nome">
</div>
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="categoria">Categoria</label>
<select class="form-control" name="categoria">
<option value="null">Scegli...</option>
#foreach($categoriasSelect as $categoriaSelect)
<option>{{$categoriaSelect->nome}}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-4">
<label>Aggiungi nuova categoria</label>
<button type="button" class="btn btn-warning btn-block" data-toggle="modal" data-target="#modalCateg">Aggiungi</button>
</div>
</div>
<div class="form-group">
<label for="descrizione">Descrizione</label>
<textarea type="text" class="form-control" id="descrizione" name="descrizione" placeholder="Descrizione"></textarea>
</div>
<input type="submit" class="btn btn-primary btn-block" value="Salva">
</form>
</div>
</div>
#stop
Ok, you don't have Oggetto model, so you need to create it first.
Also change this:
$oggetto = new Oggetto;
to this:
$oggetto = new Oggetto();

Resources