Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel crud example

<?php
  
namespace AppHttpControllers;
   
use AppModelsProduct;
use IlluminateHttpRequest;
  
class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
        $products = Product::latest()->paginate(5);
    
        return view('products.index',compact('products'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }
     
    /**
     * Show the form for creating a new resource.
     *
     * @return IlluminateHttpResponse
     */
    public function create()
    {
        return view('products.create');
    }
    
    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
        ]);
    
        Product::create($request->all());
     
        return redirect()->route('products.index')
                        ->with('success','Product created successfully.');
    }
     
    /**
     * Display the specified resource.
     *
     * @param  AppProduct  $product
     * @return IlluminateHttpResponse
     */
    public function show(Product $product)
    {
        return view('products.show',compact('product'));
    } 
     
    /**
     * Show the form for editing the specified resource.
     *
     * @param  AppProduct  $product
     * @return IlluminateHttpResponse
     */
    public function edit(Product $product)
    {
        return view('products.edit',compact('product'));
    }
    
    /**
     * Update the specified resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  AppProduct  $product
     * @return IlluminateHttpResponse
     */
    public function update(Request $request, Product $product)
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
        ]);
    
        $product->update($request->all());
    
        return redirect()->route('products.index')
                        ->with('success','Product updated successfully');
    }
    
    /**
     * Remove the specified resource from storage.
     *
     * @param  AppProduct  $product
     * @return IlluminateHttpResponse
     */
    public function destroy(Product $product)
    {
        $product->delete();
    
        return redirect()->route('products.index')
                        ->with('success','Product deleted successfully');
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: remove the public from url in laravel 
Php :: Verifying a login cookie 
Php :: laravel csrf token or protection or laravel form 
Php :: Call to undefined method :last() 
Php :: import csv in laravel 
Php :: time debug php 
Php :: Lumen framework promise 
Php :: Drupal sync directory in settings.php 
Php :: laravel select error 
Php :: redirect from controller to named route with prams in URL 
Php :: laravel blade dynamic class loop foreach 
Php :: how to check if page is opened by bot 
Php :: wordpress acf image array 
Php :: laravel migration example 
Php :: laravel localrole per many to many 3 foreign 
Php :: when WYSIWYG fields remove paragraph tag 
Php :: php count avec un tableau bidimentionnel 
Php :: seeder name singular or plural laravel 
Php :: laravel How to apply Eloquent where() to child in hasMany() relationship 
Php :: filter using meta_query 
Php :: laravel collect whereNotIn not working 
Php :: php hook function 
Php :: function wp_maintenance_mode() { 763 
Php :: public function __sleep() and __wakeup() 
Php :: laravel filemanger choose multiple images 
Php :: nodejs php 
Php :: array_shift in php 
Php :: laravel 6 migration add column to existing table 
Php :: phpunit/phpunit[6.0.0, ..., 6.5.14] require php ^7.0 - your php version (8.0.0) does not satisfy that requirement. 
Php :: laravel create custom artisan command 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =