ErrorException Attempt to read property "category_name" on int - laravel

To update the added categories, I encounter errors that are returned to me. I think there are functions missing or I wrote badly at the level of my controller
I got this error
ErrorException
Attempt to read property "category_name" on int
How to solve that ?
Here is my controller CategorieController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Categorie;
class CategorieController extends Controller
{
//
public function ajoutercategorie(){
return view('admin.ajoutercategorie');
}
public function sauvercategorie(Request $request){
$validatedData = $request->validate([
'category_name' => 'required | max:255',
]);
$categorie = Categorie::create($validatedData);
return redirect('/ajoutercategorie')->with('status', 'La catégorie '
.$categorie->category_name.' a été ajoutée avec succès');
}
public function categorie(){
$categories = Categorie::get();
return view('admin.categorie')->with('categories', $categories);
}
public function edit_categorie($id){
$categorie = Categorie::find($id);
return view('admin.editcategorie')->with('categorie', $categorie);
}
public function modifiercategorie(Request $request, Categorie $id){
$validatedData = $request->validate([
'category_name' => 'required | max:255',
]);
$categorie = Categorie::whereId($id)->update($validatedData);
return redirect('/categorie')->with('status', 'La catégorie'
.$categorie->category_name. 'a été modifiée avec succès');
}
}
Here my editcategorie.blade.php
#extends('layouts.appadmin')
#section('title')
Modifier la catégorie
#endsection
#section('contenu')
<div class="row grid-margin">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
<h4 class="card-title">Modifier la catégorie</h4>
#if (Session::has('status'))
<div class="alert alert-success">
{{Session::get('status')}}
</div>
#endif
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="cmxform" id="commentForm" method="POST" action="{{ route('categories.modifiercategorie') }}">
#csrf
<fieldset>
<div class="form-group">
<label for="cemail">Nom de la catégorie</label>
<input id="cemail" class="form-control" type="text" name="category_name" value="{{$categorie->category_name}}">
</div>
<input class="btn btn-primary" type="submit" value="Modifier">
</fieldset>
</form>
</div>
</div>
</div>
</div>
#endsection
#section('scripts')
{{--<script src="Administrateur/js/form-validation.js"></script>
<script src="Administrateur/js/bt-maxLength.js"></script>--}}
#endsection
My categorie.blade.php
#extends('layouts.appadmin')
#section('title')
Catégorie
#endsection
#section('contenu')
<div class="card">
<div class="card-body">
<h4 class="card-title">Catégorie</h4>
#if (Session::has('status'))
<div class="alert alert-success">
{{Session::get('status')}}
</div>
#endif
<div class="row">
<div class="col-12">
<div class="table-responsive">
<table id="order-listing" class="table">
<thead>
<tr>
<th>Order #</th>
<th>Nom de la catégorie</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach ($categories as $categorie)
<tr>
<td>{{$categorie->id}}</td>
<td>{{$categorie->category_name}}</td>
<td>
<button class="btn btn-outline-primary" onclick="window.location ='{{url('/edit_categorie/' .$categorie->id)}}'">Modifier</button>
<button class="btn btn-outline-danger">Supprimer</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
#endsection
#section('scripts')
<script src="Administrateur/js/data-table.js"></script>
#endsection
</body>
</html>
How can I solve this ?
I think the error comes from my function modifiercategorie in my CategorieController

Your problem is here $categorie = Categorie::whereId($id)->update($validatedData); because update method returns count of updated columns. You should find a category.
Try to do this
...
$categorie = Categorie::whereId($id)->first()
$categorie->update($validatedData);
...

in the method that you mentioned(modifiercategorie):
$categorie = Categorie::whereId($id)->update($validatedData);
here you are doing wrong you cannot update it's data by Validate method.
you need to put data like this :
$categorie = Categorie::whereId($id)->update(['category_name'=>$request->category_name]);

Related

Why Does Image Read Null When I Try To Delete My Category?

I am trying to find a way to delete my category that I have created. I have the image stored in my public folder and I get the error attempt to read image is null, I think the error has to do with category_id being null. How can I retrieve it for my category object?
//Controller class
class Index extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $category_id;
//setting category_id
public function deleteCategory($category_id)
{
$this->category_id = $category_id;
}
public function destroyCategory() {
$category = Category::find($this->category_id);
// dd($this);
$path = 'uploads/category/'.$category->image;
if (File::exists($path)) {
File::delete($path);
}
$category->delete();
session()->flash('message', 'Category Deleted');
$this->dispatchBrowserEvent('close-modal');
}
public function render()
{
$categories = Category::orderBy('id', 'DESC')->paginate(10);
return view('livewire.admin.category.index', ['categories' => $categories]);
}
}
this is my component blades file that accompanies it.
//my blade file
<div>
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Category Delete</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form wire:submit.prevent="destroyCategory">
<div class="modal-body">
<h6>Are you sure you want to delete this data?</h6>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Yes. Delete</button>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
#if(session('message'))
<div class="alert alert-success">{{session('message')}}</div>
#endif
<div class="card-header">
<h4>
Category
Add Category
</h4>
</div>
<div class="card-body">
{{-- display all categories using livewire --}}
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach ($categories as $category)
<tr>
<td>{{$category->id}}</td>
<td>{{$category->name}}</td>
<td>{{$category->status == '1' ? "Hidden" : "Visible"}}</td>
<td>
Edit
<a href="#" wire:click="deleteCategory({{$category->id}})" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal">
Delete
</a>
</tr>
#endforeach
</tbody>
</table>
<div>
{{$categories->links()}}
</div>
</div>
</div>
</div>
</div>
#push('script')
<script>
window.addEventListener('close-modal', event => {
$('#deleteModal').modal('hide');
})
</script>
#endpush
I wanted to add my sugestion as a comment, however is to long and it doesn't look good when i add the code, so i need to add a post.
I see that you are using deleteCategory() to get category_id and set it on a public varaible public $category_id;, when you get the variable then you try to delete the category with destroyCategory().
If that is true you don't need to create 2 functions for that,
so instead try and use this:
public function deleteCategory($category_id) {
$category = Category::find($category_id);
$path = 'uploads/category/'.$category->image;
// dd($path); <- try this on the debbuger, to see the path
if (File::exists($path)) {
File::delete($path);
}
$category->delete();
session()->flash('message', 'Category Deleted');
$this->dispatchBrowserEvent('close-modal');
}
Just use the function deleteCategory with the code of destroyCategory, and let the paramenter $category_id, because deleteCategory gets the id from the view
wire:click="deleteCategory({{$category->id}})"

Missing required parameter for [Route: BatterFirst.update] [URI: BatterFirst/{BatterFirst}] [Missing parameter: BatterFirst]. edit.blade.php)

