简体   繁体   中英

Laravel: return true if record exist else false

I am working on a project in which any user can like someone else business. Now I want to check it with query that either the requested business is like by current user or not. I want to make laravel eloquent which need to show following output.

{
    "message": "Business found successfully",
    "status": true,
    "data": {
        "id": 1,
        "user_id": 7,
        "name": "Shimiring Car service",
        "location_name": "Gujranwali Schoole of Science and technolgoy",
        "lat": "52.124578",
        "long": "52.124578",
        "description": "This is a testing description",
        "bannar_img": "business/F8uVexNEm57A4DZ.png",
        "business_img": "business/MXKU7LDjOaBG9iF.png",
        "created_at": "2022-09-07T23:46:05.000000Z",
        "updated_at": "2022-09-13T15:56:04.000000Z",
        "is_featured": 0,
        "featured_at": "2022-09-13 05:09:04",
        "category_id": 1,
        "status": 1,
        "is_liked": false

    }
}

I am able to achieve it with two separate eloquent query but i want it to done in single query. below code is working fine but I want it to do in single eloquent?

 $business = Business::where('id', $business_id)
            ->first();

            $business['is_favourite'] = FavouriteBusiness::where('user_id', $user_id)
            ->where('business_id', $business_id)
            ->first() == null ? false : true;

Here is my query that I am currently trying with for extracting the business, with like and unlike status, but its giving nothing?

$business = Business::where('id', $business_id)
            ->whereHas('is_favourite', function($q) use($user_id) {
                return $q->where('user_id', $user_id)->exists() ? true : false;
            })

My Business model have this relation:

  public function is_favourite()
    {
        # code...
        return $this->hasOne(FavouriteBusiness::class);
    }

Here is my Favourite product migration

public function up()
    {
        Schema::create('favourite_businesses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id');
            $table->foreignId('business_id');
            $table->timestamps();
        });
    }

Here is my Business Table migration:

 public function up()
    {
        Schema::create('businesses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id');
            $table->string('name', 255)->default('');
            $table->string('location_name', 255)->default('');
            $table->string('lat', 255)->default('');
            $table->string('long', 255)->default('');
            $table->string('description', 255)->default('');
            $table->string('bannar_img', 255)->default('');
            $table->string('business_img', 255)->default('');
            $table->timestamps();

        });
    }

I'm not quite sure I fully understand the question but I have some inputs on the code provided.

Your database migrations doesn't seem to be quite correct.

The Laravel documentation states that you have to add "constrained("some-table")" when using the foreignId method to actually associate the foreign key with another table.

So your migrations should be changed. Your favourite_businesses migration should for example look like this:

public function up()
    {
        Schema::create('favourite_businesses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users');
            $table->foreignId('business_id')->constrained('businesses');
            $table->timestamps();
        });
    }

The query using whereHas should only exist of the query inside, no need to make a conditional statement, and don't forget to use get() at the end to get a collection instead of the query builder:

$business = Business::where('id', $business_id)
            ->whereHas('is_favourite', function($q) use($user_id) {
                return $q->where('user_id', $user_id);
            })->get();

After the migration is setup correctly, this will return the business which has a favorite_business based on the user_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM