Код: Выделить всё
{
"title": "hotel",
"link": "https://github.com/typicode/hotel",
"description": "Local app manager. Start apps within your browser, developer tool with local .localhost domain and https out of the box.",
"tags":\["node", "organizing", "webapps", "domain", "developer", "https", "proxy"\]
}
Ниже функция моего магазина
Код: Выделить всё
public function store(Request $request)
{
$request->validate([
'title'=>'required|string|max:255',
'link'=> 'required|url|max:255',
'description' => 'required|string|max:255',
'tag'=> 'required|array',
'tag.name' => 'required|max:255'
]);
$ferramentas = Ferramentas::create($request->only(['title', 'link', 'description']));
$tags = [];
foreach($request->input('tag') as $tagName){
$tag = Tags::firstOrCreate(['name'=>$tagName])->id();
}
$ferramentas->tags()->sync($tags);
return FerramentasResource::collection($ferramentas);
}
\\ Теги
Код: Выделить всё
class Tags extends Model
{
use HasFactory;
protected $fillable= [
'id',
'name'
];
protected $table = 'tags';
public $timestamps = false;
public function ferramentas(){
return $this->belongsToMany(Tags::class, 'ferramentas_tags', 'tag_id', 'ferramenta_id');
}
}
Код: Выделить всё
class Ferramentas extends Model
{
use HasFactory;
protected $fillable = [
'id',
'title',
'descriptions',
'link',
'tags'
];
protected $table = 'ferramentas';
public $timestamps = false;
public function tags(){
return $this->belongsToMany(Tags::class, 'ferramentas_tags', 'ferramenta_id', 'tag_id' );
}
}
Код: Выделить всё
Route::get('ferramentas', [FerramentasController::class, 'index']);
Route::get('ferramentas/api/{tag}', [FerramentasController::class, 'show']);
Route::post('ferramentas/api', [FerramentasController::class, 'store']);
На данный момент, когда я отправляю http-сообщение в бессоннице, например, с помощью этого json
Код: Выделить всё
{
"title": "hotel",
"link": "https://github.com/typicode/hotel",
"description": "Local app manager. Start apps within your browser, developer tool with local .localhost domain and https out of the box.",
"tags":\["node", "organizing", "webapps", "domain", "developer", "https", "proxy"\]
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... -a-restapi
Мобильная версия