Edit page show blank in laravel - php

I have news details inserted and i need to show it on the edit page but when i try to edit and delete it shows blanks page insert and show is working properly. i have been stucked on this from morning. id is getting from the database but it shows a blank page,Not using any Form helper
1.what's problem,is it on route file
2.is it on Controller file
route.php
Route::get('/', function () {
return view('welcome');
});
Route::resource('books','BookController');
Route::resource('news','NewsController');
Auth::routes();
Route::get('/news','NewsController#index')->name('news');
//Route::get('/news/create','NewsController#create');
//Route::get('/news/edit','NewsController#edit');
Edit.blade.php
#extends('theme.default')
#section('content')
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
NEW BEE NEWS DETAILS
</div>
<div class="panel-body">
<div class="row">
<div class="col-sm-12">
<form method="post" action="{{route('news.update',[$news->id])}}"
enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" name="_method" value="put">
<div class="form-group">
<label>NEWS TITLE</label>
<input type="text" name="atitle" id="atitle" class="form-control"
placeholder="PLEASE ADD TITLE OF NEWS" value="{{$news->name}}">
<p class="help-block">Example: SELFY PLAYSHARE </p>
</div>
<div class="form-group">
<label>NEWS</label>
<textarea name="news" id="news" class="form-control" {{$news->news}}></textarea>
<p class="help-block">DETAILED NEWS HERE</p>
</div>
<div class="form-group">
<label>NEWS LINK</label>
<input type="text" name="alink" id="alink" class="form-control"
placeholder="PLEASE ADD LINK OF NEWS" value="{{$news->alink}}">
<p class="help-block">Example: https://play.google.com/store/apps/selfyplusure</p>
</div>
<div class="form-group">
<label>NEWS IMAGE</label>
<input type="file" name="addimage" id="addimage" value="{{$news->imagename}}">
</div>
<button type="submit" class="btn btn-default">ADD NEWS</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
NewsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\News;
use Illuminate\Http\Request;
class NewsController extends Controller
{
public function index()
{
$news = News::all();
return view('news.index', ['news' => $news]);
}
public function create()
{
return view('news.create');
}
public function store(Request $request)
{
$news=new News();
if($request->hasFile('addimage')){
$request->file('addimage');
$imagename=$request->addimage->store('public\newsimage');
$news->name = $request->input('atitle');
$news->alink = $request->input('alink');
$news->news = $request->input('news');
$news->imagename = $imagename;
$news->save();
if($news) {
return $this->index();
} }
else{
return back()->withInput()->with('error', 'Error Creating News ');
}
}
public function show(News $news)
{
//
}
public function edit(News $news)
{
$news=News::findOrFail($news->id);
return view('news.edit',['News'=>$news]);
}
public function update(Request $request, $id)
{
$news = News::findOrFail($id);
// update status as 1
$news->status = '1';
$news->save();
if ($news) {
// insert datas as new records
$newss = new News();
//On left field name in DB and on right field name in Form/view
$newss->name = $request->input('atitle');
$newss->alink = $request->input('alink');
$newss->news = $request->input('news');
$newss->imagename = $request->input('addimage');
$newss->save();
if ($newss) {
return $this->index();
}
}
}
public function destroy($id)
{
$news = News::findOrFail($id);
$news->status = '-1';
$news->save();
if ($news) {
return $this->index();
}
else{
return $this->index();
}
}
}
Link to delete and Edit
<td><input type="button" name="edit" value="EDIT">
<td><input type="button" name="delete" value="DELETE"></td>

This is the link to edit
<td><input type="button" name="edit" value="EDIT">
For delete please go through
Delete

In your controller try to use this.
return view('news.edit',compact('news'));

Related

Is there a way to handle this error when updating a record in Laravel?

