Trying to get property of a non-object problem in laravel - php

I am working on a laravel project. In this project, if a non-user tries to edit a post then it will redirect to the posts page without letting the user make any edit. But I am getting this error
Trying to get property of non-object (View: C:\xampp\htdocs\lsapp\resources\views\posts\show.blade.php).
ErrorException (E_ERROR) Trying to get property of non-object (View: C:\xampp\htdocs\lsapp\resources\views\posts\show.blade.php) Previous exceptions and may be it might help Trying to get property of non-object (0) ErrorException {#221 ▼ #message: "Trying to get property of non-object" #code: 0 #file: "C:\xampp\htdocs\lsapp\storage\framework\views\b4b78b7a31531d4e8ddf98112cc09d54ba62ed99.php" #line: 3 #severity: E_NOTICE }
Here is the edit code:
public function edit($id)
{
$post = Post::find($id);
if(auth()->user()->id !== $post->user_id){
return redirect ('posts')->with('error','Unauthorized page');
}
return view('posts.edit')->with('post',$post);
}
PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use DB;
class PostsController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth',['except'=>['index','show']]);
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//$posts=Post::all();
//$posts=Post::orderBy('title','desc')->get();
//return Post::where('title','post two')->get();
//$posts=DB::select("SELECT * FROM posts");
//$posts= Post::orderBy('title','desc')->take(1)->get();
$posts= Post::orderBy('created_at','desc')->paginate(1);
return view('posts.index')->with('posts',$posts);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'title'=>'required',
'body'=>'required'
]);
//Create posts
$post=new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->user_id=auth()->user()->id;
$post->save();
return redirect('posts')->with('success','Post Created');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$post = Post::find($id);
return view('posts.show')->with('post',$post);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$post = Post::find($id);
if(auth()->user()->id !== $post->user_id){
return redirect ('posts')->with('error','Unauthorized page');
}
return view('posts.edit')->with('post',$post);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request,[
'title'=>'required',
'body'=>'required'
]);
//Create posts
$post=Post::find($id);
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('posts')->with('success','Post Updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post=Post::find($id);
$post->delete();
return redirect('posts')->with('success','Post Deleted');
}
}
show.blade.php:
#extends('layouts.app')
#section('content')
Back
<h1>{{$post->title}}</h1>
<div>
{{$post->body}}
</div>
<hr>
#if(!Auth::guest())
#if(Auth::user()->id==$post->user_id)
<small>Witten on {{$post->created_at}} by {{$post->user->name}}</small>
<hr>
Edit
{!!Form::open(['action'=>['PostsController#destroy',$post->id],'method'=>'POST', 'class'=>'pull-right'])!!}
{{Form::hidden('_method','DELETE')}}
{{Form::submit('Delete',['class'=>'btn btn-danger'])}}
{!!Form::close()!!}
#endif
#endif
#endsection
here is my route:
Route::get('/index', 'PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/services', 'PagesController#services');
Route::resource('/posts','PostsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
Here is user.php in case it helps
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function posts(){
return $this->hasMany('App\Post');
}
}

Look in your blade file. Whenever you all $post->user->SOMEPROPERTIES, change it to $post->user['SOMEPROPERTIES']
Example:
<small>Witten on {{$post->created_at}} by {{$post->user['name']}}</small>
I hope it will work for you. (Working in my case)

Look in your blade file.
Whenever you all $post->user->SOMEPROPERTIES, change it to $post->user()
like here:
<small>Witten on {{$post->created_at}} by {{$post->user()->name}}</small>

check your href value. Check the curly braces
Wrong code:
#foreach($posts as $post)
the text of the link
#endforeach
Working code:
#foreach($posts as $post)
the text of the link
#endforeach
I hope this helps in some way.

Related

Laravel foreach variable not working ($tickets is undefined)

Hello When I run page it tells me that:
ErrorException
Undefined variable: tickets (View: D:\xampp\htdocs\new-helpdesk\resources\views\tickets\index.blade.php)
http://localhost:8000/tickets
Hide solutions
$tickets is undefined
when I dd it works.
index.blade.php
#extends('layouts.ticket')
#section('content')
#if ($tickets)
**
**#foreach ($tickets as $ticket)
<div class="form-group">
{!! Form::label('company', 'Company:', ['class' => 'form-label fs-6 d-inline-block']) !!}
{!! Form::select('company', [' '=> 'Choose Company'], null,['class'=>'form-control']);!!}
</div>
#endforeach
#endif
#endsection
TicketsController
<?php
namespace App\Http\Controllers;
use App\Models\Companies;
use App\Models\Tickets;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class TicketsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
// $tickets = Tickets::with('companies')->get();
return view('tickets.index');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
// return view('tickets.create');
$tickets = Companies::all();
dd($tickets->toArray());
// return view('tickets.index', compact('tickets'))->with('tickets', $tickets);
return view('tickets.index')->with(['tickets' => $tickets]);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$request->validate([
'ticket_title'=> 'required',
'description'=>'required'
]);
$query = DB::table('tickets')->insert([
'ticket_title'=>$request->input('ticket_title'),
'description'=>$request->input('description'),
]);
if($query){
return back()->with('success','Data have beeen inserted');
} else {
return back()->with('fail', 'Data not inserted');
}
}
/**
* Display the specified resource.
*
* #param \App\Models\Tickets $tickets
* #return \Illuminate\Http\Response
*/
public function show(Tickets $tickets)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Tickets $tickets
* #return \Illuminate\Http\Response
*/
public function edit(Tickets $tickets)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Tickets $tickets
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Tickets $tickets)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Tickets $tickets
* #return \Illuminate\Http\Response
*/
public function destroy(Tickets $tickets)
{
//
}
public function showcategory(){
// $companies = Companies::pluck('name', 'id');
// dd($companies);
}
}
web.php
Route::resource('/tickets', TicketsController::class);
Route::resource('/companies', CompaniesController::class);
Tickets Model
public function companys(){
return $this->belongsTo(Companies::class);
}
Companies Model
public function tickets(){
return $this->HasMany(Tickets::class);
}
How can I solve this problem?
I try to change it as layout
You have a little chaos in your return value, you either pass the collection in compact() or in with(). with() needs an array, so you will have to do one of the following:
return view('tickets.index', compact('tickets'));
//or
return view('tickets.index')->with(['tickets' => $tickets]);
=====EDIT
The /tickets route is calling your index() action. https://laravel.com/docs/9.x/controllers#actions-handled-by-resource-controller This action also returns the index.blade.php view, but you are not passing the tickets collection.
public function index()
{
$tickets = Tickets::with('companies')->get();
return view('tickets.index')->with(['tickets' => $tickets]);
}
As TimLewis pointed out, the #if($tickets) in your index.blade.php has no purpose and can be removed.

How to render an uploaded image inside a view?

