Laravel 8 CRUD Edit page cannot display correctly - php

I am currently making laravel project using internet references and now I'm stuck at CRUD operation.
I use Laravel 8 with jetstream for auth, now ui template assets, bootstrap.
I tried several CRUD codes from different sources and everything worked except for edit page.
Index page:
Edit page:
This is my route:
Route::resource('projects', ProjectController::class);
My controller:
public function edit(Project $project)
{
return view('projects.edit', compact('project'));
}
public function update(Request $request, Project $project)
{
$request->validate([
'name' => 'required',
'introduction' => 'required',
'location' => 'required',
'cost' => 'required'
]);
$project->update($request->all());
return redirect()->route('projects.index')
->with('success', 'Project updated successfully');
}
The blade file:
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('projects.index') }}" title="Go back"> <i class="fas fa-backward "></i> </a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<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
<form action="{{ route('projects.update', $project->id) }}" method="POST">
#csrf
#method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" value="{{ $project->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Introduction:</strong>
<textarea class="form-control" style="height:50px" name="introduction"
placeholder="Introduction">{{ $project->introduction }}</textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Location:</strong>
<input type="text" name="location" class="form-control" placeholder="{{ $project->location }}"
value="{{ $project->location }}">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Cost:</strong>
<input type="number" name="cost" class="form-control" placeholder="{{ $project->cost }}"
value="{{ $project->location }}">
</div>
</div>
<div class="text-center col-xs-12 col-sm-12 col-md-12">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
#endsection
I tried changing the edit blade file codes to different ones but still same. I tried different method from online for the edit function in controller but nothing change. I also tried completely different coding, different model controllers and all, still the same. Only edit page turns out that way. Anyone have any clue?

Related

Laravel 8 safe (think problem datetime-local input field to mysql)

When I click save on my form, the page reloads. Which is correct until then. Unfortunately, it does not save the data for me in the database. So I tried to find the error with dd($request->all()). Unfortunately, I haven't found a real error as far as the correct data is loaded on the form. However, they never end up in the database. I currently only have one guess that it is due to the wrong format of DateTime_local input. But can't say for sure.
I don't get any error messages or anything like that.
Here is a small snippet of my code:
app/Http/Controllers/TasksController
public function store(Request $request)
{
// validate all required Data
$request->validate([
'title' => 'required',
'user' => 'required',
'labels' => 'required',
'work' => 'required',
'start_work' => 'required',
'problems' => 'required',
'stop_work' => 'required',
]);
$input = $request->all();
Task::create($input);
return back()->with('success', 'Successfully saved.');
}
app/Models/Task.php
class Task extends Model
{
use HasFactory;
public $fillable = ['title', 'user', 'labels', 'work', 'start_work', 'problems', 'stop_work'];
}
Migration
public function up()
{
Schema::create('task', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('user');
$table->string('label');
$table->string('work');
$table->string('problems');
$table->dateTime('start_work');
$table->dateTime('stop_work');
$table->timestamps();
});
}
View snippet
#extends('layouts.app')
#section('content')
<!-- Success message -->
#if(Session::has('success'))
<div class="alert alert-success">
{{Session::get('success')}}
</div>
#endif
<form method="post" action="{{ route('screate') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<section class="person">
<div class="container-fluid">
<div class="card card-default">
<div class="card-header">
<h3 class="card-title">Daten</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<!-- /.card-header -->
<div class="card-body">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="title">title</label>
<input type="text" class="form-control {{ $errors->has('title') ? 'error' : '' }}" id="title" name="title" required>
</div>
<!-- /.form-group -->
</div>
<div class="col-md-4">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control {{ $errors->has('username') ? 'error' : '' }}" id="username" name="username">
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="labels">Labels</label>
<select id="labels" name="label" class="select2 select2-hidden-accessible {{ $errors->has('label') ? 'error' : '' }}" multiple="" name="label[]" style="width: 100%;" data-select2-id="5" tabindex="-1" aria-hidden="true">
#foreach($labels as $label)
<option value="{{ $label->id }}">{{ $label->name }}</option>
#endforeach
</select>
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="work">work</label>
<input type="text" class="form-control {{ $errors->has('work') ? 'error' : '' }}" id="work" name="work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="start_work">Start-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="start_work" name="start_work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="problems">Problems</label>
<input type="text" class="form-control {{ $errors->has('problems') ? 'error' : '' }}" id="problems" name="problems">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="stop_work">Stop-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="stop_work" name="stop_work">
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div><!-- /.container-fluid -->
</section>
<section class="options">
<div class="row">
<div class="col-md-2 centered">
<button class="btn btn-danger" href="{{ route('home') }}">
<i class="fas fa-times">
</i>
Abbrechen
</button>
</div>
<div class="col-md-2">
<button class="btn btn-success" type="submit" name="submit">
<i class="fas fa-plus">
</i>
Speichern
</button>
</div>
</div>
</section>
</form>
#endsection
I hope you can help me with this small problem.
edit:
Here is my dd output:
dd() output

Route [student.update] not defined. using laravel 7

i am a beginner of laravel. i ran into the problem with Route [student.update] not defined. using laravel 7. when run the laravel project. what i tried so far i attached below.
i attached the controller and view and route file below i don't what was a problem.
Controller
public function edit(Student $student)
{
return view('edit')->with('student',$student);
}
public function update(Request $request, Student $student)
{
Student::update([
'name' => $request->name,
'phone' => $request->phone,
'address' => $request->address,
'created_at' => now(),
]);
return redirect()->route('student.index')->with('success', 'Student has been Updatedddd');
}
edit.blade.php
<form action="{{ route('student.update',$student->id) }}" method="POST">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" value="{{ $student->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Phone:</strong>
<input class="form-control" name="phone" value="{{ $student->phone }}" placeholder="Phone"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Address:</strong>
<input class="form-control" name="address" value="{{ $student->address }}" placeholder="Address"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
index.blade.php
#extends('layout')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 7 CRUD Example from scratch - ItSolutionStuff.com</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('student.create')}}"> Create New Student</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Phone</th>
<th>Address</th>
<th width="280px">Action</th>
</tr>
#foreach ($students as $student)
<tr>
<td>{{ $student->id }}</td>
<td>{{ $student->name }}</td>
<td>{{ $student->phone }}</td>
<td>{{ $student->address }}</td>
<td>
<a class="btn btn-primary" href="{{ route('student.edit',$student->id) }}">Edit</a>
<button type="submit" class="btn btn-danger">Delete</button>
</td>
</tr>
#endforeach
</table>
{!! $students->links() !!}
#endsection
Routes
Route::get('/students/{student}', 'StudentController#edit')->name('student.edit');
Route::get('/students/{student}', 'StudentController#update')->name('student.update');
Your update route is defined as a get route while your edit form is trying to submit a post request to the route
You should ideally have the update route defined as a PUT or PATCH route. And if you are using Laravel 8.x, then you should have FQCN for the controllers
//import use statements at the top
//use Illuminate\Support\Facades\Route;
//use App\Http\Controllers\StudentController;
Route::match(['PUT', 'PATCH'], '/students/{student}', [StudentController::class, 'update'])->name('student.update');
And then make a PUT or PATCH submit request from edit.blade.php
<form action="{{ route('student.update',$student->id) }}" method="POST">
#csrf
#method('PUT') //Method spoofing
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" value="{{ $student->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Phone:</strong>
<input class="form-control" name="phone" value="{{ $student->phone }}" placeholder="Phone"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Address:</strong>
<input class="form-control" name="address" value="{{ $student->address }}" placeholder="Address"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
And change the controller method
public function update(Request $request, Student $student)
{
$student->update([
'name' => $request->name,
'phone' => $request->phone,
'address' => $request->address,
'created_at' => now(),
]);
return redirect()->route('student.index')->with('success', 'Student has been Updatedddd');
}

Laravel - Database store function not working

I am having trouble when inputting data and figuring out why my data is not being stored into the database. I have tried to use the resources route and my custom route code, but it seems still nothing works. Clicking submit just seems to refresh the page with no errors to show
And here is my form :
<div class="container">
<div class="row">
<div class="col-md-12">
<nav class="navbar navbar-expand-sm navbar-dark primary justify-content-between">
<div class="navbar-brand text-dark">Create a Story</div>
</nav>
<hr class="mt-3">
<form action="{{route('story.store')}}" method="post">
#csrf
<div class="row">
<div class="col col-lg-6">
<div class="form-group my-3">
<label for="title">Title</label><br>
<input type="text" name="title" id="title" class="w-100 form-control{{ $errors->has('title') ? ' is-invalid' : '' }}" value="{{ old('title') }}" required>
#if ($errors->has('title'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('title') }}</strong></span>
#endif
</div>
</div>
<div class="col col-lg-6">
<div class="form-group my-3">
<label for="category">Category</label><br>
<select name="category" id="category" class="w-100 form-control{{ $errors->has('category') ? ' is-invalid' : '' }}" value="{{ old('category') }}" required>
<option value="Active" selected>Select Category</option>
#foreach ($category as $item)
<option value="{{$item->id}}">{{$item->nama}}</option>
#endforeach
</select>
#if ($errors->has('category'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('category') }}</strong></span>
#endif
</div>
</div>
<div class="col col-lg-6">
<div class="form-group my-3">
<label for="thumbnail">Story Thumbnail <span class="text-muted">(Recomended size is 650 x 350 pixel)</span></label><br>
<input type="file" name="thumbnail" id="thumbnail">
#if ($errors->has('thumbnail'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('thumbnail') }}</strong></span>
#endif
</div>
</div>
<div class="col col-lg-12">
<div class="form-group mt-4">
<textarea id="text-content" name="content" class="w-100 form-control{{$errors->has('content') ? ' is-invalid' : ''}}" id="content" rows="3" value="{{old('content')}}"></textarea>
#if ($errors->has('content'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('content') }}</strong></span>
#endif
</div>
</div>
</div>
<div class="modal-footer pb-0">
<div class="float-right">
<button type="submit" class="btn btn-outline-primary btn-md">Publish Story</button>
</div>
</div>
</form>
</div>
</div>
The store function in controller :
public function store(Request $request)
{
$user = Auth::user();
$request->validate([
'title' => 'required|string',
'category' => 'required',
'thumbnail' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'content' => 'required',
]);
$thumbnailName = time().'.'.request()->thumbnail->getClientOriginalExtension();
$post = new Post;
$post->title = request('title');
$post->category_id = request('category');
$post->thumbnail = request('thumbnail');
$post->content = request('content');
$post->title = request('title');
$post->date = date('d-m-Y');
$post->author_id = $user->id;
$post->save();
$request->thumbnail->move(asset('storage/uploads'), $thumbnailName);
return redirect('me/stories')->with('Succes', 'Story has been published')->with('thumbnail', $thumbnailName);
}
And this is the routes:
Route::get('me/stories', 'PostController#index')->name('story');
Route::get('me/stories/create', 'PostController#create')->name('story.create');
Route::post('me/stories/store', 'PostController#store')->name('story.store');
Route::post('ck/image_upload', 'PostController#imageUpload')->name('ck.upload');
I'm not getting any error messages at all, it's just that the submission button does nothing. Thanks a lot!
Clicking submit just seems to refresh the page with no errors to show
Your Validator is doing a return to last page with the errors.
Add this snippet of code to your blade
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Also, inspect the page before submitting and go to the network tab (check preserve log at the top) and proceed with the submission, you will get more details of what happened.

how to fetch value from database to dropdown for filter search in laravel 5.2

I am doing project in laravel 5.2, for report creation in "vehicleDetail.blade.php" i need to give one drop down option, in that i need to get vehicle names which are there in the database. From that list if i select one vehicle, select fromDate , toDate and then click on generate report i should be able to fetch data from the database (device table). For this filter engine how can i make the drop down list byb fetching data fro the database?
vehicleDetail.blade.php
#extends('app')
#section('content')
<br><br><br><br><br>
<div class="templatemo-content-wrapper">
<div class="container">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">Vehicle Detail</li>
</ol>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-success">
<div class="panel-heading">Vehicle Detail</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<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
<form class="form-horizontal" role="form" method="POST" action="{{ url('group/update/') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">Vehicle</label>
<div class="col-md-6">
<input type="text" class="form-control" name="groupID" value="{{ ('#')}}">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">From Date</label>
<div class="col-md-6">
<input type="date" class="form-control" name="Fdate">
</div>
<label class="col-md-4 control-label">To Date</label>
<div class="col-md-6">
<input type="date" class="form-control" name="Tdate" >
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-warning">
Get Report
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
Any one please help me to do this, response are appreciable.
In your controller get all values from database in a variable like:
$list = DB::table('tablename')->get(array('id', 'name'));
and pass it on view like:
return view('viewname', array('list' => $list));
Now use it on view like:
foreach($list as $key => $val)
{
//drop down html here
}

Undefined variable: errors in Laravel 5.2

I get this error exception in Laravel 5.2. I am trying to access auth/login
Undefined variable: errors
Undefined variable: errors (View:
C:\xampp\htdocs\tutorials\laravel\resources\views\auth\login.blade.php)
This is my routes.php
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
});
Route::get('about' , 'PagesController#about');
Route::resource('articles' , 'ArticlesController');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController'
]);
});
This is auth/login.blade.php
#extends('app')
#section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<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
<form class="form-horizontal" role="form" method="POST" action="{{ url('/auth/login') }}">
{!! csrf_field() !!}
<div class="form-group">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember"> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">Login</button>
<a class="btn btn-link" href="{{ url('/password/email') }}">Forgot Your Password?</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
protected $middleware = [
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
];

Categories