Error 404 on delete row codeigniter 2 - codeigniter

im deleting a row in coneigniter2 but it takes me to 404 page
(The page you requested was not found.)
this is my code
Controler Post
class Posts extends CI_Controller{
public function index(){
$data['title'] = 'Ultimos Post';
$data['posts'] = $this->Post_model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
...
public function delete($id){
//apagar post pelo id
$this->Post_Model->delete_post($id);
redirect('posts');
}}
Post Model
class Post_model extends Ci_Model{
...
public function delete_post($id) {
$this->db->where('id',$id);
$this->db->delete('posts');
}}
In the View.php
<?php echo form_open('/posts/delete/'.$post['id']);?>
<input type="submit" value="Delete" class="btn btn-danger" name="delete">
Routes.php
$route['posts/create'] = 'posts/create';
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts'] = 'posts/index';
$route['default_controller'] = "pages/view";
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Replace this code
Controller
class Posts extends CI_Controller{
...
public function delete(){
$id = $this->input->post('id');
$this->Post_Model->delete_post($id);
redirect('posts');
}
}
Model
class Post_model extends Ci_Model{
...
public function delete_post($id) {
$this->db->where('id',$id);
$this->db->delete('posts');
}
}
View
<?php echo form_open('/posts/delete'); ?>
<input type="hidden" name="id" value="<?php echo $post['id']; ?>" />
<input type="submit" value="Delete" class="btn btn-danger" name="delete">
</form>

After deleting your redirecting its on Posts,
Which calling the index function of Posts controller .
The problem in you Posts controller index method , Please
share that method or check it yourself .

Based on your config, posts/delete/2 route to posts/view/delete/2. Change your routes config to following code:
$route['posts/create'] = 'posts/create';
$route['posts/delete/(:num)'] = 'posts/delete/$1';
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts'] = 'posts/index';
$route['default_controller'] = "pages/view";
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Solved by doing this on the controller and Route
Controller
class Posts extends CI_Controller{
...
public function delete(){
$id = $this->uri->segment(3);
$this->Post_model->delete_post($id);
redirect('posts');
}
}
Route.php
$route['posts/delete/(:num)'] = 'posts/delete/$1'

Related

I wrote the delete method according to the Laravel documentation, but the data is not deleted

Employees.php file
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* #method static find($id)
*/
class Employees extends Model
{
use HasFactory;
protected $table = 'employees';
public $timestamps = false;
}
EmployeesController.php
<?php
namespace App\Http\Controllers;
use App\Models\Employees;
class EmployeesController extends Controller
{
public function employees_salaries()
{
return view('director.employees_salaries');
}
public function employees()
{
$employees = Employees::all();
return view('director.employees', ['employees'=>$employees]);
}
public function destroy($id)
{
$employees = Employees::find($id);
$employees->delete();
return redirect('/director.employees')->with('status', 'Your Data is Deleted');
}
}
employees.blade.php file
<from action="/delete/{{$employee->id}}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-delete btn-form me-3">
Delete
</button>
</from>
route.php file
Route::match(['get', 'post'], '/employees', array(EmployeesController::class, 'employees'))->name('employees');
Route::delete('/delete/{id}', array(EmployeesController::class, 'destroy'))->name('delete');
I cleared the cache but have no idea what the problem is. it looks like I wrote the function correctly
p.s version Laravel 9
mySQL 8
phpMyAdmin
Welcome to SO, i think youre not using the variable you assigned the value into,from controller in your blade view. maybe try to make sure you have the right variable or maybe try to use var dump.
try to put this in your controller b4 parsing value to view, to check whether you get the data you wanted or not
dd('$employees');
make sure you use the variable you assigned in your controller to your view
<?php
namespace App\Http\Controllers;
use App\Models\Employees;
class EmployeesController extends Controller
{
public function employees_salaries()
{
return view('director.employees_salaries');
}
public function employees()
{
$employees = Employees::all();
return view('director.employees', ['employees'=>$employees]); //<< here you name the variable 'employees'
}
public function destroy($id)
{
$employees = Employees::find($id);
$employees->delete();
return redirect('/director.employees')->with('status', 'Your Data is Deleted');
}
}
change this variable in view
<from action="/delete/{{$employee->id}}" method="POST"> //<< this one you use '$employee' instead of '$employees'
#csrf
#method('DELETE')
<button type="submit" class="btn btn-delete btn-form me-3">
Delete
</button>
</from>
there might also be other problem, but for now thats what i can point out.
since im also learning.

show the data in dropdown fetched from database using laravel 8

i have to show the category values in dropdown in product form,the given code is from my view,the error is undefined $categories.this is my first code in laravel i dont know how to make changings in other files.which variable is used in foreach?or i have to create new function in ProductController?
<form action="/upload_product" method="post">
#csrf
<label>Choose Categories</label>
<select name="category_id" id="category" class="category">
<option disable selected>--select category--</option>
#foreach($categories as $item)
<option value="{{ $item->id }}">{{ $item->name}}</option>
#endforeach
</select>
<input type="text" name="name" placeholder="name">
<input type="number" name="sale_price" placeholder="sale_price">
</form>
Model Product.php
class Product extends Model
{
use HasFactory;
protected $table = 'products';
public $timestamps = true;
public function category(){
return $this->belongsTo('App\Models\Category');
}
}
Model Category.php
class Category extends Model
{
use HasFactory;
public $fillable = [ 'name' ];
protected $dates = [ 'deleted_at' ];
public $timestamps = true;
public function products (){
return $this->hasMany('App\Models\Product');
}
}
ProductController.php
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\Category;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
$products = Product::all();
return view('products/index', ['products'=>$products]);
}
public function view()
{
$products = Product::with('category')->get();
$categories = Category::with('products')->get();
return view ('product.view')-> with([
'products' => $products,
'categories' => $categories,
]);
}
You have to use like below
public function index(){
$products = Product::all();
$categories = Category::with('products')->get();
return view('products.index', compact('products','categories'));
}
You don't have $categories in your index file.
Based on the index method, you're sendig just products:
public function index()
{
$products = Product::all();
return view('products/index', ['products'=>$products]);
}
So add categories too.
public function index()
{
$products = Product::get();
return view('products/index', ['products'=>$products]);
}
you can do this in three ways
public function index(){
$products = Product::all();
$categories = Category::with('products')->get();
return view('products.index', compact('products','categories'));
}
public function index(){
$products = Product::all();
$categories = Category::with('products')->get();
return view('products.index')->with(['products'=>$products,'categories'=>$categories]));
}
public function index(){
$data['products'] = Product::all();
$data['categories'] = Category::with('products')->get();
return view('products.index',$data);
}

Display reviews for a specific restaurant Laravel

I am trying to display reviews and ratings on a restaurant profile.
<div>
<h1>Reviews</h1>
#foreach($reviews as $review)
<hr>
<div class="row">
<div class="col-md-12">
#for ($i=1; $i <= 5 ; $i++)
<span class="glyphicon glyphicon-star{{ ($i <= $review->rating) ? '' : '-empty'}}"></span>
#endfor
{{ $review->user ? $review->user->name : 'Anonymous'}}
<p>{{{$review->value}}}</p>
</div>
</div>
#endforeach
</div>
Error
Undefined variable: reviews (View:
C:\xampp\htdocs\restaurantFinder\resources\views\restaurants\viewer.blade.php)
Reviews Controller
class ReviewsController extends Controller
{
public function index()
{
//
}
public function create()
{
//
}
public function store(Request $request)
{
if (!Auth::check()) {
return redirect('/index');
}
$review = new Review;
$review->user_id = auth()->user()->id;
$review->restaurant_id = $request->get('restaurant_id');
$review->value = $request->input('value');
$review->rating = $request->input('rating');
$review->save();
}
public function show($restaurant)
{
// $restaurant=Restaurant::find($id);
return view('restaurants.review', compact('restaurant'));
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
}
you are looking in the wrong place. to track the problem start with your route file. Search for the URL in the web.php file you will find the route there. ones you get the route, you will also get which controller and action are making this page render.
this issue is shown when you use a variable in a blade template but do not set it in the controller for use. In the controller, you can set this variable and you are good to go.
hope this help. incase of any issue feel free to ask.
update your ReviewsController.php
public function show($restaurantID)
{
$reviews = Reviews::find($restaurantID); // Find all reviews model by restaurantID
return view('viewer',compact('reviews ')); // This will do $reviews = reviews
// now you can foreach over reviews in your blade
}
this Link is useful

FatalErrorException in Articles.php line 22: Call to undefined method Carbon\Carbon::createFormFormat()

I am getting error on this code so I start it from blade, then controller then modle kindly give me solution why this problem happened.
blade:
#extends ('lay')
#section('content')
<h1>Write a New Article</h1>
<hr>
{!!Form::open(['url'=>'articles'])!!}
<div class="form-group">
{!!Form::label('title','Title:')!!}
{!!Form::text('title','',['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::label('body','Body:')!!}
{!!Form::textarea('body','',['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::label('published_at','Published on:')!!}
{!!Form::input('date','published_at',date('Y-m-d'),['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::submit('Add Article',['class'=>'btn btn-primary form-control','name'=>'submit'])!!}
</div>
{!!Form::close()!!}
#stop
controller: this is my controller of the program all this is made in laravel
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Articles;
use Request;
use Carbon\Carbon;
class ArticlesController extends Controller {
public function index()
{
/*$art=[
'title'=>'ashwani',
'body'=>'rathi',
'published_at'=>'Carbon\Carbon::now()'
];
Articles::create($art);*/
$articles=Articles::latest()->get();
return view('articles.index', compact('articles'));
}
public function show($id)
{
$article=Articles::findorFail($id);
return view('articles.show',compact('article'));
}
public function create(){
return view('articles.create');
}
public function store()
{
//$input=Request::all();
//$input['published_at']=Carbon::now();
//Articles::create($input);
Articles::create(Request::all());
return redirect('articles');
}
}
model: this is model where i using date and its not working
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Articles extends Model {
protected $fillable= [ 'title', 'body', 'published_at' ];
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFormFormat('Y-m-d|', $date);
}
}
It looks like this is stemming from a small typo.
You have put createFormFormat, where you have spelt Form instead of From.
You just need to correct this in your function:
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date);
}
You can use
public function getPublishedAtAttribute($date){
return Carbon::parse($date)->format('Y-m-d');
}
public function setPublishedAtAttribute($date){
$this->attributes['published_at'] = Carbon::parse($date);
}
in place of
public function setPublishedAtAttribute($date){
//$this->attributes['published_at'] = Carbon::createFormFormat('Y-m-d', $date);
return $this->attributes['published_at']->format('Y-m-d');
}

Codeigniter submit form not working, blank page

I'm attempting to submit a form with codeigniter and when i submit the form a get a blank page. Where did i go wrong?
View
echo "<form class='order_ctn_parent' method='POST' action='add/insert_orders'>";
//inputs
echo "</form>";
Controller
class Add extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index() {
}
public function insert_orders()
{
$this->load->model('order_model', 'order');
$this->order->insert_orders();
redirect('view_orders', 'location');
}
}
Model
class Order_model extends CI_Model {
public function insert_orders()
{
$DB2 = $this->load->database('orders', TRUE);
$timber_array = $this->input->post("timber_choose");
$products_array = $this->input->post("product_choose");
$qty_array = $this->input->post("quantity");
$price_array = $this->input->post("price");
$qty_array = $this->input->post("quantity");
$loop = 0;
foreach($products_array as $product) {
$price = str_replace("£","",$price_array[$loop]);
$data = array(
'product_code' => "",
'timber_type' => $timber_array[$loop],
'product' => $product,
'quantity' => $qty_array[$loop],
'price' => $price
);
$DB2->insert('timber_order_products', $data);
$loop++;
}
}
}
Try something like this
?>//end of php
<form class='order_ctn_parent' method='POST' action='<?php echo base_url()?>add/insert_orders'>
</form>
and load model in __construct(), like this
public function __construct()
{
parent::__construct();
$this->load->model('order_model', 'order');
}
and no need of check $DB2 = $this->load->database('orders', TRUE);
and Codeigniter insert should be Like This
Please check your url after submitting . i am sure your form action is not working . give proper controller and its function url into Form action . example is here .
<?php echo site_url('add/insert_orders);?>
where add will be your controller and insert_orders will be function of add controller.

Resources