0
votes

1)When I tried to use Model in update function of controller I get the error? What might be the reason for it. I got error: Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically

2)Another problem is that my delete function doesn't delete the product. It redirects in another page. What might be the mistake in routes?

ProductController.php

<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $products=product::all();
        return view('products', ['products'=>$products]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('createProduct');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Product::create(request()->validate([
            'title' => 'required',
            'type' => 'required',
            'firstname' => 'required',
            'surname' => 'required',
            'price' => 'required',
            'papl' => 'required'
        ]));



        return redirect('/products');

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function show(Product $id)
    {
       
        return view('singleProduct', ['product'=>$id]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function edit(Product $id)
    {
        
        return view('editProduct', ['product'=>$id]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function update(Product $id)
    {
        Product::update(request()->validate([
            'title' => 'required',
            'type' => 'required',
            'firstname' => 'required',
            'surname' => 'required',
            'price' => 'required',
            'papl' => 'required'
        ]));



        return redirect('/products');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $product=product::find($id)->delete();
        return redirect('/products');
    }
}

Model:Product.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    //protected $guarded = [];
    protected $fillable = ['title', 'type', 'firstname', 'surname', 'price', 'papl'];
    //use HasFactory;
    
}

Route for delete:

Route::delete('/products/{id}',[ProductController::class, 'delete'])->name('deleteProduct');

deleteProduct.blade.php

@extends('layouts/masterlayout')
@section('title', 'All Products')
@section('mainbody')
<form method = "POST" action="../../products/{{$product->id}}">
    @csrf
    @method('DELETE')
    <label>Product Name</label>
    <input type = "text" name="title" value="{{$product->title}}">
    <label>Product Type</label>
    <input type = "text" name="type" value="{{$product->type}}">
    <label>Price</label>
    <input type ="number" name="price" value="{{$product->price}}">
    <label>Firstname</label>
    <input type ="text" name="firstname" value="{{$product->firstname}}">
    <label>Surname</label>
    <input type ="text" name="surname" value="{{$product->surname}}">
    <label>Playlength/ PageNumber</label>
    <input type ="number" name="papl" value="{{$product->papl}}">
</form>
@endsection
1
You can not call update function like this, it should be like this $id->update([])Mudit Gulgulia
try this $variblename->update([]);Arjun bhati

1 Answers

1
votes

Controller

<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $products = Product::all();
        return view('products', compact('products'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('createProduct');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Product::create($request->validate([
            'title'     => 'required',
            'type'      => 'required',
            'firstname' => 'required',
            'surname'   => 'required',
            'price'     => 'required',
            'papl'      => 'required'
        ]));

        return redirect('/products');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function show(Product $product)
    {
        return view('singleProduct', compact('product'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function edit(Product $product)
    {
        return view('editProduct', compact('product'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function update(Product $product, Request $request)
    {
        $product->update($request->validate([
            'title'      => 'required',
            'type'       => 'required',
            'firstname'  => 'required',
            'surname'    => 'required',
            'price'      => 'required',
            'papl'       => 'required'
        ]));

        return redirect('/products');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function destroy(Product $product)
    {
        $product->delete();
        return redirect('/products');
    }
}

Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;
    protected $fillable = [
        'title', 'type', 'firstname', 'surname', 'price', 'papl'
    ];
    
}

Web.php

<?php

use Illuminate\Support\Facades\Route;

Route::resource('product', ProductController::class);

Blade File

@extends('layouts.masterlayout')
@section('title', 'All Products')
@section('mainbody')
    <form method="POST" action="{{ route('product.destroy', $product->id) }}">
        @csrf @method('delete')
        <label>Product Name</label>
        <input type="text" name="title" value="{{ $product->title }}">
        <label>Product Type</label>
        <input type="text" name="type" value="{{ $product->type }}">
        <label>Price</label>
        <input type="number" name="price" value="{{ $product->price }}">
        <label>Firstname</label>
        <input type="text" name="firstname" value="{{ $product->firstname }}">
        <label>Surname</label>
        <input type="text" name="surname" value="{{ $product->surname }}">
        <label>Playlength/ PageNumber</label>
        <input type="number" name="papl" value="{{ $product->papl }}">
    </form>
@endsection