Update with CRUD

Posted 1 month ago by wuschelino22

Hallo, i want to Update my SQL Databade via CRUD (Update)

This is my Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use View;
use App\Url;
use App\Categorie;


class ShareController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $shares = Url::all();
        $categories = Categorie::all();

        return view('index', compact('shares', 'categories'));

        //**Holt alle benötigten Daten für die Indexseite aus den beiden Datenbanken */
}


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = Categorie::all();
    
        return view('create', compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

             $share = new Url([
            'name' => $request->get('name'),
            'urls'=> $request->get('urls'),
            'categorie_id'=> $request->get('categorie_id')
          ]);
          $share->save();
          return back();
    }

    /**
     * Display the specified resource. 
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit ($id)

    {
       
        $shares = Url::findOrFail($id);
        $categories = Categorie::get();  

        return view('edit', compact('shares'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
     
      $share = Url::find($id)([
            'name' => $request->get('name'),
            'urls'=> $request->get('urls'),
            'categorie_id'=> $request->get('categorie_id')
          ]);
          $share->save();
          return back();
             

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function search(Request $request)
    {
 

    $urls = Url::where('name', $request->get('search'))
    ->orWhere('name', 'like', '%' . $request->get('search') . '%')->get();

    $shares = $urls;

    $categories = Categorie::whereIn('id', $shares->pluck('categorie_id'))->get();

    return view('index', compact('shares', 'categories'));


    }

    
}



And this is my EditBlade

@extends('layout')

@section('content')




<div class="panel panel-default">
      <div class="panel-heading">
      
      </div>
      <div class="panel-body">
    <form class="form-horizontal" role="form" method="POST" action="{{ route('shares.update', $shares->id) }}">
    @csrf
       
    <fieldset>
            <div class="form-group">
            <label for="firstname" class="col-md-2 control-label">Name:</label>

            <div class="col-md-10">
              <input type="text" class="form-control" value="{{ $shares->name }}" name="name" placeholder="First Name">
            </div>
          </div>

          <div class="form-group">
            <label for="lastname" class="col-md-2 control-label">Url:</label>

            <div class="col-md-10">
              <input type="text" class="form-control" value="{{ $shares->urls }}" name="urls" placeholder="Last Name">
            </div>
          </div>
          <button type="POST" class="btn btn-primary" action="{{ route('shares.update', $shares->id) }}">Update</button>


      </form>
     </div>
    </div>
  </div>

And this my web.php

Route::resource('shares', 'ShareController');

Route::get('create', '[email protected]eate');

Route::get('/', '[email protected]');

Route::post('search', '[email protected]');

Route::get('edit/{id}', '[email protected]');

Route::get('update', '[email protected]');

I get the follow Error Message what do i wrong ?

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.

Thank you very much

Please sign in or create an account to participate in this conversation.