LARAVEL 6.x = Trying to get property 'id' of non-object - php

I wish to be able to edit my users through the admin panel but this returns the following error to me:
Trying to get property 'id' of non-object
it will be an error in my view with the call of the variable ID if I change it I have the same thing with my variable name.
I use the users table and in no other place in my code do I have problems
help me please
URI : /role-edit/{id}
View :
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>Edit register roles</h4>
</div>
<div class="card-body">
<form action="/role-register-update/{{ $users->id }}" method="POST">
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="form-group">
<label>Name</label>
<input type="text" name="name" value="{{ $users->name }}" class="form-control">
</div>
<div class="form-group">
<label>Give role</label>
<select name="type" class="form-control">
<option value="admin">Admin</option>
<option value="vendor">Vendor</option>
<option value="">None</option>
</select>
<button type="submit" class="btn btn-success">Update</button>
Cancel
</div>
</form>
</div>
</div>
</div>
</div>
</div>
Controller :
class DashboardController extends Controller
{
public function registered()
{
$users = User::all();
return view('admin.registeradmin')->with('users', $users);
}
public function edit(Request $request,$id)
{
$users = User::findOrFail($id);
return view('admin.edit-register')->with('users',$users);
}
public function update(Request $request, $id)
{
$users = User::findOrFail($id);
$users->name = $request->input('name');
$users->usertype = $request->input('type');
$users->update();
return redirect('/role-register')->with('status', 'You data is update');
}
public function destroy($id)
{
$users = User::where('id', $id);
if ($users != null)
{
$users->delete();
return redirect('/role-register')->with('status', 'User is correctly deleted !');
}
return redirect('/role-register')->with('status', 'User is not correctly deleted !');
}
}
Routes :
Route::get('/', function () {
return view('pages.home');
});
Route::get('/aboutus', function () {
return view('pages.aboutus');
})->name('aboutus');
Auth::routes();
Route::get('profile', 'UserProfileController#show')->middleware('auth')->name('profile.show');
Route::post('profile', 'UserProfileController#update')->middleware('auth')->name('profile.update');
Route::get('/home', 'HomeController#index')->name('home');
Route::group(['middleware' => ['auth', 'admin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('/role-register', 'Admin\DashboardController#registered');
Route::get('/role-edit/{id}', 'Admin\DashboardController#edit');
Route::put('/role-register-update/{id}', 'Admin\DashboardController#update');
Route::delete('/role-delete/{id}', 'Admin\DashboardController#destroy');
});

Add dd($users) in the edit function of your controller. If you get the data, add the following to the form action form action:
{{route('routename',['id'=>$users->id])}}

// Controller
public function Updateprofile(Request $request)
{
if (Auth::check() && Auth::user()->role->id == 2) {
$this->validate($request, [
'name' => 'required',
'email' => 'required|email'
]);
$image = $request->file('image');
$slug = str_slug($request->name);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(600,500);
if (!file_exists('storage/uploads/profile'))
{
mkdir('storage/uploads/profile',0777,true);
}
unlink('storage/uploads/profile/'.Auth::user()->image);
$image_resize->save('storage/uploads/profile/'.$imagename);
}else{
$imagename = Auth::user()->image;
}
$user = User::find(Auth::id());
$user->name = $request->name;
$user->email = $request->email;
$user->image = $imagename;
$user->save();
Toastr::success('Profile Successfully Updated :)', 'Success');
return redirect()->back();
}
}
// blade file
<form method="POST" action="{{route('user.profile.update')}}" class="form-horizontal" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="name">Name : </label>
</div>
<div class="col-lg-10 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="text" id="name" class="form-control" placeholder="Enter your name" name="name" value="{{Auth::user()->name}} {{old('name')}}">
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="image">{{__('Image')}} : </label>
</div>
<div class="col-lg-10 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="file" name="image" >
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="email_address_2">Email Address</label>
</div>
<div class="col-lg-10 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="text" id="email_address_2" class="form-control" value="{{Auth::user()->email}} {{old('email')}}" placeholder="Enter your email address" name="email" ">
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-offset-2 col-md-offset-2 col-sm-offset-4 col-xs-offset-5">
<button type="submit" class="btn btn-primary m-t-15 waves-effect">UPDATE</button>
</div>
</div>
</form>

Related

dropzone using a form in laravel

I added dropzone in a form in laravel and every single time I add the images the form immediately submits without me clicking on the submit button. Please refer to following codes:
Here are the codes from the dropzone vue component
<template>
<div class="container" ref="imageUpload">
<div class="row justify-content-center mb-3">
<div class="col-12 bg-dark text-white rounded py-3 my-2 text-center">
DROP IMAGE(S) HERE
</div>
</div>
</div>
</template>
<script>
import Dropzone from 'dropzone';
export default {
data: function () {
return{
dropzone: null
}
},
mounted() {
this.dropzone = new Dropzone(this.$refs.imageUpload, {
url: '/blog'
});
}
}
</script>
Here are the codes from the BlogsController.php:
public function store(Request $request)
{
if(! is_dir(public_path('blog_images'))){
mkdir(public_path('/blog_images'), 0777);
}
$blogimages = Collection::wrap( request()->file('file'));
$blogimages->each(function($blogimage){
$basename = Str::random();
$original = $basename . '.'. $blogimage->getClientOriginalExtension();
$blogimage->move(public_path('/blog_images'), $original);
Blog::create([
'original' => '/blog_images/' . $original,
]);
});
$blog = Blog::create($this->validateRequest());
return view('blog.index');
}
public function validateRequest()
{
return request()->validate([
'title' => 'required',
'caption' => 'required',
]);
}
Here are the codes from the create.blade.php which contains the form:
<div class="container mt-5 blog-body">
<form action="/blog" method="POST" enctype="multipart/form-data">
<div class="form-group row">
<label for="title" class="col-4 col-form-label">Blog Title</label>
<div class="col-8">
<input id="title" type="text" class="form-control #error('title') is-invalid #enderror" name="title"
value="{{old('title') }}" autocomplete="title" placeholder="Title">
<div class="text-danger">{{ $errors->first('title') }}</div>
</div>
</div>
<div class="form-group row">
<label for="caption" class="col-4 col-form-label">Blog Caption</label>
<div class="col-8">
<input id="caption" type="text" class="form-control #error('caption') is-invalid #enderror" name="caption"
value="{{old('caption') }}" autocomplete="caption" placeholder="caption">
<div class="text-danger">{{ $errors->first('caption') }}</div>
</div>
</div>
<div class="form-group row">
<label class="col-4 col-form-label">Blog Images</label>
<div class="col-8" id="app">
<dropzone-component></dropzone-component>
</div>
</div>
<button type="submit" class="btn btn-primary"> Post Blog</button>
#csrf
</form>
Here are the codes from the web.php file:
Route::resource('blog', 'BlogsController');
Here are the codes from the create_blogs_table.php
Schema::create('blogs', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('original');
$table->string('title');
$table->string('caption');
});
You can set the options:
autoProcessQueue: false
parallelUploads: 10
So the dropzone does not upload immediately.

How I upload an image on laravel 6?

im trying to edit an image on laravel 6, but but it does not advance to next view, stays on the form view.
I have seen many tutorials of laravel 5.8 and 6. I can't make it work in any way
This is de controller:
public function update(Request $request, $id)
{
$validator = $request->validate([
'titulo' => 'required | max:50', //campo obligatorio y máximo 50 caracteres
'contenido' => 'required | max:150',
'imagen' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:4096',
]);
$image_name = time().'.'.$request->imagen->getClientOriginalExtension();
$request->image->move(public_path('images'), $image_name);
$datos = array(
'titulo' => $request->titulo,
'contenido' => $request->contenido,
'imagen' => $image_name,
);
Noticia::whereId($id)->update($datos);
return redirect('/mostrar');
}
THis is Web.php file:
Route::get('/actualizar/{id}', 'crearNoticiaController#update')->name('actualizar');
Route::get('/editar/{id}', 'crearNoticiaController#edit')->name('editar');
this is form file:
<div class="subir-imagen">
<form method="get" action="{{ route('actualizar', $noticia->id) }}" enctype="multipart/form-data">
#csrf
<div class="crear-titulo">
<input class="titulo" type="text" name="titulo" placeholder="Escriba el titulo" value="{{$noticia->titulo}}">
</div>
<div class="crear-contenido">
<textarea class="ckeditor" name="contenido" placeholder="Escriba el contenido" >
{{$noticia->contenido}}
</textarea>
</div>
<table border="2">
<tr>
<td><img src="{{URL::to('/')}}/images/{{$noticia->imagen}}" alt="imagen" width="250" align="left"/></td>
</tr>
</table>
<div class="form-group">
<div class="col-md-6">
<input type="file" class="form-control" name="imagen" />
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<input type="submit" class="btn btn-primary" value="Enviar" id="btn-enviar" />
</div>
</div>
</form>
</div>
Thnaks for help
I faced same issue, but luckily i solved this problem.I added my solution below, i think this will help you to solve this problem
public function updatePost(Request $request, $id)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:25|min:4',
'image' => 'mimes:jpeg,jpg,png,JPEG,JPG,PNG | max:100000',
]);
$data = array();
$data['category_id'] = $request->category_id;
$data['title'] = $request->title;
$data['details'] = $request->details;
$image = $request->file('image');
if($image)
{
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name.'.'.$ext;
$upload_path = 'public/assets/img/';
$image_url = $upload_path.$image_full_name;
$success = $image->move($upload_path,$image_full_name);
$data['image'] = $image_url;
unlink($request->old_photo);
$posts = DB::table('posts')->where('posts.id', $id)->update($data);
if($posts)
{
return Redirect()->route('all.posts')->with('success','Posts are inserted successfully');
}
else
{
return back()->with('error', 'Posts are not inserted successfully');
}
}
else
{
$data['image'] = $request->old_photo;
$posts = DB::table('posts')->where('posts.id', $id)->update($data);
if($posts)
{
return Redirect()->route('all.posts')->with('success','Posts are inserted successfully');
}
else
{
return back()->with('error', 'Posts are not inserted successfully');
}
}
}
edit_post.blade.php
#extends('welcome')
#section('content')
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<p>
List Posts
</p>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ url('posts.update_posts/'.$posts->id) }}" method="post" enctype="multipart/form-data">
#csrf
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<div>Category Name</div>
<label>Category ID</label>
<select class="form-control" name="category_id">
#foreach($category as $categories)
<option value="{{ $categories->id }}" <?php if ($categories->id == $posts->category_id)
echo "selected"; ?> > {{ $categories->name }} </option>
#endforeach
</select>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label>Product Title</label>
<input type="text" name="title" class="form-control" value="{{ $posts->title }}" id="title" required data-validation-required-message="Please product name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label>Details</label>
<textarea name="details" rows="5" class="form-control" value="{{ $posts->details }}" id="details"></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label>Product Image</label>
<input type="file" name="image" class="form-control" id="image"><br/>
Old Image : <img src="{{ URL::to($posts->image) }}" style="hight: 40px; width: 100px">
<input type="hidden" name="old_photo" value="{{ $posts->image }}">
</div>
</div>
<br>
<div id="success"></div>
<div class="form-group">
<button type="submit" class="btn btn-success" id="sendMessageButton">Update</button>
</div>
</form>
</div>
</div>
</div>
#endsection
First run on your project console command:
php artisan storage:link
Then try this code and if return any error message tell me khow:
$imagen = $request->file("imagen");
$extension = $imagen->extension();
$filename = time().".".$extension;
$request->file('imagen')->storeAs("public/images", $filename);
Finally check your public/images folder for image file exists.
Also you can read about storing uploaded files in laravel 6.x official documentation
I've solved with this way:
In web.php I put patch instead get
Route::patch('/actualizar/{id}', 'crearNoticiaController#update')->name('actualizar');
In the edit blade I put: #method('PATCH')
And this is the update in the controller:
public function update(Request $request, $id)
{
$noticia = Noticia::findOrFail($id);
$noticia->titulo = $request->get('titulo');
$noticia->contenido = $request->get('contenido');
$noticia->imagen = $request->file('imagen');
$validator = $request->validate([
'imagen' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:4096',
]);
$imageName = time().'.'.request()->imagen->getClientOriginalExtension();
request()->imagen->move(public_path('images'), $imageName);
$noticia->imagen = $imageName;
$noticia->update();
return redirect('/mostrar'); //Redirigimos a la la vista para mostrar las noticias
}