So this is my web.php
<?php
use App\Http\Controllers\BookingController;
use App\Http\Controllers\BookingRoomController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\GuestController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\RoomController;
use App\Models\Customer;
use App\Models\Room;
use App\Models\Guest;
use Illuminate\Foundation\Auth\EmailVerificationRequest;
/*
|--------------------------------------------------------------------------
| 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',[
'customerCount' => Customer::count(),
'guestCount' => Guest::count(),
'roomCount' => Room::count()
]);
});
// Customer Controller
Route::get('/customers',[CustomerController::class,'index'])->name('customers.index');
Route::get('/customers/add',[CustomerController::class,'create'])->name('customers.create');
Route::post('/customers',[CustomerController::class,'store'])->name('customers.store');
Route::post('/customers/update',[CustomerController::class,'update'])->name('customers.update');
Route::get('/customers/search',[CustomerController::class,'search'])->name('customers.search');
Route::get('/customers/{id}',[CustomerController::class,'show'])->name('customers.show');
Route::delete('/customers/{id}',[CustomerController::class,'destroy'])->name('customers.destroy');
// Guest Controller
Route::get('/guests',[GuestController::class,'index'])->name('guests.index');
Route::get('/guests/add',[GuestController::class,'create'])->name('guests.create');
Route::post('/guests',[GuestController::class,'store'])->name('guests.store');
Route::post('/guests/update',[GuestController::class,'update'])->name('guests.update');
Route::get('/guests/search',[GuestController::class,'search'])->name('guests.search');
Route::get('/guests/{id}',[GuestController::class,'show'])->name('guests.show');
Route::delete('/guests/{id}',[GuestController::class,'destroy'])->name('guests.destroy');
// Rooms
Route::get('/rooms',[RoomController::class,'index'])->name('rooms.index');
Route::post('/rooms/update',[RoomController::class,'update'])->name('rooms.update');
Route::get('/rooms/{id}',[RoomController::class,'show'])->name('rooms.show');
// Bookings
Route::get('/bookings',[BookingController::class,'index'])->name('bookings.index');
Route::get('/bookings/create',[BookingController::class,'create'])->name('bookings.create');
Route::post('/bookings',[BookingController::class,'store'])->name('bookings.store');
Route::get('/bookings/{id}',[BookingController::class,'show'])->name('bookings.show');
// Booking Rooms
Route::post('/bookingrooms',[BookingRoomController::class,'store'])->name('bookingrooms.store');
// Authentication
Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');
// Route::get('/email/verify', function () {
// return view('auth.verify-email');
// })->middleware('auth')->name('verification.notice');
// Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
// $request->fulfill();
// return redirect('/home');
// })->middleware(['auth', 'signed'])->name('verification.verify');
This is my customer controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Customer;
class CustomerController extends Controller
{
public function __construct()
{
$this->middleware('auth')->except(['store','create']);
}
public function index(){
$customer = Customer::latest()->get();
return view('customers.index',['customer' => $customer]);
}
public function show($id){
$customer = Customer::findOrFail($id);
return view('customers.show',['customer' => $customer]);
}
public function create(){
return view('customers.create');
}
public function store(){
$customer = new Customer();
$customer->title = request('title');
$customer->first_name = request('firstname');
$customer->last_name = request('lastname');
$customer->date_of_birth = request('dateofbirth');
$customer->street = request('street');
$customer->town = request('town');
$customer->province = request('province');
$customer->postal_code = request('postalcode');
$customer->home_phone = request('homephone');
$customer->work_phone = request('workphone');
$customer->mobile_phone = request('mobilephone');
$customer->email = request('email');
$customer->save();
return redirect('/')->with('mssg','Added customer successfully!');
}
public function update(Request $req){
$customer = Customer::findorFail($req->id);
$customer->title = $req->title;
$customer->first_name = $req->firstname;
$customer->last_name = $req->lastname;
$customer->date_of_birth = $req->dateofbirth;
$customer->street = $req->street;
$customer->town = $req->town;
$customer->province = $req->province;
$customer->postal_code = $req->postalcode;
$customer->home_phone = $req->homephone;
$customer->work_phone = $req->workphone;
$customer->mobile_phone = $req->mobilephone;
$customer->email = $req->email;
$customer->save();
return redirect('/')->with('mssg','Updated customer successfully!');
}
public function search(Request $req){
$search = $req->get('search-customer');
$customer = Customer::where('first_name','like','%'.$search.'%')->paginate(5);
return view('customers.index',['customer' => $customer]);
// $records = ['first_name' => '%'.$search.'%',
// 'title' => '%'.$search.'%',
// 'last_name' => '%'.$search.'%'
// ];
}
public function destroy($id){
$customer = Customer::findOrFail($id);
$customer->delete();
return redirect('/')->with('mssg','Customer has been deleted!');
}
public function quantity(){
$customer = Customer::all();
return view('/',['customers' => $customer]);
}
}
This is my show.blade.php
#extends('layouts.app')
#section('content')
<div class="insert-customer-container-main">
<h1>Customer Information</h1>
<p>Please fill in the following</p>
<form class="insert-customer-container" action="{{ route('customers.update') }}" method="POST">
#csrf
{{-- get in controller to manipulate updating --}}
<input type="hidden" name="id" value="{{$customer->id}}">
<div class="customer-input-container row-one">
<div class="customer-input">
<label for="title">Title</label><br/>
<input style="width:945px" type="text" name="title" value="{{$customer->title}}" required>
</div>
</div>
<div class="customer-input-container row-two">
<div class="customer-input">
<label for="firstname">First Name</label><br/>
<input type="text" name="firstname" value="{{$customer->first_name}}">
</div>
<div class="customer-input">
<label for="lastname">Last Name</label><br/>
<input type="text" name="lastname" value="{{$customer->last_name}}">
</div>
<div class="customer-input">
<label for="dateofbirth">Date of Birth</label><br/>
<input type="date" name="dateofbirth" value="{{$customer->date_of_birth}}">
</div>
</div>
<div class="customer-input-container row-three">
<div class="customer-input">
<label for="street">Street</label><br/>
<input type="text" name="street" value="{{$customer->street}}">
</div>
<div class="customer-input">
<label for="town">Town</label><br/>
<input type="text" name="town" value="{{$customer->town}}">
</div>
<div class="customer-input">
<label for="province">Province</label><br/>
<input type="text" name="province" value="{{$customer->province}}">
</div>
</div>
<div class="customer-input-container row-four">
<div class="customer-input">
<label for="homephone">Home Phone</label><br/>
<input type="number" name="homephone" value="{{$customer->home_phone}}">
</div>
<div class="customer-input">
<label for="workphone">Work Phone</label><br/>
<input type="number" name="workphone" value="{{$customer->work_phone}}">
</div>
<div class="customer-input">
<label for="mobilephone">Mobile Phone</label><br/>
<input type="number" name="mobilephone" value="{{$customer->mobile_phone}}">
</div>
</div>
<div class="customer-input-container row-five">
<div class="customer-input">
<label for="postalcode">Postal Code</label><br/>
<input type="number" name="postalcode" value="{{$customer->postal_code}}">
</div>
<div class="customer-input">
<label for="email">Email Address</label><br/>
<input style="width:50vw;" type="email" name="email" value="{{$customer->email}}">
</div>
</div>
<input class="submit" type="submit" value="Update">
</form>
<form class="delete-form" action={{ route('customers.destroy', $customer->customer_id) }} method="POST">
#csrf
#method('DELETE')
<input type="submit" class="submit delete-btn" value="Delete">
</form>
</div>
#endsection
So my question is
When I'm editing a specific record and when I submit it, it redirects me to 404 page which is not what I'm expecting. Is there a way to fix this problem? I'm new in laravel and hoping someone could help. Thanks everyone!
May be your Route wrong
Route::get('/customers',[CustomerController::class,'index'])->name('customers.index');
Route::get('/customers/add',[CustomerController::class,'create'])->name('customers.create');
Route::post('/customers',[CustomerController::class,'store'])->name('customers.store');
Route::post('/customers/update',[CustomerController::class,'update'])->name('customers.update');
Route::get('/customers/search',[CustomerController::class,'search'])->name('customers.search');
Route::get('/customers/{id}',[CustomerController::class,'show'])->name('customers.show');
Route::delete('/customers/{id}',[CustomerController::class,'destroy'])->name('customers.destroy');
after
Route::resource('customers', 'CustomerController');
edit your all routes by resource after that try and also check your
route list
php artisan route:list
404: Not found
I think that is due to the wrong url you have passed as an action.
try to clean up your routes:
Route::group(['prefix' => 'customers', 'as' => 'customers.'], function() { Route::post('/update',[CustomerController::class,'update'])->name('customers.update'); });

how can insert data post with get route in Laravel

how do i send a form POST method with GET route in laravel?
Route
Route::get('domain_detail/{domain_name}','domain_detailController#index');
View domain_detail folder
<form method="post" action="{{url('domain_detail')}}/{{strtolower($domain_detail->domain_name)}}">
<div class="form-group">
<label for="namefamily">namefamily</label>
<input type="text" class="form-control round shadow-sm bg-white text-dark" name="namefamily">
</div>
<div class="form-group">
<label for="mobile">mobile</label>
<input type="text" class="form-control round shadow-sm bg-white text-dark" name="mobile">
</div>
<div class="form-group">
<label for="myprice">myprice</label>
<input type="number" class="form-control round shadow-sm bg-white text-dark" name="myprice">
</div>
<div class="form-group">
<input type="submit" name="send_price" class="btn btn-success" value="submit">
</div>
</form>
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class domain_detailController extends Controller
{
public function index($domain_name)
{
$domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
if ($domain_detail_exist) {
$domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();
return view('domain_detail/index', ['domain_detail' => $domain_detail]);
} else {
return view('404');
}
}
public function create()
{
return view('domain_detail.index');
}
}
At the controller i didn't put any codes in the create function, but when i click on submit button in form i get this error
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods:
GET, HEAD.
Use the index function in your domain_detailController just so return the view.
like this:
public function index($domain_name)
{
return view('domain_detail.index');
}
create a route to return the view:
Route::get('domain_detail/','domain_detailController#index');
Then use the create function to store the domain detail like this:
public function create($domain_name)
{
$domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
if ($domain_detail_exist) {
$domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();
return view('domain_detail/index', ['domain_detail' => $domain_detail]);
} else {
return view('404');
}
}
make a POST route like this:
Route::post('domain_detail/','domain_detailController#create');
Also take a look at the laravel best practices when it comes to naming conventions:
https://www.laravelbestpractices.com/

Laravel Error: The POST method is not supported for this route. Supported methods: GET, HEAD

Good evening , for school i am trying to create a simple CRUD app, using laravel 6 and mongoDB.
I can get read, update and delete working but creat fails with The POST method is not supported for this route. Supported methods: GET, HEAD.. I have searched the answers here and other sites but im stuck for 2 days now (could be something very silly but im not seeing it)
my routes are:
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/post/{_id?}', 'PostController#form')->name('post.form');
Route::post('/post/save/', 'PostController#save')->name('post.save');
Route::put('/post/update/{_id}', 'PostController#update')->name('post.update');
Route::get('/post/delete/{_id}', 'PostController#delete')->name('post.delete');
form.blade is:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Post Form</div>
<div class="card-body">
#if($data)
<form action = "{{Route ('post.update', $data->_id)}}" method="post">
#csrf
#method('PUT')
<div class="form-group">
<label for="usr">Title:</label>
<input type="text" class="form-control" name="title" value = "{{$data->title}}" >
</div>
<div class="form-group">
<label for="comment">Content:</label>
<textarea class="form-control" rows="5" name="content">{{$data->content}}</textarea>
</div>
<p align="center"> <button class="btn btn-primary">save</button></p>
</form>
#else
<form action = "{{Route ('post.form')}}" method="post">
#csrf
<div class="form-group">
<label for="usr">Title:</label>
<input type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label for="comment">Content:</label>
<textarea class="form-control" rows="5" name="content"></textarea>
</div>
<p align="center"> <button class="btn btn-primary">save</button></p>
</form>
#endif
</div>
</div>
</div>
</div>
#endsection
and my PostController is:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
//
public function form($_id = false){
if($_id){
$data = Post::findOrFail($_id);
}
$data = false;
return view ('post.form', compact('data'));
}
public function save (Request $request){
$data = new Post($request->all());
$data->save();
if($data){
return redirect()->route('home');
}else{
return back();
}
}
public function update (Request $request, $_id){
$data = post::findOrFail($_id);
$data->title = $request->title;
$data->content = $request->content;
$data->save();
/* return response()->json([
'name' => 'Abigail',
'state' => 'CA'
]); */
if($data){
return redirect()->route('home');
}else{
return back();
}
}
public function delete($_id){
$data = post::destroy($_id);
if($data) {
return redirect()->route('home');
}
else {
dd('error cannot delete this post');
}
}
}
Anybody any idea what i am missing?
Thanks in advance
You have to replace this line <form action = "{{Route ('post.form')}}" method="post"> with <form action = "{{Route ('post.save')}}" method="post">
You are using wrong route. Please change to Route ('post.save')
EDIT: I found that one myself, the PostControler didnt return a view if there was an $_id
Thanks for the help everyone!
Thanks for pointing that out, it did bring my from back to life :) However it breaks the update function :S.
When i now click on the edit button, the form does no longer get filled with the data for the post, and "save" creates a new post in stead of updating it.

