G
G
gomer17262019-02-13 13:08:09
Laravel
gomer1726, 2019-02-13 13:08:09

How to access laravel model methods in vue?

I'm using vue on Laravel and I'm having this problem.
I extract through axious let's say model data

public function getPosts()
  {
     $posts = Post::all();
     return response()->json([
        'posts' => $posts
     ]);
  }

In the vue component, all this data is all ok
But let's say I have my own custom method in the model
public function thumb()
  {
     return 'images/thumb/'.$this->img; 
  }

how to call this method in vue?
{{ note.thumb() }}
it won't work
, what are the options?
You don’t need to write that the path to the image can also be put in vue, I know this, but I have a lot of custom methods, with different logic. Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2019-02-13
@gomer1726

getThumbAttribute()+protected $appends = ['thumb']

J
jazzus, 2019-02-13
@jazzus

Laravel Resource
Need to be done Write
in this file

public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
           // Метод из модели
            'thumb' => $this->thumb(),
        ];
    }

And in the controller in the header we connect use App\Http\Resources\PostResource as PostResource;
and pass json in the method
public function getPosts()
  {
     $posts = Post::all();
     return PostResource::collection($posts); 
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question