Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wuschelino22's avatar

Edit Blade View

Hello, i want to build a edit view that only shows the data for the one i want to edit in my edit.blade.php I use the follow foreach at the moment on my blade. But it only shows me every thing.

@foreach($shares as $share)


<div class="panel panel-default">
      <div class="panel-heading">
      
      </div>
      <div class="panel-body">
        <form class="form-horizontal" action="{{ route('shares.edit',$share->id) }}" method="POST">
          {{ csrf_field() }}
        <fieldset>

          <div class="form-group">
            <label for="firstname" class="col-md-2 control-label">First Name</label>

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

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

            <div class="col-md-10">
              <input type="text" class="form-control" value="{{ $share->urls }}" name="lastname" placeholder="Last Name">
            </div>
          </div>

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

But i only want that it show me this one wich i have selected with the edit button on the index.view Here is the index view:

@extends('layout')

@section('content')
<style>
a:link{color:#000;}


  .uper {
    margin-top: 20px;
  
  }

  .card {
    width: 80%;
    float: right;
    background-color: #f7f7f7;
    margin-right: 50px;
  }

  .nav {
    width: 17%;
    float: left;
    background-color: #f7f7f7;
    height:80%;
    padding-top: 20px;
  padding-right: 20px;
  padding-bottom: 20px;
  padding-left: 20px;

  }

  .accordion {
    padding-top: 20px;
  padding-right: 20px;
  padding-bottom: 20px;
  padding-left: 20px;


  }


.navbar-expand-lg {

  padding-top: 20px;
  padding-right: 60px;
  padding-bottom: 20px;
  padding-left: 20px;
  height: 8%;
  border: 1px solid black;
}


</style>

<!-- Navigationsleiste -->

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="{{url('/')}}">IBP1 - Linkliste</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Link hinzufügen
</button>
      </li>
 </ul>

    <form class="form-inline my-2 my-lg-0" action="/search" method="post">
    {{ csrf_field() }}
      <input class="form-control mr-sm-2" name="search" type="search" placeholder="Suchen" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Suchen</button>
    </form>
  </div>
</nav>






<!-- Accordion  -->
<div class="accordion" id="accordionExample">
@foreach ($categories as $categorie)

@foreach ($shares as $share)
    <a href="{{route('shares.edit', $categorie->id)}}" inputtype="hidden" class="btn btn-default" action="{{$categorie->id}}" name="data-name" style="position: absolute;
        right: 20;"  ><i class="fas fa-edit"></i></a>
@endforeach


<div class="cars">
    <div class="card-header" id="headingSix">
      <h2 class="mb-0">
        <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapse{{$categorie->id}}" aria-expanded="false" aria-controls="collapse{{$categorie->id}}" style="color: black">
        {{ $categorie->name}}
        </button>

        <!--
       <a href="{{ url('/edit') }}" class="btn btn-xs btn-info pull-right" input type="submit" value="{{$categorie->id}}" style="float: right; background-color: #3490dc">Edit</a>
        -->

      </h2>

    </div>
    <div id="collapse{{$categorie->id}}" class="collapse" aria-labelledby="heading{{$categorie->id}}" data-parent="#accordionExample">
      <div class="card-body">



    <!-- Einfügen der Daten in die Tabelle nach Kategorie ID -->
      @foreach($shares->where('categorie_id', $categorie->id) as $share)
     
 
      <a href='{{ $share->urls }}' style="color: black">{{ $share->name }}</a><br>


      <div>

      </div>
      @endforeach
      </div> 
    </div>
</div>       
        @endforeach
</div>





<!-- Modal Pushfenster Link hinzufügen -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Link hinzufügen:</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
      
      <form method="post" action="{{ route('shares.store') }}">
      <div class="form-name">
              @csrf
              <label for="name">Link Name:</label>
              <input type="text" class="form-control" name="name"/>
          </div>
          <div class="form-adress">
              <label for="adress">Link Adresse:</label>
              <input type="text" class="form-control" name="urls"/>
              </div>
             
          <div class="form-kategorie">
              <label for="categorie">Kategorie:</label>
                   <option value='0' selected></option>
                   <select name='categorie_id' class='form-control'>
                                    <option value='0' selected></option>
                                    @foreach($categories as $categorie)
                                    <option value='{{ $categorie->id }}'>{{ $categorie->name }}</option>
                                    @endforeach
                                    </select>
                                       </div>
                                       <br>
                                       <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                       <button type="submit" class="btn btn-primary" action="{{ route('shares.store') }}">Hinzufügen</button>
                                       </form>
      <div class="modal-footer">
        
      </div>
    </div>
  </div>
</div>

Thats the web.php

Route::get('edit', 'ShareController@edit');

An that is my ShareCOntroller@edit:

 public function edit(Request $request, $id)

    {
       
        $shares = Url::all();
        $categories = Categorie::all();        

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

Iam a absolutly beginner have read an whatch a lot in the internet. But i cant get the right solution :-( Thank you very much

0 likes
28 replies
ftiersch's avatar

First you have to add a parameter to your edit URL

Route::get('edit/{id}', 'ShareController@edit');

Second you have to limit the URL you get in your controller

public function edit(Request $request, $id)

    {
       
        $shares = Url::findOrFail($id);

Third maybe you have to change your route() call

{{ route('shares.edit', ['id' => $share->id]) }}
jlrdw's avatar

Well for one thing, if you are new to laravel some of Jeffrey's free videos would show you some of this stuff.

Or even another basic tutorial on laravel crud.

wuschelino22's avatar

@FTIERSCH - Thank you very much. Now i get the follow error message.

</div>
<div class="pull-right">
<a class="btn btn-primary" href="<?php echo e(url('edit')); ?>"> Back</a>
</div>
</div>
</div>
<?php if($errors->any()): ?>
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
<?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<li><?php echo e($error); ?></li>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
</ul>
</div>
<?php endif; ?>
 
<?php $__currentLoopData = $shares; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $share): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
 
<form action="<?php echo e(url('edit', $share->id)); ?>" method="POST">
<?php echo csrf_field(); ?>
<?php echo method_field('PUT'); ?>
 
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
-->
 
 
<?php $__currentLoopData = $shares; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $share): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
 
 
<div class="panel panel-default">
      <div class="panel-heading">
      
      </div>
      <div class="panel-body">
        <form class="form-horizontal" action="<?php echo e(route('shares.edit',$share->id)); ?>" method="POST">
          <?php echo e(csrf_field()); ?>
 
        <fieldset>
 
Arguments
"Trying to get property 'id' of non-object (View: C:\xampp\htdocs\Linkliste\resources\views\edit.blade.php)"

That is my edit.blade

@extends('layout')

@section('content')

@foreach($shares as $share)


<div class="panel panel-default">
      <div class="panel-heading">
      
      </div>
      <div class="panel-body">
        <form class="form-horizontal" action="{{ route('shares.edit', ['id' => $share->id]) }}" method="POST">
          {{ csrf_field() }}
        <fieldset>

          <div class="form-group">
            <label for="firstname" class="col-md-2 control-label">First Name</label>

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

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

            <div class="col-md-10">
              <input type="text" class="form-control" value="{{ $share->urls }}" name="lastname" placeholder="Last Name">
            </div>
          </div>

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





@endforeach

I dont no what this error mean i have read a lot in the internet but i cant use it in my project to solve the problem.

Thank you

Tray2's avatar

Try removing the @foreach since it's only one share found you don't need to loop over it.

Ty's avatar

Is there a reason you are using a for foreach in your edit view? You are looping through each shares. you should pass the id and then present the edit view.

wuschelino22's avatar

@TRAY2 - When i dont use for each, i get the follow error message:

ErrorException (E_ERROR) Undefined variable: share (View: C:\xampp\htdocs\Linkliste\resources\views\edit.blade.php) Previous exceptions Undefined variable: share (0)

Tray2's avatar

Yes you do since you call it $shares in your controller instead of $share.

Either change in the controller or the view. I'd go for the controller option.

wuschelino22's avatar

@TRAY2 - Thank you i have done it look: My Edit Blade:

@extends('layout')

@section('content')




<div class="panel panel-default">
      <div class="panel-heading">
      
      </div>
      <div class="panel-body">
        <form class="form-horizontal" action="{{ route('shares.edit', ['id' => $shares->id]) }}" method="POST">
          {{ csrf_field() }}
        <fieldset>

          <div class="form-group">
            <label for="firstname" class="col-md-2 control-label">First Name</label>

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

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

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

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

And the ShareController:

 public function edit (Request $request, $id)

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

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

But now i get this message again when i click on the editbutton in index.blade:


C:\xampp\htdocs\Linkliste\storage\framework\viewsbc79337464b3814b5f964f1ec46b8cf5d15e923.php
</div>
<div class="pull-right">
<a class="btn btn-primary" href="<?php echo e(url('edit')); ?>"> Back</a>
</div>
</div>
</div>
<?php if($errors->any()): ?>
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
<?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<li><?php echo e($error); ?></li>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
</ul>
</div>
<?php endif; ?>
 
<?php $__currentLoopData = $shares; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $share): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
 
<form action="<?php echo e(url('edit', $share->id)); ?>" method="POST">
<?php echo csrf_field(); ?>
<?php echo method_field('PUT'); ?>
 
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
-->
 
 
 
 
<?php echo $__env->make('layout', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\xampp\htdocs\Linkliste\resources\views/edit.blade.php ENDPATH**/ ?>
Arguments

"Trying to get property 'id' of non-object (View: C:\xampp\htdocs\Linkliste\resources\views\edit.blade.php)"

"Trying to get property 'id' of non-object (View: C:\xampp\htdocs\Linkliste\resources\views\edit.blade.php)"

Tray2's avatar

Run this command and try again

php artisan view:clear
wuschelino22's avatar

@TRAY2 - I have done it but the same error message. Do you have another idea ? Thank you

Ty's avatar

Your edit should be really simple and doesn't require a request. your request is done on the

public function update(Request $request, $id) {.......}

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

       return view('users.edit', compact('shares'));

   }
wuschelino22's avatar

@TY - In my ShareControler i have edit it in :

public function edit (Request $request, $id)

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

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

but i got the error message again:

Trying to get property 'id' of non-object (View: C:\xampp\htdocs\Linkliste\resources\views\edit.blade.php) Previous exceptions Trying to get property 'id' of non-object (0)

jlrdw's avatar

i want to build a edit view that only shows the data for the one i want to edit in my edit.blade.php I use the follow foreach at the moment on my blade. But it only shows me every thing.

Have you even viewed any documentation on retrieving just one record.

// Retrieve a model by its primary key...
$flight = App\Flight::find(1);

// Retrieve the first model matching the query constraints...
$flight = App\Flight::where('active', 1)->first();

It is literally right in the docs, examples That Taylor put there.

wuschelino22's avatar

@JLRDW - i have watch a lot of crud tutorial befor i put my question here in the channel. No tutorial help me sorry:-(

jlrdw's avatar

i have watch a lot of crud tutorial befor

Can't just watch, have to also do the code in the tutorials.

In other words practice the code while you learn.

Prior to laravel, do you even know the difference in PDO

fetchAll    

  // verses

fetch

That difference is the key to what you need here.

That with the examples in Taylor's documentation. Jeffrey also has free video courses for beginners.

Some basic PDO and PHP he covers in another free course.

No reply needed just saying learn some of the basics first and it will all fall into place easier.

Ty's avatar

@wuschelino22 why do you still have Request $request? remove that and just have $id your request should be on your update method.

wuschelino22's avatar

@TY - Now i have done this , but also the same error.

public function edit ($id)

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

        return view('edit', compact('shares'));
    }
Ty's avatar

can you show more of your controller? something is missing. Did you create the controller manually or did you create it through artisan using --resource

Ty's avatar

change your edit form to read like this.

<form class="form-horizontal" role="form" method="POST" action="{{ url('shares', $shares->id) }}">
    {{ csrf_field() }}
    {{ method_field('PATCH') }}
wuschelino22's avatar

@TY - Hello now i have change my Edit Blade to this:

@extends('layout')

@section('content')




<div class="panel panel-default">
      <div class="panel-heading">
      
      </div>
      <div class="panel-body">
        


      <!--  <form class="form-horizontal" action="{{ route('shares.edit', ['id' => $shares->id]) }}" method="POST">
          {{ csrf_field() }}-->






    <form class="form-horizontal" role="form" method="POST" action="{{ url('shares', $shares->id) }}">
    {{ csrf_field() }}
    {{ method_field('PATCH') }}

        <fieldset>

          <div class="form-group">
            <label for="firstname" class="col-md-2 control-label">First Name</label>

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

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

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

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













<!--

<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Profile</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ url('edit') }}"> 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

@foreach ($shares as $share)

<form action="{{ url('edit', $share->id) }}" method="POST">
@csrf
@method('PUT')

@endforeach
-->

But i got also the error message:

Trying to get property 'id' of non-object (View: C:\xampp\htdocs\Linkliste\resources\views\edit.blade.php)

This ist my Controller i build it with php artisan:

<?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)
    {
     
     Url::find($id)->update($request->all());
      return redirect()->route('index')->with('success','Post update successfully');

    }

    /**
     * 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'));


    }

    
}

Thank you very very much

ftiersch's avatar

Yes, you get that error message because you are still using $share->id inside of your commented out code. Making an HTML comment out of it doesn't stop the PHP functionality from being executed. Remove the whole comment.

jlrdw's avatar

Also look at how many questions and answers was done here.

Editing a record is a real basic thing in laravel.

If you would take some of the basic tutorials first, you would learn this stuff.

Much of the code is available free on GitHub so you can study.

Please or to participate in this conversation.