Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use DB;
use Session;
class Flight extends Model {
protected $table = 'flight';
protected $primaryKey = 'id';
// protected $guarded = [];
// protected $hidden = ['id'];
protected $fillable = ['name','age','status'];
public $timestamps = true;
public function updateflight ($id,$data)
{
$updatedRows = App\Flight::where('id', $id)->update('name'=>$data['name'],'status'=>$data['status'])->save();
return $updatedRows;
}
public function deleteflight ($id)
{
$deletedRows = App\Flight::where('id', $id)->where('status','0')->delete();
return $deletedRows;
}
}
?>
Controller
<?php
namespace App\Http\Controllers;
use App\Models\Flight;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Session;
use DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
class FlightController extends Controller
{
public function addrecord(Request $request)
{
$postData = Input::all();
$messages = array(
'name.required'=>'You cant leave name field empty',
'age.required'=>'You cant leave age field empty',
'status.required'=>'Age cannot be blank',
);
$rules = array(
'name'=>'required | min:5',',
'age'=>''required ',
'status'=>'required',
);
$validator = Validator::make($postData, $rules,$messsages);
if($validator->fails()) {
return Redirect::to('sales/createnew')->withInput()->withErrors($validator);
}else {
$flight = new Flight;
$flight->name = $request->name;
$flight->age = $request->age;
$flight->status = $request->status;
$flight->save();
return redirect('dashboard')->with('status', 'Sucessfully inserted!');
}
}
public function updaterecord(Request $request)
{
// Validate the request...
$id = $request->id;
$flight = new Flight;
$flight_array = Flight::find( $id );
//CHECK FOR NEW OR EXISTING Flight
if( $flight_array ) {
$flight_array->name = $request->name;
$flight_array->age = $request->age;
$flight_array->status = $request->status;
$flight_array->save();
}
or
$data['name'] = $request->name;
$data['age'] = $request->age;
$data['status'] = $request->status;
$updated = $flight->updateflight($id,$data);
if($updated > 0 )
{
Session::flash('success',"Sucessfully updated!");
return redirect('dashboard');
or
return redirect('dashboard')->with('success', 'Sucessfully updated!');
} else {
return redirect('dashboard')->with('error', 'Try again!');
}
}
public function deleterecord(Request $request)
{
// Validate the request...
$id = $request->id;
$flight = new Flight;
$flight_array = Flight::find( $id );
//CHECK FOR NEW OR EXISTING Flight
if( $flight_array )
{
$flight_array->delete();
}
Or
$deletedRows = $flight->deleteflight($id);
if($deletedRows > 0 )
{
return redirect('dashboard')->with('status', 'Sucessfully deleted!');
} else {
return redirect('dashboard')->with('error', 'Try again!');
}
}
}
?>
**************************************
View file
<form action="" method="post" name="form1">
<table width="200" border="1">
<tr>
<td colspan="2">
@if(Session::has('success'))
<div class="alert alert-success">{{ Session::get('success') }}</div>
@endif
Error on view page for all the errors.
@foreach ($errors->all() as $error)
<p class="alert alert-danger">{{ $error }}</p>
@endforeach
</td>
</tr>
<tr>
<td>Name</td>
<td>
<input type="text" class="form-control" id="name" placeholder="Name" name="name" value="{{ old('name') }}">
<?php if($errors->first('name') ){ ?><div class="alert alert-danger"> {{ $errors->first(name') }}</div><?php } ?>
</td>
</tr>
<tr>
<td>Age</td>
<td>
<input type="text" class="form-control" id="age" placeholder="Age" name="age" value="{{ old('age') }}">
<?php if($errors->first('age') ){ ?><div class="alert
alert-danger"> {{ $errors->first(age') }}</div><?php }
?>
</td>
</tr>
<tr>
<td>Status</td>
<td><input name="status" type="radio" value="Active">
<input name="status" type="radio" value="Inactive">
</td>
</tr>
</table>
</form>