مهدی
3 سال پیش توسط مهدی مطرح شد
3 پاسخ

resource laravel

سلام خسته نباشید
یه سوال من یک جدول واسط دارم که میخوام یک ریسورس براش بنویسم چجوری باید این کار رو بکنم چون توی جدول واسطم یکسری اطلاعات اضافی دارم که یکسری جاها نیازشون ندارم چجوری میتونم این کار رو بکنم باتشکر


ثبت پرسش جدید
Ali
@411proplayer 3 سال پیش آپدیت شد
0

منظورتون چیه چه شکلی ریسورس درست کنم ؟
مثال من :
وقتی کسی پیچ کاربر رو میبینه باید تعداد فالوور ها و کسایی که فالو کرده رو ببین که اینا همش بر اساس جدول واسط پیاده شده
api

Route::get('{user:username}', [UserController::class, 'show'])->name('user.show');

UserController

<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\UserPageResource;

class UserController extends Controller
{
    public function show(User $user)
    {
        return new UserPageResource($user);
    }
}

UserPageRecource

<?php

namespace App\Http\Resources;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Resources\Json\JsonResource;

class UserPageResource extends JsonResource
{
    public static $wrap = null;

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'name' => $this->name,
            'username' => $this->username,
            'bio' =>  $this->bio,
            'avatar' => $this->avatar ?? asset('statics/profile.svg'),
            'followers' => $this->followers()->count(),
            'following' => $this->following()->count(),
            'followed' => $this->followers()->where('follower_id', auth('sanctum')->id())->exists(),
            'verified' => $this->verified,
        ];
    }
}

User Model

// ---------- RelationShips ----------
    public function followers()
    {
        return $this->belongsToMany(User::class, 'followers', 'user_id', 'follower_id');
    }

    public function following()
    {
        return $this->belongsToMany(User::class, 'followers', 'follower_id', 'user_id');
    }

مهدی
@MM20001378 3 سال پیش مطرح شد
0

ببینید من الان این کد رو زدم این یک مثاله

public function show(Post $post)
    {
        return new PostShowResource($post);
    }

resource

 public function toArray($request)
    {
        return [
            'title' => $this->title,
            'reply' => $this->reply,
            'reply_comment' => $this->users,
        ];
    }

خب حالا چیزی که دریافت میکنم اینه

{
    "data": {
        "title": "post5",
        "reply": 1,
        "reply_comment": [
            {
                "id": 1,
                "name": "ali",
                "email": "a@gmail.com",
                "email_verified_at": null,
                "created_at": "2021-07-12T17:43:42.000000Z",
                "updated_at": "2021-07-12T17:43:42.000000Z",
                "pivot": {
                    "post_id": 5,
                    "user_id": 1,
                    "created_at": "2021-07-19T05:58:24.000000Z",
                    "updated_at": "2021-07-19T05:58:24.000000Z",
                    "body": "کامنت اول"
                }
            },
            {
                "id": 1,
                "name": "ali",
                "email": "a@gmail.com",
                "email_verified_at": null,
                "created_at": "2021-07-12T17:43:42.000000Z",
                "updated_at": "2021-07-12T17:43:42.000000Z",
                "pivot": {
                    "post_id": 5,
                    "user_id": 1,
                    "created_at": "2021-07-19T06:23:40.000000Z",
                    "updated_at": "2021-07-19T06:23:40.000000Z",
                    "body": "کامنت اول"
                }
            },
            {
                "id": 1,
                "name": "ali",
                "email": "a@gmail.com",
                "email_verified_at": null,
                "created_at": "2021-07-12T17:43:42.000000Z",
                "updated_at": "2021-07-12T17:43:42.000000Z",
                "pivot": {
                    "post_id": 5,
                    "user_id": 1,
                    "created_at": "2021-07-19T06:23:43.000000Z",
                    "updated_at": "2021-07-19T06:23:43.000000Z",
                    "body": "کامنت اول"
                }
            }
        ]
    }
}

در صورتی که توی اون pivot من نمیخوام created_at رو داشته باشم برای این کار باید چ کنم ؟
@411proplayer


Ali
@411proplayer 3 سال پیش مطرح شد
0

یدونه user resource بساز و تو اونجا استفاده کن

return [
            'title' => $this->title,
            'reply' => $this->reply,
            'reply_comments' => UserResource::collection($this->users),
];

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام