PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 29, 2022

[FIXED] Call to a member function delete() on null

 January 29, 2022     laravel, laravel-5     No comments   

Issue

I want to delete a row from table. The function I used in controller is:

public function destroy($id)
{
    $post = Post::find($id);
    $post->delete();
    Session::flash('success', 'The post was just trashed.');

    return redirect()->back();
}

However it throws me an error:

Call to a member function delete() on null

when I check $post if it's empty with the code:

$post = Post::find($id); dd($id);

I get the output: "id => $post->id"

If i change $post = Post::find(1); It actually updates the deleted_at field in the database.

All the answers I've gotten so far only prevents the code from throwing an error.

Route Method:

Route::get('/post/delete/{id}', [
    'uses' => 'PostsController@destroy',
    'as' => 'post.delete'
]);

Code from the Blade file:

<table class="table table-hover">
    <thead>
        <th>
            Image
        </th>

        <th>
            Title
        </th>

        <th>
            Edit
        </th>

        <th>
            Delete
        </th>
    </thead>

    <tbody>
    @foreach($posts as $post)
    <tr>

    <td><img src="{{ $post->featured }}" alt="{{ $post->title }}" width="90px;" height="50px;"></td>
    <td>{{ $post->title }}</td>
    <td>Edit</td>
    <td><a href="{{ route('post.delete',['id => $post->id']) }}" class="btn btn-danger">Trash</a></td>

    </tr>

    @endforeach
    </tbody>
</table>

    </div>
</div>

Solution

well the problem is actually in your blade file, passing the parameter to the route. you are passing a parameter like route('post.delete',['id => $post->id']) which makes id => $post->id a single parameter as it is a string. you have to make it like

<a href="{{ route('post.delete',['id' => $post->id]) }}" class="btn btn-danger">Trash</a>

as you are sending a single parameter you can write it just like

<a href="{{ route('post.delete',$post->id) }}" class="btn btn-danger">Trash</a>

now a bit of advice for you. never use a get request for deleting something. use either post request or delete request for deleting.

an example using delete request.

Route::delete('post/delete/{id}', 'PostsController@destroy')->name('post.delete'); 

now send a delete request from blade file using a form.

<form method="post" action="{{ route('post.delete', $post->id) }}">
    @csrf
    @method('delete')    

    <button type="submit" class="btn btn-danger">Trash</button>
</form>


Answered By - zahid hasan emon
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing