代码之家  ›  专栏  ›  技术社区  ›  BARNOWL

Laravel无法获取试图获取非对象属性的访问器

  •  1
  • BARNOWL  · 技术社区  · 6 年前

    我正在尝试获取map对象的值,以检查用户是否进行了评级。

    {{ $book->rated }} 不是假的,也不是真的。

    修补匠

    $book->getTypeAttribute();PHP注意:正在尝试获取 在线/Applications/MAMP/htdocs/eliating/app/Book.php中的非对象 四十

    此方法需要获取评级的类型,默认值设置为false

    public function getTypeAttribute(){        
        return Rate::where('type', $this->attributes['id'])->where('user_id',auth()->user()->id) === self::RATED; 
    }
    

    Book.php文件

    use Illuminate\Database\Eloquent\Model;
    use willvincent\Rateable\Rateable;
    use App\User;
    use App\Rate;
    
    class Book extends Model
    {
    
        use Rateable;
    
        const RATED = "true";
        const NOT_RATED = "false";
    
        protected $fillable = [ 'user_id', 'title', 'description'];
    
    
        public function scopeGetBook($query, $book_name )
        {
            return $query->where('slug',  $book_name );
        }
    
        public function setTitleAttribute($value)
        {
            $this->attributes['title'] = $value;
            $this->attributes['slug'] = str_slug($value);
    
        }
    
        public function scopeGetBookUser($query, $user_id)
        {
            return $query->where('user_id',  $user_id )->first();
        }
    
        public function getTypeAttribute(){        
            return Rate::where('type', $this->attributes['id'])->where('user_id',Auth()->user()->id) === self::RATED; 
        }
    

    率.php

    <?php
    
    namespace App;
    
    
    use App\User;
    
    use Illuminate\Database\Eloquent\Model;
    use willvincent\Rateable\Rateable;
    
    class Rate extends Model
    {
        protected $fillable = [
            'user_id',
            'type',
            'rating'
        ];
    
    
        public $timestamps = false;
    
        protected $table = 'ratings';
    
    
    
    
    
    }
    

    BookController.php文件 (这是设置类型的方式,也是我尝试检索值的方式)

    public function rate(Request $request, $book_id)
    {
    
        $book = Book::find($book_id);
        $rating = $book->ratings()->where('user_id', auth()->user()->id)->first();
    
        if(is_null($rating)){
            $ratings = new Rating();
            $ratings->rating =  $request['rating'];
            $ratings->user_id = auth()->user()->id;
            $ratings->type = Book::RATED;
            $book->ratings()->save($ratings);
    
    
    
    
            return json_encode($book);
    
        }
        else{
           return response()->json(['status' => 'You already left a review']);
        }
    }
    
      public function show($book_name)
    {
        $books = Book::with('ratings')->GetBook($book_name)->get();
    
    
        $data = $books->map(function(Book $book){
    
            $book['rated'] = $book->getTypeAttribute();
    
            return $book;
    
        });
    
        return view('books.show', compact('data', $data));
    }
    

    HTML格式

      <div id="rateYo" data-rateyo-rating="{{  $book->userSumRating or 0}}" data-rateyo-read-only="{{ $book->rated }}" > ></div>
    

    图像(只读属性必须有true或false)两者都不显示。

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   Brian Lee    6 年前

    你需要打电话 first get 在访问器中的查询上:

    return Rate::where(['type' => $this->getKey(), 'user_id' => auth()->id()])->first()
    

    另外,在使用访问器时,不调用完整函数,如 getTypeAttribute ,只使用属性名, type .

    // wrong
    $book['rated'] = $book->getTypeAttribute();
    
    // right
    $book['rated'] = $book->type;
    

    但是,我认为如果用户留下了评分,您应该使用 exists doesntExist 查询函数。例如:

    // this will return true/false
    public function getRatedAttribute()
    {
        return Rate::where(['type' => $this->getKey(), 'user_id' => auth()->id()])->exists();
    }
    

    将属性附加到 Book 型号:

    // this will add the attribute to each book in the query result.
    protected $appends = ['rated']; // or type
    

    然后您可以简单地使用:

    $book->rated; // true if a rating exists, otherwise false.