How to give a more specific role, like read, create, update, and delete? here I use laravel 5.8

For now the role that I created has been running properly, but when I add read, create, update and delete I have difficulty. this is what I expect:
my expectations
This is my roles table:
roles table
This is my user_role table:
user_role
This is my User Model:
public function roles() {
// return $this->belongsToMany('App\Role');
//test
return $this->belongsToMany('App\Role')->withPivot([
'rd',
'cr',
'up',
'dl'
]);
}
public function hasAnyRoles($roles) {
return null !== $this->roles()->whereIn('kd_role', $roles)->first();
}
public function hasAnyRole($role) {
return null !== $this->roles()->where('kd_role', $role)->first();
}
public function karyawan()
{
return $this->hasOne('App\Karyawan', 'nik', 'nik');
}
public function roleUser() {
return $this->hasMany('App\RoleUser');
}
This is my User Controller
public function edit($id)
{
$id = Crypt::decrypt($id);
//Auth user
$authUser = Auth::user()->id;
$authAdmin = Auth::user()->is_admin;
if ($authUser == $id && $authAdmin == false) {
return redirect('/users')->with('warning', 'Kamu Tidak Dapat Mengubah Data Dirimu Sendiri.');
}
return view('layouts.user.edit')->with(['user' => User::with('roles')->find($id), 'roles' => Role::all()]);
// Test
// $user = User::with('roles')->find($id);
// return json_encode($user);
}
And this is my view:
<div class="form-group row">
<label for="role"
class="col-sm-2 col-form-label col-form-label-sm">Role</label>
<!-- Input text validasi -->
<div class="col-sm-10">
#if ($roles->count())
#foreach($roles as $role)
<div class="custom-control custom-checkbox mb-3">
<div class="row">
<div class="col-sm-2">
<input name="roleUser[]" value="{{$role->id}}" {{ $user->hasAnyRole($role->kd_role)?'checked':'' }} id="customCheck{{$role->id}}" class="custom-control-input" type="checkbox">
<label class="custom-control-label" for="customCheck{{$role->id}}">{{$role->nm_role}}</label>
</div>
<div class="col-sm-10">
{{-- Testing --}}
<div class="col d-inline mr-3">
<input name="create" value="" id="customCheck111" class="custom-control-input" type="checkbox">
<label class="custom-control-label">Read</label>
</div>
<div class="col d-inline mr-3">
<input name="create" value="" id="customCheck111" class="custom-control-input" type="checkbox">
<label class="custom-control-label">Create</label>
</div>
<div class="col d-inline mr-3">
<input name="update" value="" id="customCheck222" class="custom-control-input" type="checkbox">
<label class="custom-control-label">Update</label>
</div>
<div class="col d-inline mr-3">
<input name="delete" value="" id="customCheck333" class="custom-control-input" type="checkbox">
<label class="custom-control-label">Delete</label>
</div>
{{-- End Testing --}}
</div>
</div>
</div>
#endforeach
#endif
</div>
Thanks a lot.

Edit page is not working(Undefinded variable branch) In Laravel

I have edit page contains 3 fields company, dealership, branch, when I click one of the save list of the branch I need to show the details into the page but when I did this it shows undefined variable error throwing up, how to fix this and also need to update the form after listing the details in the corresponding fields
Edit Page
#include('theme.header')
<?php
use App\Company;
?>
<div class="page-content-wrapper ">
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<div class="page-title-box">
<div class="btn-group float-right">
</div>
<h4 class="page-title">Branch Management</h4>
</div>
</div>
</div>
<!-- end page title end breadcrumb -->
<div class="row">
<div class="col-12">
<div class="card m-b-30">
<div class="card-body">
<h4 class="mt-0 header-title">Branch</h4>
<br>
<br>
{!! Form::open(['method' => 'PUT', 'route' => ['branchs.update',$branch->id]] ) !!}
<div class="form-group row">
<label class="col-sm-2 col-form-label">Company</label>
<div class="col-sm-10">
<?php
$comp=Company::where('comp_id',$branch->comp_id)->first();
$companies=Company::where('status','0')
->get();
?>
<input type="hidden" name="br_id" id="br_id" value="{{$branch->br_id}}">
<select class="form-control" id="company" name="company">
<option selected value="{{$branch->br_id}}">{{$comp->name}}</option>
#foreach($companies as $company)
<option value="{{$company->comp_id}}">{{$company->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Dealership</label>
<div class="col-sm-10">
<?php
$cn = App\Dealership::where('dlr_id', $branch->dlr_id)->first();
$companies =App\Dealership::where('status', '0')
->get();
?>
<select class="form-control" id="dealer" name=" dealer">
<option>Select Dealership</option>
#foreach($dealership as $dealerships)
<option value="{{$dealerships->dlr_id}}">{{$dealerships->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label for="example-text-input" class="col-sm-2 col-form-label">Branch Name</label>
<div class="col-sm-10">
<input class="form-control" type="text" id="branch" name="branch" value="{{$branch->name}}">
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="page-title-box">
<div class="btn-group float-right">
<button class="btn btn-primary" type="submit">Button</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div> <!-- end col -->
</div> <!-- end row -->
</div>
</div>
#include('theme.footer')
Controller File
<?php
namespace App\Http\Controllers;
use App\Branch;
use App\Company;
use App\Dealership;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class BranchController extends Controller
{
public function index()
{
$companies = Company::where('status', '0')->get();
$dealership = Dealership::where('status', '0')->get();
$branches = Branch::where('status', '0')->get();
return view('branch.index', compact('branches'));
}
public function store(Request $request)
{
$branch_id = new Branch;
$branch_id = Branch::orderBy('br_id', 'desc')->take(1)->get();
if (count($branch_id) > 0) {
$id = $branch_id[0]->br_id;
$id = $id + 1;
} else {
$id = 1;
}
$branch = new Branch;
$branch->br_id = $id;
$branch->name = $request->input('branch');
$branch->dlr_id = $request->input('dealer');
$branch->comp_id = $request->input('company');
$branch->created_id = '0';
$branch->save();
return redirect()->back()->with('message', 'Successfully saved');
}
public function edit(Branch $branch)
{
$branch=Branch::where('id',$branch->id)->first();
return view('branch.edit',['branches'=>$branch]);
}
public function update(Request $request,$id)
{
$branch = Branch::findOrFail($id);
$branch->status = '1';
$branch->save();
if ($branch) {
$branchs = new Branch();
$branchs->comp_id = $request->input('company');
$branchs->dlr_id = $request->input('dealer');
$branchs->name = $request->input('branch');
$branchs->created_id = '0';
$branchs->save();
if ($branchs) {
return redirect('/branch')->with('message', 'Successfully saved');
}
}
}
public function destroy(Branch $branch)
{
DB::table('branches')
->where('id', $branch->id)
->update(['status' => '-1']);
return back()->with('message', 'Successfully Deleted');
}
}
You are passing branch with parameter name branches. Change your controller code to:
public function edit(Branch $branch)
{
$branch=Branch::where('id',$branch->id)->first();
return view('branch.edit',['branch'=>$branch]);
}
You are sending 'branches' from your controller and used $branch in your view! Try to change it in your edit() function like:
public function edit(Branch $branch)
{
$branch=Branch::where('id',$branch->id)->first();
return view('branch.edit',['branch' => $branch]);
}
And also, in your index() function change
return view('branch.index', compact('branches'));
To
return view('branch.index', compact(['branches', 'companies', 'dealership']));
Hope this will helps you!
Everything seems to be correct just that you are sending branches instead of branch and when the view looks up for branch it's not available .. that's the error you are getting

Validation error message not showing and datas not being saved to database

I am trying to first validate the inputs and then add the values to database but neither I am able to see the validation error message nor I am able to insert records to database. I have a table in database named 'news' where I have
this is my boxed.blade.php
<form class="form-horizontal" role="form" method="post" action="/addNews" novalidate>
<div class="form-group">
<label for="inputEmail1" class="col-lg-2 col-sm-2 control-label">Title</label>
<div class="col-lg-10">
<input type="text" name="title" class="form-control" id="inputEmail1" placeholder="News Title" value="{{ Input::old('title') }}">
</div>
</div>
<div class="form-group">
<label for="inputPassword1" class="col-lg-2 col-sm-2 control-label">Description</label>
<div class="col-lg-10">
<textarea name="description" class="form-control" rows="6">{{ Input::old('description') }}</textarea>
</div>
</div>
<div class="form-group">
<label for="inputEmail1" class="col-lg-2 col-sm-2 control-label">Reporter</label>
<div class="col-lg-10">
<input type="text" name="reported_by" class="form-control" id="inputEmail1" placeholder="Reporter Name" value="{{ Input::old('reported_by') }}">
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<div class="checkbox">
<label>
<input type="checkbox" name="status"> Status
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-danger"><i class="fa fa-comments"></i> Add To List</button>
</div>
</div>
</form>
And routes.php
Route::get('addNews', function()
{
return View::make('pages.boxed');
}
);
Route::post('addNews', function()
{
//processing the form
$rules = array(
'title' => 'required',
'description' => 'required|min:50',
'reported_by' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()){
$message = $validator->message();
return Redirect::to('addNews')->withErrors($validator)->withInput(Input::all());
}
else{
$news = new News;
$news->title = Input::get('title');
$news->description = Input::get('description');
$news->reported_by = Input::get('reported_by');
$news->status = Input::get('status');
$news->save();
return Redirect::to('addNews');
}
}
This is my model News.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class News extends Model
{
protected $fillable = array('title', 'description', 'reported_by', 'status');
}
If you want to see the error, place the following code above your form:
#if (count($errors) > 0)
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
UPDATE 1: Instead of defining the methods in routes.php file, define it in a Controller called NewsController
So, to do this, update your routes.php file to this:
Route::get('/addNews', 'getNewsForm');
Route::post('/addNews', 'postNewsForm');
NewsController.php
/**
* Display the add news form to the user.
*
* #return view
*/
public function getNewsForm()
{
return View::make('pages.boxed');
}
/**
* Store the input in the database after validation
*
* #param $request Illuminate\Http\Facade\Request
*/
public function postNewsForm(Request $request)
{
$this->validate($request, [
'title' => 'required',
'description' => 'required|min:50',
'reported_by' => 'required'
]);
News::create($request->all());
return redirect('/addNews');
}

Categories