Add initial Recipe support to ingredient picker (WIP)

A new class it likely needed here -- IngredientAmount -- to support either a
Food or a Recipe.
This commit is contained in:
Christopher C. Wells 2021-01-22 21:09:24 -08:00 committed by Christopher Charbonneau Wells
parent 0aa2267e3b
commit 13f1302132
5 changed files with 29 additions and 19 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Models\Food;
use App\Models\Recipe;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@ -17,6 +18,7 @@ class IngredientPickerController extends Controller
$term = $request->query->get('term');
if (!empty($term)) {
$results = Food::search($term);
$results = $results->merge(Recipe::search($term));
}
return response()->json($results);
}

View File

@ -2,13 +2,13 @@
namespace App\Models;
use App\Models\Traits\Ingredient;
use App\Models\Traits\Journalable;
use App\Models\Traits\Sluggable;
use App\Support\Number;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
/**
* App\Models\Food
@ -58,9 +58,7 @@ use Illuminate\Support\Collection;
*/
class Food extends Model
{
use HasFactory;
use Sluggable;
use Journalable;
use HasFactory, Ingredient, Journalable, Sluggable;
/**
* @inheritdoc
@ -118,10 +116,4 @@ class Food extends Model
return $this->hasMany(FoodAmount::class);
}
/**
* Gets search results for a term.
*/
public static function search(string $term, int $limit = 10): Collection {
return (new static)::where('name', 'like', "%{$term}%")->limit($limit)->get();
}
}

View File

@ -2,6 +2,7 @@
namespace App\Models;
use App\Models\Traits\Ingredient;
use App\Models\Traits\Journalable;
use App\Models\Traits\Sluggable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@ -41,9 +42,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
*/
class Recipe extends Model
{
use HasFactory;
use Journalable;
use Sluggable;
use HasFactory, Ingredient, Journalable, Sluggable;
/**
* @inheritdoc
@ -56,7 +55,7 @@ class Recipe extends Model
];
/**
* The attributes that should be cast.
* @inheritdoc
*/
protected $casts = [
'servings' => 'int',

View File

@ -0,0 +1,15 @@
<?php
namespace App\Models\Traits;
use Illuminate\Support\Collection;
trait Ingredient
{
/**
* Gets search results for a term.
*/
public static function search(string $term, int $limit = 10): Collection {
return (new static)::where('name', 'like', "%{$term}%")->limit($limit)->get();
}
}

View File

@ -26,11 +26,13 @@
x-bind:data-value="result.name">
<div class="pointer-events-none">
<div x-text="result.name"></div>
<div class="text-sm text-gray-600" x-text="result.brand" x-show="result.brand"></div>
<div class="text-sm">
Serving size <span x-text="result.serving_size_formatted"></span>
<span x-text="result.serving_unit"></span>
(<span x-text="result.serving_weight"></span>g)
<div x-show="result.serving_size">
<div class="text-sm text-gray-600" x-text="result.brand" x-show="result.brand"></div>
<div class="text-sm">
Serving size <span x-text="result.serving_size_formatted"></span>
<span x-text="result.serving_unit"></span>
(<span x-text="result.serving_weight"></span>g)
</div>
</div>
</div>
</div>