单击编辑按钮时尝试获取非对象的属性“id”

我是 Laravel 的新手。我有一个页面,其中显示了所有已申请工作的候选人。


工作申请.blade.php


<table class="table table-hover table-striped" id="dataTable" style="font-size: 13px !important">


    <thead>

        <th style="padding:5px 22px 10px 6px !important">Candidate</th>

        <th style="padding:5px 22px 10px 6px !important">Status</th>

        <th style="padding:5px 22px 10px 6px !important">Edit</th>

    </thead>


    <tbody>


        @foreach ($applications as $application)


        <tr>

            <td>

            <a class="text-center" href="{{ route('candidates.show', $application->user->username) }}"


                target="_blank" class="text-theme">{{ $application->user->name }}</a>

            </td>


            <td>

                {{ $application->status }}

            </td>


            <td>

                    <a href="route('employers.applicants.edit', $applicant->id)" class="mt-1 text-center btn-sm btn btn-outline-yellow"> <i class="fa fa-pencil"></i> Edit</a> 

            </td>

        </tr>

    </tbody>

 </table>

每当我单击编辑按钮时,它都会显示错误


尝试获取非对象的属性“id”


这是我的 jobApplications 控制器代码


public function jobApplications($slug)

{

    if (!Auth::check()) {


        session()->flash('error', 'Sorry !! You are not an authenticated Employer !!');


        return back();

    }


    $user = Auth::user();


    $user_id = $user->id;


    $applicant = DB::table('job_activities')->where('user_id',$user_id)->get();


    $job = Job::where('slug', $slug)->first();


    $expreience_data = Experience::all();


    $query = JobActivity::query();


    $applications = $query->where('job_id', $job->id)->get();


    $experiences = [];


    $education = [];


    $application_data = [];



它在线上给出错误:


$applications = $query->where('job_id', $job->id)->get();

根据我可以通过互联网找到的解决方案,这是因为我试图将对象类型 ID 分配给数组应用程序。我尝试将 $jobs 变量上的 first() 更改为 get() 但随后出现错误


此集合实例上不存在属性 [id]。


30秒到达战场
浏览 97回答 2
2回答

慕村225694

这里有一个问题:<a href="route('employers.applicants.edit', $applicant->id)" class="mt-1 text-center btn-sm btn btn-outline-yellow">&nbsp;<i class="fa fa-pencil"></i> Edit</a>{{ }}您忘记了应该围绕路线()助手的大括号应该:<a href="{{ route('employers.applicants.edit', $applicant->id) }}" class="mt-1 text-center btn-sm btn btn-outline-yellow">&nbsp;<i class="fa fa-pencil"></i> Edit</a>

富国沪深

你的$applicant是一个收藏首先,您要迭代$applications并执行 Blade 中的编辑按钮,$applicant而不是$application. 您的$applicant变量是一个集合而不是单个项目。刀片文件错误@foreach你的刀片文件里没有尽头。您可以放在元素@endforeach后面</tr>您可以更改应用程序查询您可以使用此行简化您的应用程序$applications = JobActivity::where('job_id', $job->id)->get();如果仍然出现错误,请检查变量$job设置是否正确 - 是否找到记录并且对象具有属性 id。也许$slug变量设置不正确,也证明这个变量。
打开App,查看更多内容
随时随地看视频慕课网APP