Laravel 5.4 How to fix 'No query results for model'

Whenever I try to access /skills/add through an anchor in another page I get this error
The anchor that redirects to this page(with GET method) is:
<a class="btn icon-btn btn-success" href="/skills/add">
<span class="glyphicon btn-glyphicon glyphicon-plus img-circle text-success"></span>
Add
</a>
Tried using dd("test") to test it out but won't even work.
This are my routes for skills/add this:
Route::put('/skills/add', 'SkillsController#add');
Route::get('/skills/add', 'SkillsController#addIndex');
Here are my functions in SkillsController
public function addIndex() {
if (Auth::check()) {
return view('skills.add');
} else {
return redirect('/home');
}
}
public function add(Request $request) {
/*Sets validation rules for Skill object*/
$skillRules = [
'name' => 'required|max:25|regex:/[1-9a-zA-Z ]\w*/',
];
if (Skills::where('name', '=', $request->name)->count() > 0) {
return redirect('/skills')->with('message', "EXISTS");
}
$validator = Validator::validate($request->all(), $skillRules);
if ($validator == null) {
$newSkill = new Skills;
$newSkill->name = strtolower($request->name);
$newSkill->save();
return redirect('/skills')->with('message', "CREATED");
}
}
the skills.add view is this
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Edit Skill</h1>
<form method="POST" action="/skills/add">
{{method_field('PUT')}}
{{csrf_field()}}
<div class="row">
<div class="col-lg-5">
<div class="form-group">
Name:
<input name="name" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-2">
<div class="form-group">
<button type="submit" class="btn btn-primary">Add Skill</button>
<button type="button" class="btn btn-danger">Cancel</button>
</div>
</div>
</div>
</form>
</div>
#endsection
Not sure what happened, if someone in the future has this same problem, I literally just deleted and made manually thee controller, model and blade and it started to work. Not a lot of science or explain, sorry for that.
Change your route to named ones
Route::GET('/skills/add', 'SkillsController#addIndex')->name('addSkills');
Route::POST('/skills/add', 'SkillsController#add')->name('saveSkills');
and your blade to
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Edit Skill</h1>
<form method="POST" action="{{route('saveSkills')"> //** Change this
{{csrf_field()}}
<div class="row">
<div class="col-lg-5">
<div class="form-group">
Name:
<input type="text" name="name" class="form-control"> //** Change this
</div>
</div>
//Remaining form groups
</div>
<div class="row">
<div class="col-lg-2">
<div class="form-group">
<button type="submit" class="btn btn-primary">Add Skill</button>
<button type="button" class="btn btn-danger">Cancel</button>
</div>
</div>
</div>
</form>
</div>
#endsection
Change your anchor tag's href value to named route like
<a class="btn icon-btn btn-success" href="{{route('addSkills')}}">
<span class="glyphicon btn-glyphicon glyphicon-plus img-circle text-success"></span>
Add
</a>
and your controller
// SHOW ADD NEW SKILL FORM
public function addIndex() {
if (Auth::check()) {
return view('skills.add'); //view path is skills->add.blade.php
} else {
return redirect('/home');
}
}
//SAVE ADD NEW SKILL FORM DATA
public function add(Request $request) {
dd($request->name); //Check if value is getting
/*Sets validation rules for Skill object*/
$skillRules = [
'name' => 'required|max:25|regex:/[1-9a-zA-Z ]\w*/',
];
$validator = Validator::validate($request->all(), $skillRules);
if (Skills::where('name', '=', $request->name)->count() > 0) {
return redirect('/skills')->with('message', "EXISTS");
}
if ($validator == null) {
$newSkill = new Skills;
$newSkill->name = strtolower($request->name);
$newSkill->save();
return redirect('/skills')->with('message', "CREATED");
}
}
also add use App\RampUp\Skills; on top of controller
Sorry but there is no POST-route for <form method="POST" action="/skills/add">
maybe the POST is still executed.
On the other hand most Route errors could be resolved by clearing the cache (if present), rearranging the routes or put a group in between like
Route::group(['prefix' => 'skills'], function() {
Route::put('/add', 'SkillsController#add');
Route::get('/add', 'SkillsController#addIndex');
}

Using Cjax to POST data in CodeIgniter

I'm trying to POST some data using Cjax in CodeIgniter.
My view is:
<?php
require_once(FCPATH . 'ajaxfw.php');
$ajax->click('#subscribesubmit' , $ajax->form('ajax.php?subscriber/add/'));
?>
<div class="col-md-4">
<form class="form-inline subscribe-box" role="form" method="post">
<div class="form-group">
<label class="sr-only" for="subscribemail">Email address</label>
<input type="text" class="form-control" id="subscribemail" name="subscribemail" placeholder="Enter email">
</div>
<button type="submit" class="btn btn-default" id="subscribesubmit">Subscribe</button>
</form>
</div>
This view is loaded in controller index().
My subscriber controller:
class Subscriber extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('email');
$this->load->model('subscriber_model');
require_once(FCPATH.'ajaxfw.php');
}
public function add($subscribemail) {
$ajax = ajax();
//$email = $this->input->post('subscribemail');
echo $subscribemail;
$data['status'] = $this->subscriber_model->new_subscriber($subscribemail);
}
}
}
Try this:
in your code block replace to:
require_once(FCPATH . 'ajax.php');
instead of:
require_once(FCPATH . 'ajaxfw.php');
You should include ajax.php instead of ajaxfw.php (ajax.php would include itself ajaxfw.php if it needs to)

Categories