While I am doing laravel CRUD project I got . I stuck here its 2 days
Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameter for [Route: BatterFirst.update] [URI: BatterFirst/{BatterFirst}] [Missing parameter: BatterFirst]. (View: C:\xampp\htdocs\CricBangla\resources\views\BatterFirst\edit.blade.php) Error.
I can`t find whire is the error.
Here is my web.php
Route::resource('BatterFirst', BatterFirstController::class);
This is my model BatterFirst.php
class BatterFirst extends Model
{
use HasFactory;
protected $table = 'batterfirst';
protected $fillable = [
'name', 'runs', 'balls', 'sixs', 'fours'
];
}
This is my BatterFirstController.php
<?php
namespace App\Http\Controllers;
use App\Models\BatterFirst;
use Illuminate\Http\Request;
class BatterFirstController extends Controller
{
public function index()
{
$data = BatterFirst::latest()->paginate(5);
return view('BatterFirst.index',compact('data'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
public function create()
{
return view('BatterFirst.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'runs' => 'required',
'balls' => 'required',
'sixs' => 'required',
'fours' => 'required',
]);
BatterFirst::create($request->all());
return redirect()->route('BatterFirst.index')
->with('success','Batter created successfully.');
}
public function show(BatterFirst $batterFirst)
{
return view('BatterFirst.show',compact('batterFirst'));
}
public function edit(BatterFirst $batterFirst)
{
return view('BatterFirst.edit',compact('batterFirst'));
}
public function update(Request $request, BatterFirst $batterFirst)
{
$request->validate([
'name' => 'required',
'runs' => 'required',
'balls' => 'required',
'sixs' => 'required',
'fours' => 'required',
]);
$batterFirst->update($request->all());
return redirect()->route('BatterFirst.index')
->with('success','Batter updated successfully');
}
public function destroy(BatterFirst $batterFirst)
{
$batterFirst->delete();
return redirect()->route('BatterFirst.index')
->with('success','Batter deleted successfully');
}
}
This is my edit.blade.php
#extends('BatterFirst.layout')
#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('BatterFirst.index') }}"> Back</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('BatterFirst.update',$batterFirst->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="{{ $batterFirst->name }}" class="form-control" placeholder="name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Runs:</strong>
<input type="number" name="runs" value="{{ $batterFirst->runs }}" class="form-control" placeholder="runs"> </div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Balls:</strong>
<input type="number" name="balls" value="{{ $batterFirst->balls }}" class="form-control" placeholder="balls"> </div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Sixs:</strong>
<input type="number" name="sixs" value="{{ $batterFirst->runs }}" class="form-control" placeholder="sixs"> </div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Fours:</strong>
<input type="number" name="fours" value="{{ $batterFirst->fours }}" class="form-control" placeholder="fours"> </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>
#endsection
In this image when i click delete button it showing Batter delete successfully but not deleting Batter
This is my index.blade.php
#extends('BatterFirst.layout')
#section('content')
<div class="row" style="margin-top: 5rem;">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 8 CRUD Example from scratch - laravelcode.com</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('BatterFirst.create') }}"> Create New Post</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>Runs</th>
<th>Balls</th>
<th>Sixs</th>
<th>Fours</th>
<th>Strick Rate</th>
</tr>
#foreach ($data as $key => $value)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->runs }}</td>
<td>{{ $value->balls }}</td>
<td>{{ $value->sixs }}</td>
<td>{{ $value->fours }}</td>
{{-- <td>{{ $value->runs/$value->balls*100 }}</td> --}}
<td>#if ($value->runs > 0 and $value->runs ==0)
{{ $value->runs*100 }}
#elseif ($value->balls>0 and $value->runs ==0)
{{ $value->balls*$value->runs }}
#elseif ($value->balls==0 and $value->runs ==0)
{{ $value->balls * $value->runs }}
#elseif ($value->runs>0 and $value->balls>=0)
{{ $value->runs/$value->balls*100 }}
#endif
</td>
<td>
<form action="{{ route('BatterFirst.destroy',$value->id) }}" method="POST">
<a class="btn btn-info" href="{{ route('BatterFirst.show',$value->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('BatterFirst.edit',$value->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
{!! $data->links() !!}
#endsection
Note: I just started learning laravel. Thanks
Try to delete the method POST. Update will use PUT as Method.
<form action="{{ route('BatterFirst.update',$batterFirst->id) }}" method="POST">
#csrf
#method('PUT')
and then change inside your route function this. Pass the id inside a array.
"{{route('BatterFirst.update',['id' => $batterFirst->id])}}"
then will ĺooking so:
<form action="{{ route('BatterFirst.update',['id' => $batterFirst->id]) }}" method="PUT">
#csrf
#method('PUT')
It happens CZ of wrong naming of table and model, folders

Base table or view not found: 1146 Table 'cricbangla.batter_firsts' doesn't exist (SQL: select count(*) as aggregate from `batter_firsts`)-Laravel-8

This is my database name in .env
DB_DATABASE=cricbangla
This is a screenshot of my database and all table
This is The Image of my database
You can see there is no table called batter_firsts and i don't want any table with this name
I am doing a CRUD project in laravel-8 where i create a table name batterfirst.php
Schema::create('batterfirst', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('runs');
$table->integer('balls');
$table->integer('sixs');
$table->integer('fours');
$table->timestamps();
});
Where my web.php is
<?php
use App\Http\Controllers\BatterFirstController;
Route::get('/', function () {
return view('welcome');
});
Route::resource('BatterFirst', BatterFirstController::class);
Where my model name is BatterFirst.php which is
class BatterFirst extends Model
{
use HasFactory;
protected $fillable = [
'name', 'runs', 'balls', 'sixs', 'fours'
];
}
And My Controller is BatterFirstController.php which is
<?php
namespace App\Http\Controllers;
use App\Models\BatterFirst;
use Illuminate\Http\Request;
class BatterFirstController extends Controller
{
public function index()
{
$data = BatterFirst::latest()->paginate(5);
return view('BatterFirst.index',compact('data'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
public function create()
{
return view('BatterFirst.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'runs' => 'required',
'balls' => 'required',
'sixs' => 'required',
'fours' => 'required',
]);
BatterFirst::create($request->all());
return redirect()->route('BatterFirst.index')
->with('success','Batter created successfully.');
}
public function show(BatterFirst $batterFirst)
{
return view('BatterFirst.show',compact('batterfirst'));
}
public function edit(BatterFirst $batterFirst)
{
return view('BatterFirst.edit',compact('batterfirst'));
}
public function update(Request $request, BatterFirst $batterFirst)
{
$request->validate([
'name' => 'required',
'runs' => 'required',
'balls' => 'required',
'sixs' => 'required',
'fours' => 'required',
]);
$batterFirst->update($request->all());
return redirect()->route('BatterFirst.index')
->with('success','Batter updated successfully');
}
public function destroy(BatterFirst $batterFirst)
{
$batterFirst->delete();
return redirect()->route('BatterFirst.index')
->with('success','Batter deleted successfully');
}
}
This is my index.php file inside BatterFirst Folder BatterFirst/index.blade.php
#extends('BatterFirst.layout')
#section('content')
<div class="row" style="margin-top: 5rem;">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 8 CRUD Example from scratch - laravelcode.com</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('BatterFirst.create') }}"> Create New Post</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>Runs</th>
<th>Balls</th>
<th>Sixs</th>
<th>Fours</th>
<th>Strick Rate</th>
</tr>
#foreach ($data as $key => $value)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->runs }}</td>
<td>{{ $value->overs }}</td>
<td>{{ $value->balls }}</td>
<td>{{ $value->sixs }}</td>
<td>{{ $value->fours }}</td>
{{-- <td>{{ $value->runs/$value->balls*100 }}</td> --}}
<td>#if ($value->runs > 0 and $value->runs ==0)
{{ $value->runs*100 }}
#elseif ($value->balls>0 and $value->runs ==0)
{{ $value->balls*$value->runs }}
#elseif ($value->balls==0 and $value->runs ==0)
{{ $value->balls * $value->runs }}
#elseif ($value->runs>0 and $value->balls>=0)
{{ $value->runs/$value->balls*100 }}
#endif
</td>
<td>
<form action="{{ route('BatterFirst.destroy',$value->id) }}" method="POST">
<a class="btn btn-info" href="{{ route('BatterFirst.show',$value->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('BatterFirst.edit',$value->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
{!! $data->links() !!}
#endsection
This is my BatterFirst/create.blade.php file
#extends('BatterFirst.layout')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('BatterFirst.index') }}"> Back</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('BatterFirst.store') }}" 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" class="form-control" placeholder="Enter Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Runs:</strong>
<input type="number" name="runs" class="form-control" placeholder="Enter Runs">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Balls:</strong>
<input type="number" name="balls" class="form-control" placeholder="Enter Balls">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Sixs:</strong>
<input type="number" name="sixs" class="form-control" placeholder="Enter Sixs">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Fours:</strong>
<input type="number" name="fours" class="form-control" placeholder="Enter Fours">
</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>
#endsection
I don`t know why this problem is showing.
Note: I just started to learn laravel
Its looking for the correct named table, since you have it in camel it should be batter_first you can change this via the model below;
class BatterFirst extends Model
{
use HasFactory;
protected $table = 'batterfirst';
protected $fillable = [
'name', 'runs', 'balls', 'sixs', 'fours'
];
}

