在null

时间:2019-10-21 06:08:49

标签: laravel laravel-5

我想从表中删除一行。我在控制器中使用的功能是:

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

    return redirect()->back();
}

但是它抛出了一个错误:

  

在null上调用成员函数delete()

当我检查$ post的代码是否为空时:

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

我得到输出: “ id => $ post-> id”

如果我改变 $ post = Post :: find(1); 实际上,它会更新数据库中的Deleted_at字段。

到目前为止,我得到的所有答案都只能防止代码抛出错误。

路由方法:

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

Blade文件中的代码:

        

<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>

7 个答案:

答案 0 :(得分:2)

  

在null上调用成员函数delete()

此错误消息表示您正在尝试调用不存在的对象的方法。空对象。

在这种情况下,Post::find($id)返回。因为它找不到任何$id记录。

因此您的$post变量为空。

要消除错误消息,请检查$post变量是否为空。

public function destroy($id)
{
    $post = Post::find($id);

    if(empty($post)) {
        return;
    }

    $post->delete();
    Session::flash('success', 'The post was just trashed.');

    return redirect()->back();
}

答案 1 :(得分:1)

更好的是,利用route model binding

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

在控制器中:

public function destroy(Post $post)
{
    $post->delete();

    Session::flash('success', 'The post was just trashed.');

    return redirect()->back();
}

这将在后台使用findOrFail,因此如果该帖子不存在,它将抛出404。

要在您的视图中使用它:

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

    <button type="submit">Delete</button>
</form>

答案 2 :(得分:0)

使用这种方法:

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

答案 3 :(得分:0)

有很多方法可以解决其他问题,这是另一种方法!我更喜欢使用。

public function destroy($id)
{

    $validator = Validator::make(compact('id'), [
        'id' => 'required|int|exists:posts,id'
    }

    if ($validator->fails()) {
        return response([ERROR_MESSAGE], [ERROR_CODE]);
    }

    $post = Post::find($id)->delete();
    return response([SUCCESS_MESSAGE], [SUCCESS_CODE]);
}

&为了更加小心,最好将代码放在“ Try catch”中。

答案 4 :(得分:0)

public function destroy($id)
{
    $post = Post::find($id);

    if(is_null($post)) {
        abort(404);
    }

    $post->delete();

    Session::flash('success', 'The post was just trashed.'); 

    return redirect()->back();
}

答案 5 :(得分:0)

最佳做法是在laravel中使用findOrFail帮助函数。您可以处理以下异常:Post不存在,并且在前面显示特定的错误消息,如下所示,

使用Illuminate \ Database \ Eloquent \ ModelNotFoundException;

public function destroy($id)
{
    // Will return a ModelNotFoundException if no user with that id
    try
    {
        $post = Post::findOrFail($id);
        $post->delete();
        Session::flash('success', 'The post was just trashed.');
        return redirect()->back();
    }
    // catch(Exception $e) catch any exception
    catch(ModelNotFoundException $e)
    {
        Session::flash('error', 'The post does not exist.');
        return redirect()->back();
    }
}

这样,您的应用将永远不会崩溃。希望这会有所帮助!

答案 6 :(得分:0)

问题实际上出在刀片文件中,将参数传递给了路由。您正在传递类似route('post.delete',['id => $post->id'])的参数,该参数使id => $post->id成为单个参数,因为它是字符串。您必须使其像

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

当您发送单个参数时,您可以像写它一样

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

现在为您提供一些建议。切勿使用get请求删除某些内容。使用post请求或delete请求进行删除。

使用delete请求的示例。

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

现在使用表格发送来自刀片文件的删除请求。

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

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