【问题标题】:laravel trashed method does not existlaravel 垃圾方法不存在
【发布时间】:2020-03-20 01:56:47
【问题描述】:

我正在进行一个项目,我希望在该表中显示我被丢弃的记录及其正常工作以及我被丢弃的记录。

我的代码逻辑是,当我的已删除记录显示在操作字段中时,它只显示恢复按钮,否则在该字段中显示两个按钮“删除”和“垃圾箱”。为此,当我在我的视图文件中应用这个条件时,它会返回这个写在我标题中的错误。 @if($record->trashed())

资源控制器

namespace App\Http\Controllers;

use App\sepCategory; use Illuminate\Http\Request;

class SepCategoryController extends Controller {
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $cat=sepCategory::all();
        return view('cusCate',compact('cat'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

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

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

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

        $a= sepCategory::find($id);

       return  view('cusEdit',['data'=>$a]);

    }    

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\sepCategory  $sepCategory
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, sepCategory $sepCategory)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\sepCategory  $sepCategory
     * @return \Illuminate\Http\Response
     */
    public function destroy(sepCategory $sepCategory,$id)
    {
          if ($sepCategory::find($id)->forceDelete()) {
                return back()->with('message','record successfully delete');
          } else {
            return back()->with('error','record not delete');
          }





    }
    public function trash()
    {
         $cat=sepCategory::onlyTrashed()->paginate(3);
         return view('cusCate',compact('cat'));
    }

    public function remove($id)
    {
       $temp= sepCategory::find($id);
       if ($temp->delete()) {
           return back()->with('message','record successfully trashed');

       }else{
           return back()->with('error','can not correctly trashed');
       }
    }

    public function recover($id)
    {

    } }

查看刀片文件

 <a href="{{ url('category/trash') }}" class="btn-success">view the trashed data</a>


Category of student

   @if(session()->has('message'))
     <div class="alert alert-success">
      {{ session()->get('message') }}
      </div>
  @elseif(session()->has('error'))
      <div class="alert alert-danger">
     {{ session()->get('error') }}
      </div>
  @else
  @endif
  <table>
  <tr>
    <th>Name</th>
    <th>id</th>
    <th>Phone</th>
    <th>Roll no</th>
    <th>Action</th>
    <th>
         @if ($cat->trashed())
         Deleted At
         @else
        updated At
         @endif
        </th>
</tr>




@if (isset($cat))


@foreach ($cat as $item)
<tr>


    <td>{{ $item->name }}</td>
  <td>{{ $item->id }}</td>
  <td>{{ $item->mobile_no }}</td>
  <td>{{ $item->roll_no }}</td>
  <td>


    @if ($item->trashed())
        <a href="" class="btn btn-success">Restore</a>
    @else

        <a href="{{ route('catgry.edit',$item->id) }}" class="btn btn-success">Edit</a>

        <form action="{{ url('/home/destroy') }}" method="post">
          @csrf
          {{ method_field('DELETE') }}
          <input type="hidden" value="{{ $item->id }}" name="id">
          <button type="submit">Delete</button>
        </form>



       <!-- this is trashed-->
       <a href="{{ url('category/TempRemove',$item->id) }}" class="btn btn-primary">Trashed</a>
    @endif


        </td>




        <td>
                @if ($item->trashed())
                      {{ $item->deleted_at }}
                @else
                      {{ $item->updated_at }}
                @endif

        </td>






</tr>
@endforeach

  @endif

错误

方法 Illuminate\Database\Eloquent\Collection::trashed 不存在。 (查看:D:\xamp\htdocs\project laravel\newTran\resources\views\cusCate.blade.php)

为什么说垃圾不存在

【问题讨论】:

    标签: php laravel laravel-5 eloquent laravel-6


    【解决方案1】:

    首先,如果您在查询时使用withTrashed() 范围,则可以使用trashed() 方法。因此,您需要添加此内容(另请注意,我将 all() 替换为 get()):

    # SepCategoryController.php
    
    $cat = sepCategory::withTrashed()->get();
    //                  ^^^^^^^^^^^^^^^^^^^^
    return view('cusCate', compact('cat'));
    

    请注意,此模型必须使用 SoftDeletes 特征。

    现在,您在 Collection 实例而不是模型实例上调用 trash() 方法。这是因为$catsepCategory 的集合:

    # SepCategoryController.php
    
    $cat = sepCategory::withTrashed()->get();
    
    return view('cusCate', compact('cat'));
    
    <!-- In your view -->
    <th>
      @if ($cat->trashed()) // <---
        Deleted At
      @else
        updated At
      @endif
    </th>
    

    您应该遍历此集合的元素以访问各个模型:

    @foreach($cat as $category)
      <th>
        @if ($category->trashed()) // <---
          Deleted At
        @else
          updated At
        @endif
      </th>
    @endforeach
    

    【讨论】:

    • @MuhammadAhmad 很高兴为您提供帮助。您现在可以接受答案了。
    猜你喜欢
    • 2014-07-02
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2021-03-25
    • 2013-07-18
    • 1970-01-01
    相关资源
    最近更新 更多