Undefined variable: title (View: C:\xampp\htdocs\myproject\resources\views\categories\index.blade.php)

I try to make a page index.blade, but i getting error
Undefined variable: title (View: C:\xampp\htdocs\myproject\resources\views\categories\index.blade.php)
I am using laravel 5.4 PHP 7.0.33
Is there anything wrong with the code?
My Controller
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$categories = Category::orderBy('created_at', 'DESC')->paginate(10);
return view('categories.index', compact('categories'));//
}
My index.blade
this is my view/categories/index.blade.php
#extends('layout.master')
​
#section('title')
<title>Manajemen Kategori</title>
#endsection
​
#section('content')
<div class="content-wrapper">
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0 text-dark">Manajemen Kategori</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item">Home</li>
<li class="breadcrumb-item active">Kategori</li>
</ol>
</div>
</div>
</div>
</div>
​
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
#card
#slot('title')
<div class="card">
<div class="card-header with-border">
<h3 class="card-title">{{ $title }}</h3>
</div>
<div class="card-body">
{{ $slot }}
</div>
{{ $footer }}
</div>
#endslot
#if (session('error'))
#alert
<div class="alert alert-{{ $type }} alert-dismissible">
{{ $slot }}
</div>
#endalert
#endif
​
<form role="form" action="{{ route('kategori.store') }}" method="POST">
#csrf
<div class="form-group">
<label for="name">Kategori</label>
<input type="text"
name="name"
class="form-control {{ $errors->has('name') ? 'is-invalid':'' }}" id="name" required>
</div>
<div class="form-group">
<label for="description">Deskripsi</label>
<textarea name="description" id="description" cols="5" rows="5" class="form-control {{ $errors->has('description') ? 'is-invalid':'' }}"></textarea>
</div>
#slot('footer')
<div class="card-footer">
<button class="btn btn-primary">Simpan</button>
</div>
</form>
#endslot
#endcard
</div>
<div class="col-md-8">
#card
#slot('title')
List Kategori
#endslot
#if (session('success'))
#alert(['type' => 'success'])
{!! session('success') !!}
#endalert
#endif
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<td>#</td>
<td>Kategori</td>
<td>Deskripsi</td>
<td>Aksi</td>
</tr>
</thead>
<tbody>
#php $no = 1; #endphp
#forelse ($categories as $row)
<tr>
<td>{{ $no++ }}</td>
<td>{{ $row->name }}</td>
<td>{{ $row->description }}</td>
<td>
<form action="{{ route('kategori.destroy', $row->id) }}" method="POST">
#csrf
<input type="hidden" name="_method" value="DELETE">
<i class="fa fa-edit"></i>
<button class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></button>
</form>
</td>
</tr>
#empty
<tr>
<td colspan="4" class="text-center">Tidak ada data</td>
</tr>
#endforelse
</tbody>
</table>
</div>
#slot('footer')
​
#endslot
#endcard
</div>
</div>
</div>
</section>
</div>
#endsection
My route web.php
Route::resource('/kategori', 'CategoryController',
['except' => ['create', 'show']]);
It should be like this
public function index()
{
$categories = Category::orderBy('created_at', 'DESC')->paginate(10);
$title = ''; //your title
return view('categories.index', compact('categories','title'));
}
because title not getting value from controller.
You dont pass variable title to view
Add some like this:
$title = 'Your title';
return view('categories.index', compact('categories','title'));
If title is a field of Category
If title is a field/member of Category model, then you would do {{ $category->title }}. I think this is the real error.
Otherwise
You need to define and send the variable as other have said.
$tile='Your Title';
return view('categories.index', compact('categories','title'));

