我得到了非对象错误的属性
竞选、宣誓和会员模式相互关联,也就是说他们之间的关系是自相矛盾的。
这是我的竞选模式

   class Campaign extends Model
{
    protected $table = "campaigns";

    public function pledges()
    {
        return $this->hasMany(Pledge::class, 'campaign_id', 'id')->orderBy('created_at', 'desc');
    }
}

这是我的保证模型
   class Pledge extends Model
{
    protected $table = "pledges";

    public function campaign()
    {
        return $this->hasOne(Campaign::class, 'id', 'campaign_id');
    }


    public function member()
    {
        return $this->hasOne(Member::class, 'id', 'member_id');
    }
}

这是我的会员模型
   class Member extends Model
{
    protected $table = "members";

    public function loans()
    {
        return $this->hasMany(Loan::class, 'borrower_id', 'id');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function pledges()
    {
        return $this->hasMany(Pledge::class, 'member_id', 'id');
    }

    public function contributions()
    {
        return $this->hasMany(Contribution::class, 'member_id', 'id');
    }

    public function tags()
    {
        return $this->hasMany(MemberTag::class, 'member_id', 'id');
    }

    public function attendance()
    {
        return $this->hasMany(EventAttendance::class, 'member_id', 'id');
    }
    public function families()
    {
        return $this->hasMany(FamilyMember::class, 'member_id', 'id');
    }
    public function family()
    {
        return $this->hasOne(Family::class, 'member_id', 'id');
    }
}

这是我的view.pleedge.blade文件我不知道为什么会得到试图得到非对象错误,你的输入将非常感谢各位。
<div class="widget-content nopadding">
            <table class="table table-bordered data-table">
              <thead>
                <tr>
                  <th>Campaign</th>
                  <th>Member</th>
                  <th>Amount</th>
                  <th>Date</th>
                </tr>
              </thead>
              <tbody>


                <tr class="gradeX">


                    @foreach ($pledge as $pledges)
                      <td>{{ $pledges->member->first_name }}</td>
                       <td>{{ $pledges->campaign->campaign_id }}</td>
                        <td>{{ $pledges->pledges->amount }}</td>
                        <td>{{ $pledges->pledges->date }}</td>
                    @endforeach
                </tr>
              </tbody>
            </table>
          </div>

嘿,伙计们,这是保证控制器,便于调试。非常感谢到目前为止的反馈。
<?php

namespace App\Http\Controllers;


use App\Campaign;
use App\Member;
use App\Pledge;
use Illuminate\Http\Request;

class Pledgecontroller extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $member= Member::all();
        $campaign= Campaign::all();
        $pledge= Pledge::all();
        return view ('pledges.index', compact ('member', 'campaign', 'pledge'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $member= Member::all();
        $campaign= Campaign::all();
        $pledge= Pledge::all();
        return view ('pledges.create', compact ('member', 'campaign', 'pledge'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $pledge= new Pledge;
        $pledge->member_id = $request->member_id;
        $pledge->campaign_id = $request->campaign_id;
        $pledge->amount = $request->amount;
        $pledge->date = $request->date;
        $pledge->notes = $request->notes;
        $pledge->save();

        return redirect (route('pledge.index'));
        // return $request->all();
    }

    /**
     * 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)
    {
        //
    }

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

最佳答案

你应该使用正确的关系:

class Pledge extends Model
{
    protected $table = "pledges";

    public function campaign()
    {
        return $this->belongsTo(Campaign::class, 'campaign_id');
    }


    public function member()
    {
        return $this->belongsTo(Member::class, 'member_id');
    }
}

另外,你不应该这样做,因为你没有承诺有承诺:
$pledges->pledges->amount

此外,如果不是所有的保证都属于某个成员,则需要检查以下内容:
optional($pledges->member)->first_name

10-04 12:41