I am trying to get an image to upload and render when asked too.
I have successfully got the image upload working on my localhost, but once I push the code to the forge server, it breaks.
The image is uploaded however this is what I see once the post is created... See below.
I am not sure how to fix this issue.
Does anyone have any advice or suggestions on how to address this issue?
What I am doing wrong?
PostConroller
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;
class PostController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::all();
return view('post.index', compact('posts'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$post = new Post(); // What is this?
return view('post.create', compact ('post'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'body' => 'required',
]);
$post = Post::create([
'user_id' => Auth::id(),
'title' => $request->title,
'body' => $request->body
]);
$this->storeImage($post);
return redirect('/posts');
}
/**
* Display the specified resource.
*
* #param \App\Post $post
* #return \Illuminate\Http\Response
*/
public function show(Post $post)
{
return view('post.show', compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Post $post
* #return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
return view('post.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Post $post
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
$this->validatedRequest();
$post->update($request->except('image'));
$this->storeImage($post);
return redirect('/posts');
}
/**
* Remove the specified resource from storage.
*
* #param \App\Post $post
* #return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
$post->delete();
return redirect('/posts');
}
private function validatedRequest()
{
return request()->validate([
'title' => 'required',
'body' => 'required',
'image' => 'sometimes|file|image|max:5000',
]);
}
private function storeImage($post)
{
if (request()->has('image')) {
$filename = Str::random(14).'.'.request()->image->getClientOriginalExtension();
$image = Image::make(request()->image)->fit(300, 300, null, 'top-left')->encode();
Storage::disk('public')->put("uploads/{$filename}", (string) $image);
$post->update([
'image' => "uploads/{$filename}"
]);
}
}
}
Form (show.blade.php)
#if($post->image)
<div class="row">
<div class="col-12">
<img src="{{ asset('storage/uploads/' . $post->image) }}" alt=""
class="img-thumbnail">
</div>
</div>
#endif
Files uploaded and stored via Storage can be accessed with the same Storage class. Try
<img src="{{ Storage::disk('public')->url('/uploads/' . $post->image) }}" alt=""
class="img-thumbnail">
asset() is used for accessing files in /public

Laravel: Trouble with update db after click button

I have problem: I build an app and I have 2 tables in my db: habits and users. The User has a field called points and after click a button 'Add point' I want to increment that value. I added a method in HabitsController called addPoints and button in a view, but I get an error message: MethodNotAllowedHttpException No message. There is my code.
HabitsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Habit;
use App\User;
use DB;
use App\Quotation;
class HabitsController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//store in $habits all posts of current user
$habits = DB::table('habits')->where('user_id', auth()->id())->get();
return view('habits.index')->with('habits', $habits);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('habits.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name'=>'required',
'description'=>'required',
'difficulty'=>'required'
]);
$habit = new Habit;
$habit->name = $request->input('name');
$habit->description = $request->input('description');
$habit->difficulty = $request->input('difficulty');
$habit->NumberOfCompleted = 0;
$habit->user_id = auth()->user()->id;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$single = Habit::find($id);
return view('habits.show')->with('single', $single);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$single = Habit::find($id);
return view('habits.edit')->with('single', $single);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'name'=>'required',
'description'=>'required',
'difficulty'=>'required'
]);
$habit = Habit::find($id);
$habit->name = $request->input('name');
$habit->description = $request->input('description');
$habit->difficulty = $request->input('difficulty');
$habit->NumberOfCompleted = 0;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$single = Habit::find($id);
$single->delete();
return redirect('/habits')->with('success', 'Habit deleted');
}
public function addPoint(Request $request, $id)
{
$habit = User::find($id);
$habit->points += 1;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
}
show.blade.php
#extends('layouts/app')
#section('content')
<div class="row single">
<div class="col-sm-8 col-sm-offset-2 showSingleHabit">
<h2>{{$single->name}}</h2>
<p>{{$single->description}}</p>
<p>{{$single->difficulty}}</p>
<p>{{$single->NumberOfCompleted}}</p>
Edit
{!!Form::open(['action' => ['HabitsController#destroy', $single->id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
{!!Form::open(['action' => ['HabitsController#update', $single->id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'POST')}}
{{Form::submit('Add point', ['class' => 'btn btn-primary'])}}
{!!Form::close()!!}
</div>
</div>
#endsection
routes/web.php
Route::get('/', 'PagesController#index');
Route::get('/about', 'PagesController#about');
Route::resource('habits', 'HabitsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
I will be grateful for any help. :)
Change
{{Form::hidden('_method', 'POST')}}
To
{{Form::hidden('_method', 'PUT')}}

Databases not linking using hasMany in laravel

I am trying to use data from two databases in laravel. Edit: I have added the products controller to the bottom of this post
This is how I test if they are linked:
<p>TEST :{{ $products->ticket->created_at}}</p>
error message:
Undefined variable: products (View: /Applications/MAMP/htdocs/lsapp/resources/views/products/create.blade.php)
Product.php (App)
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
//Table NAME
protected $table = 'products';
//PRIMARY KEY
public $primaryKey = 'id';
//Timestamps
public $timestamps =true;
public function user(){
return $this->belongsTo('App\User');
}
public function products()
{
return $this->hasMany(App\product::class);
}
}
Ticket.php (app)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class product extends Model
{
public function Product(){
return $this->belongsTo(App\Product::class);
}
}
Product Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\product;
use App\Ticket;
class productController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth',['except' => ['index','show']]);
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$products = product::orderBy('created_at','desc')->paginate(10);
//$products = product::where('type','major')->get();
return view('products.index')->with('products',$products);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('products.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'title' => 'required',
]);
//create product
$product = new product;
$product->title = $request->input('title');
$product->venue = $request->input('venue');
$product->city = $request->input('city');
$product->country = $request->input('country');
$product->description = $request->input('description');
$product->date = $request->input('date');
$product->user_id = auth()->user()->id;
$product->save();
return redirect('/products')->with('success','product Created');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$product = product::find($id);
return view('products.show')->with('product',$product);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$product = product::find($id);
if(auth()->user()->id !==$product->user_id){
return redirect('/products')->with('error','unauthorised page');
}
return view('products.edit')->with('product',$product);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'title' => 'required'
]);
$product = product::find($id);
$product->title = $request->input('title');
$product->venue = $request->input('venue');
$product->city = $request->input('city');
$product->country = $request->input('country');
$product->description = $request->input('description');
$product->date = $request->input('date');
$product->user_id = auth()->user()->id;
$product->save();
return redirect('/products')->with('success','product updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$product = product::find($id);
if(auth()->user()->id !==$product->user_id){
return redirect('/products')->with('error','unauthorised page');
}
$product->delete();
return redirect('/products')->with('success','product deleted');
}
}
There are a few problems with your code.
<p>TEST :{{ $products->ticket->created_at}}</p> will never work.
Assuming you are testing in your index page, you need to loop over each product in order to see it's relationship with a ticket.
Do all of your products have a single ticket? If so, then you can just define the single ticket in a config variable. It does not make sense to define a many-to-one relationship in a database.
In your Product model, you have not defined a relationship to the Ticket model. You only have a relationship defined in the Ticket model to the Product model. This means that you can access a product from the ticket, but not the other way around. You need to explicitly define a one-on-one relationship from Product to Ticket in order to use it.
You have defined this function within the Product model.
public function products() {
return $this->hasMany(App\product::class);
}
Why? Do products themselves have many products? I think you may benefit from learning about eloquent relationships. Laracasts is a good resource for learning Laravel from scratch.
Also see Laravel official Eloquent documentation

delete method not allowed and returning MethodNotAllowedHttpException in Laravel 5

So I can create a new record on my database with form, but somehow i fail to delete my database using form And right now, I'm using laravel 5.
So this is my code looks like
routes.php
Route::get('/', [
'as' => '/',
'uses' => 'PagesController#getIndex'
]);
Route::resource('product', 'ProductController');
ProductController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use App\Product;
use resources\views\products;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$product = DB::select('select * from feedback');
return view('products.index')
->with('product',$product);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('products.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
/*$rating = new Product;
$rating->Name= $request->name;
$rating->avg=$request->price;
$rating->save();*/
$inputs= $request->all();
$product= Product::create($inputs);
//return redirect()->route('product.index');
return redirect()->action('ProductController#index');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$product= Product::where('idoveralRating',$id)->first();
//return $product;
return view('products.show')
->with('product',$product);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//echo '<script>console.log("bitch")</script>';
//Product::destroy($id);
$product= Product::where('idoveralRating',$id)
->delete();
return redirect()->route('product.show');
}
}
show.blade.php
#extends('layouts.layout')
#section('Head')
<h1>{{$product}}</h1>
#stop
#section('Body')
<h1>{{$product->Name}}</h1>
{!!Form::open([
'method' => 'delete',
'route'=> array('product.destroy',$product->id),
])!!}
{!!Form::submit('Delete')!!}
{!!Form::close()!!}
#stop
index.blade.php
#extends('layouts.layout')
#section('Head')
ALL Product
#stop
#section('Body')
#foreach($product as $col)
<h1>{{$col->Name}}</h1>
#endforeach
#stop
layout.blade.php
<!DOCTYPE html>
<html>
<head>
#yield('Head')
</head>
<body>
#yield('Body')
</body>
</html>
Ok, So im trying to delete my database based on my database id that i typed onmy browser link(so let say i type product/1),it means i want to delete my my database with id of 1.
What I've achieve so far is that I'm able to show my database based on id i typed but somehow when i want to route the id to my destroy method in ProductController class, it shows that method=>'delete' not allowed,what am i do wrong?
Try to use 'laravel data binding'.
Add to your RouteServiceProvied.php in boot method following code:
$router->model('Product', Product::class);
And change destroy method at your controller to this:
public function destroy(Product $product)
{
$product->delete();
return back();
}
And your route at show.blade.php file must be like this:
'route'=> array('product.destroy', $product),
FORM dont have DELETE method.
You have to use it like this:
In your show.blade.php
{!!Form::open([
'method' => 'post',
'route'=> array('product.destroy',$product->id),])!!}
{{ method_field('DELETE') }}
...
I think problem in your case not in delete route. After you delete record in destroy method, you return redirect to product.show route which is required parameter id.
So try
public function destroy($id)
{
$product= Product::where('idoveralRating',$id)
->delete();
return redirect()->route('product.index');
}
Ok i want to answer my own question, the problem in my code is that I'm using a default primary key of which is equal to id(in other word, $primaryKey = 'id'), that's why when i pass my $id to destroy it seems wrong because I pass primary key that doesnt belong to the table, that's why the delete method seems not recognized because of you can't delete something that not exist.
so the problem is solved with simple step
change the primary key,protected $primaryKey='idOveralRating'(for my case, and it works!)

Categories