Unable to show data from database in Laravel after inserting data into database

I was able to insert data into database in Laravel but when I was trying to show the data in a tabular form I couldn't - it was displaying Route [stock_edit] not defined. (View: C:\wamp\www\pump\core\resources\views\dashboard\stock-show.blade.php)
Like I said in my quetion yesterday, I am new to Laravel and I am yet to understand the environment. I have been on this for the past 48 hours searching for help online but couldn't find a satisfactory ones
Here is my stock-show.blade.php
#extends('layouts.dashboard')
#section('title', 'All Stock')
#section('content')
#if(count($stock))
<div class="row">
<div class="col-md-12">
<div class="portlet light bordered">
<div class="portlet-title">
<div class="caption font-dark">
</div>
<div class="tools"> </div>
</div>
<div class="portlet-body">
<table class="table table-striped table-bordered table-hover" id="sample_1">
<thead>
<tr>
<th>ID#</th>
<th>Product Name</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($stock as $p)
<tr>
<td>{{ $p->id }}</td>
<td>{{ $p->name }}</td>
<td>{{ $p->price }} </td>
<td>
<i class="fa fa-edit"></i> EDIT
<button type="button" class="btn btn-danger btn-sm delete_button"
data-toggle="modal" data-target="#DelModal"
data-id="{{ $p->id }}">
<i class='fa fa-times'></i> DELETE
</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div><!-- ROW-->
<div class="text-center">
{!! $stock->render() !!}
</div>
#else
<div class="text-center">
<h3>No Data available</h3>
</div>
#endif
<!-- Modal for DELETE -->
<div class="modal fade" id="DelModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel"> <i class='fa fa-trash'></i> Delete !</h4>
</div>
<div class="modal-body">
<strong>Are you sure you want to Delete ?</strong>
</div>
<div class="modal-footer">
<form method="post" action="{{ route('stock_delete') }}" class="form-inline">
{!! csrf_field() !!}
{{ method_field('DELETE') }}
<input type="hidden" name="id" class="abir_id" value="0">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">DELETE</button>
</form>
</div>
</div>
</div>
</div>
#endsection
#section('scripts')
<script>
$(document).ready(function () {
$(document).on("click", '.delete_button', function (e) {
var id = $(this).data('id');
$(".abir_id").val(id);
});
});
</script>
#endsection
and this is the DashboardController.php
//Stocks
public function createStock()
{
$data['site_title'] = $this->site_title;
$data['page_title'] = "Create Stock";
//$data['currency'] = Currency::all();
return view('dashboard.stock-create',$data);
}
public function storeStock(Request $request)
{
$this->validate($request,[
'name' => 'required|unique:stocks,name',
'price' => 'required',
//'currency_id' => 'required'
]);
try {
$stock = Input::except('_method','_token');
Stock::create($stock);
session()->flash('message', 'Stock Create Successfully.');
Session::flash('type', 'success');
return redirect()->back();
} catch (\PDOException $e) {
session()->flash('message', 'Some Problem Occurs, Please Try Again!');
Session::flash('type', 'danger');
return redirect()->back();
}
}
public function showStock()
{
$data['site_title'] = $this->site_title;
$data['page_title'] = "All Stock";
$data['stock'] = Stock::orderBy('id','ASC')->paginate(100);
return view('dashboard.stock-show',$data);
}
public function editStock($id)
{
$data['stock'] = Stock::findOrFail($id);
$data['site_title'] = $this->site_title;
$data['page_title'] = 'Edit Product';
$data['stock'] = Stock::all();
return view('dashboard.stock-edit',$data);
}
public function updateStock(Request $request,$id)
{
$stocks = Stock::findOrFail($id);
$this->validate($request,[
'name' => 'required|unique:stocks,name,'.$stocks->id,
'price' => 'required',
//'currency_id' => 'required',
]);
try {
$stock = Input::except('_method','_token');
$stocks->fill($stock)->save();
session()->flash('message', 'Stock Updated Successfully.');
Session::flash('type', 'success');
return redirect()->back();
} catch (\PDOException $e) {
session()->flash('message', 'Some Problem Occurs, Please Try Again!');
Session::flash('type', 'danger');
return redirect()->back();
}
}//Stocks End
And lastly the route.php
/* Stock Route List */
Route::get('stock-create',['as'=>'stock-create','uses'=>'DashboardController#createStock']);
Route::post('stock-create',['as'=>'stock-store','uses'=>'DashboardController#storeStock']);
Route::get('stock-show',['as'=>'stock-show','uses'=>'DashboardController#showStock']);
Route::get('stock-edit/{id}',['as'=>'stock-edit','uses'=>'DashboardController#editStock']);
Route::put('stock-edit/{id}',['as'=>'stock-update','uses'=>'DashboardController#updateStock']);
You have a typo in your code,
change stock_edit to stock-edit in your view

Resources