Laravel – stdClass类的对象不能被转换为int。

我有一个小的搜索我的汽车表。一切都很好,直到我把这个添加到我的视图中。

@guest
   <a href="/login" class="play-btn"><i class="icon-heart"></i></a>
@else
   @if(auth()->user()->hasLiked($car))
      <span class="auto-featured"><a id="like{{$car->id}}-bs3" style="cursor: pointer;"><i 
      id="like{{$car->id}}" class="icon-heart" style="color:white"></i></a>&nbsp;&nbsp;&nbsp;&nbsp; 
      {{$car->likers()->count()}}</span>
   @else
      <span class="auto-featured"><a id="like{{$car->id}}-bs3" style="cursor: pointer;"><i 
      id="like{{$car->id}}" class="icon-heart-o" style="color:white;font-family: 'Raleway', sans-serif;"> 
      </i></a>&nbsp;&nbsp;&nbsp;&nbsp;{{$car->likers()->count()}}</span>
   @endif
@endguest

然后它就显示出这个错误: Object of class stdClass could not be converted to int. 我正在使用 overtrue 的喜欢,关注和更多.这是我的。search() 函数。

public function search(Request $request){


        $carQuery = DB::table('cars');
        $cars = Car::all();
        //$cars = Car::orderBy('placeni_status', 'desc')->get();
        // standard where fields
        foreach ($request->only(['marka', 'model']) as $term => $value) {
            if (empty($value)) {
                continue;
            }
            $carQuery->where($term, $value);
        }

        // gear is one of gears array values
        if ($sigurnost = $request->get('sigurnost')) {
            $carQuery->whereIn('sigurnost', $sigurnost);
        }

        // gear is one of gears array values
        if ($karoserija = $request->get('karoserija')) {
            $carQuery->where('karoserija', $karoserija);
        }

        // gear is one of gears array values
        if ($gorivo = $request->get('gorivo')) {
            $carQuery->where('gorivo', $gorivo);
        }

        if($lokacija = $request->get('lokacija')){
            $carQuery->where($cars->user->city, $lokacija);
        }

        // between a price from/to the values set
        if (
            $from = $request->input('from')
            && $to = $request->input('to')
        ) {
            $carQuery->whereBetween('cijena', [$from, $to]);
        }

        // between a price from/to the values set
        if (
            $fromm = $request->input('fromm')
            && $too = $request->input('too')
        ) {
            $carQuery->whereBetween('kilometraza', [$fromm, $too]);
        }

        if (
            $od = $request->input('od')
            && $do = $request->input('do')
        ) {
            $carQuery->whereBetween('godiste', [$od, $do]);
        }

        $cars = $carQuery->orderBy('placeni_status', 'desc')->get();

        return view('search.cars')->with('cars', $cars);
    }

我做错了什么,这个错误是什么意思?我只需要在搜索的汽车上显示喜欢的数量和其他一些东西,如喜爱和关注.这是我的。ajaxRequestCarsController:

/**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function ajaxRequestCars(Request $request){


        $car = Car::find($request->id);
        $response = auth()->user()->toggleLike($car);
        //$response = auth()->user()->toggleFavorite($car);

        return response()->json(['success'=>$response]);
    }

这是我的 Car.php 模型。

<?php

namespace App;

use Overtrue\LaravelFollow\Traits\CanBeLiked;
use Overtrue\LaravelFollow\Traits\CanBeBookmarked;
use Overtrue\LaravelFollow\Traits\CanBeFavorited;
use Overtrue\LaravelFollow\Traits\CanBeFollowed;
use Illuminate\Database\Eloquent\Model;
use App\User;

class Car extends Model
{
    use CanBeLiked, CanBeBookmarked, CanBeFavorited, CanBeFollowed;

    protected $table = "cars";
    protected $primaryKey = "id";

    protected $fillable = [
        'naslov', 'marka', 'model', 'kubikaza', 'zamajac', 'karoserija', 'godiste', 'kilometraza', 'br_brzina_mjenjaca',
        'gorivo', 'vlasnistvo', 'kilovata', 'konjska_snaga', 'emisiona_klasa', 'pogon', 'mjenjac', 'br_vrata', 'velicina_felni', 'posjeduje_gume',
        'br_sjedista', 'str_volana', 'klima', 'boja_spolj', 'boja_unutrasnj', 'materijal_unutrasnj', 'registracija', 'ostecenje',
        'zamjena', 'sigurnost', 'oprema', 'stanje', 'nacin_finansiranja', 'nacin_prodaje', 'cijena', 'vrsta_cijene', 'opis_oglasa', 'fotografije'
    ];

    public function user(){
        return $this->belongsTo(User::class);
    }

    public function vehicleinfo(){
        return $this->hasMany(VehicleInfo::class);
    }

    public function ad(){
        return $this->hasMany(Ad::class);
    }
}

解决方案:

我想 $car->likers() 返回关系实例,而不是实际的 likers. 试着做 $car->likers->count() 在这两个地方。

给TA打赏
共{{data.count}}人
人已打赏
未分类

从元素中添加和删除活动类

2022-9-7 22:55:20

未分类

Antlr 4没有给出正确的错误线和位置。

2022-9-7 22:55:22

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索